1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Mon Sep 24 11:07:38 2012 +0200
1.3 @@ -0,0 +1,101 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +package org.apidesign.vm4brwsr;
1.9 +
1.10 +import javax.script.Invocable;
1.11 +import javax.script.ScriptException;
1.12 +import org.testng.annotations.Test;
1.13 +import static org.testng.Assert.*;
1.14 +
1.15 +/**
1.16 + *
1.17 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.18 + */
1.19 +public class InstanceTest {
1.20 + @Test public void verifyDefaultDoubleValue() throws Exception {
1.21 + assertExec(
1.22 + "Will be zero",
1.23 + "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
1.24 + Double.valueOf(0)
1.25 + );
1.26 + }
1.27 + @Test public void verifyAssignedByteValue() throws Exception {
1.28 + assertExec(
1.29 + "Will one thirty one",
1.30 + "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
1.31 + Double.valueOf(31)
1.32 + );
1.33 + }
1.34 + @Test public void verifyMagicOne() throws Exception {
1.35 + assertExec(
1.36 + "Should be three and something",
1.37 + "org_apidesign_vm4brwsr_Instance_magicOneD",
1.38 + Double.valueOf(3.3)
1.39 + );
1.40 + }
1.41 + @Test public void verifyInstanceMethods() throws Exception {
1.42 + assertExec(
1.43 + "Should be eleven as we invoke overwritten method, plus 44",
1.44 + "org_apidesign_vm4brwsr_Instance_virtualBytesI",
1.45 + Double.valueOf(55)
1.46 + );
1.47 + }
1.48 + @Test public void verifyInterfaceMethods() throws Exception {
1.49 + assertExec(
1.50 + "Retruns default value",
1.51 + "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
1.52 + Double.valueOf(31)
1.53 + );
1.54 + }
1.55 +
1.56 + @Test public void isNull() throws Exception {
1.57 + assertExec(
1.58 + "Yes, we are instance",
1.59 + "org_apidesign_vm4brwsr_Instance_isNullZ",
1.60 + Double.valueOf(0.0)
1.61 + );
1.62 + }
1.63 +
1.64 + @Test public void isInstanceOf() throws Exception {
1.65 + assertExec(
1.66 + "Yes, we are instance",
1.67 + "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
1.68 + Double.valueOf(1.0), true
1.69 + );
1.70 + }
1.71 +
1.72 + @Test public void notInstanceOf() throws Exception {
1.73 + assertExec(
1.74 + "No, we are not an instance",
1.75 + "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
1.76 + Double.valueOf(0.0), false
1.77 + );
1.78 + }
1.79 +
1.80 + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.81 + StringBuilder sb = new StringBuilder();
1.82 + Invocable i = StaticMethodTest.compileClass(sb,
1.83 + "org/apidesign/vm4brwsr/Instance"
1.84 + );
1.85 +
1.86 + Object ret = null;
1.87 + try {
1.88 + ret = i.invokeFunction(methodName, args);
1.89 + } catch (ScriptException ex) {
1.90 + fail("Execution failed in " + sb, ex);
1.91 + } catch (NoSuchMethodException ex) {
1.92 + fail("Cannot find method in " + sb, ex);
1.93 + }
1.94 + if (ret == null && expRes == null) {
1.95 + return;
1.96 + }
1.97 + if (expRes.equals(ret)) {
1.98 + return;
1.99 + }
1.100 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
1.101 +
1.102 + }
1.103 +
1.104 +}