vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Nov 2012 17:07:51 +0100
changeset 199 866e4f4820c7
parent 113 1b6410322d6e
child 203 c6a0b5b64133
permissions -rw-r--r--
Dump the code to file
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import javax.script.Invocable;
    21 import javax.script.ScriptException;
    22 import org.testng.annotations.Test;
    23 import static org.testng.Assert.*;
    24 import org.testng.annotations.BeforeClass;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class InstanceTest {
    31     @Test public void verifyDefaultDoubleValue() throws Exception {
    32         assertExec(
    33             "Will be zero",
    34             "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
    35             Double.valueOf(0)
    36         );
    37     }
    38     @Test public void verifyStaticMethodCall() throws Exception {
    39         assertExec(
    40             "Will be zero",
    41             "org_apidesign_vm4brwsr_InstanceSub_recallDblD",
    42             Double.valueOf(0)
    43         );
    44     }
    45     @Test public void verifyAssignedByteValue() throws Exception {
    46         assertExec(
    47             "Will one thirty one",
    48             "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
    49             Double.valueOf(31)
    50         );
    51     }
    52     @Test public void verifyMagicOne() throws Exception {
    53         assertExec(
    54             "Should be three and something",
    55             "org_apidesign_vm4brwsr_Instance_magicOneD",
    56             Double.valueOf(3.3)
    57         );
    58     }
    59     @Test public void verifyInstanceMethods() throws Exception {
    60         assertExec(
    61             "Should be eleven as we invoke overwritten method, plus 44",
    62             "org_apidesign_vm4brwsr_Instance_virtualBytesI",
    63             Double.valueOf(55)
    64         );
    65     }
    66     @Test public void verifyInterfaceMethods() throws Exception {
    67         assertExec(
    68             "Retruns default value",
    69             "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
    70             Double.valueOf(31)
    71         );
    72     }
    73 
    74     @Test public void isNull() throws Exception {
    75         assertExec(
    76             "Yes, we are instance",
    77             "org_apidesign_vm4brwsr_Instance_isNullZ",
    78             Double.valueOf(0.0)
    79         );
    80     }
    81 
    82     @Test public void isInstanceOf() throws Exception {
    83         assertExec(
    84             "Yes, we are instance",
    85             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    86             Double.valueOf(1.0), true
    87         );
    88     }
    89 
    90     @Test public void notInstanceOf() throws Exception {
    91         assertExec(
    92             "No, we are not an instance",
    93             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    94             Double.valueOf(0.0), false
    95         );
    96     }
    97     
    98     @Test public void verifyCastToClass() throws Exception {
    99         assertExec(
   100             "Five signals all is good",
   101             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
   102             Double.valueOf(5.0), false
   103         );
   104     }
   105     @Test public void verifyCastToInterface() throws Exception {
   106         assertExec(
   107             "Five signals all is good",
   108             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
   109             Double.valueOf(5.0), true
   110         );
   111     }
   112     
   113     protected String startCompilationWith() {
   114         return "org/apidesign/vm4brwsr/Instance";
   115     }
   116     
   117     private static CharSequence codeSeq;
   118     private static Invocable code;
   119     
   120     @BeforeClass
   121     public void compileTheCode() throws Exception {
   122         if (codeSeq == null) {
   123             StringBuilder sb = new StringBuilder();
   124             code = StaticMethodTest.compileClass(sb, startCompilationWith());
   125             codeSeq = sb;
   126         }
   127     }
   128     
   129     private void assertExec(
   130         String msg, String methodName, Object expRes, Object... args
   131     ) throws Exception {
   132 
   133         Object ret = null;
   134         try {
   135             ret = code.invokeFunction(methodName, args);
   136         } catch (ScriptException ex) {
   137             fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
   138         } catch (NoSuchMethodException ex) {
   139             fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
   140         }
   141         if (ret == null && expRes == null) {
   142             return;
   143         }
   144         if (expRes.equals(ret)) {
   145             return;
   146         }
   147         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
   148         
   149     }
   150     
   151 }