vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Sep 2012 12:26:29 +0200
changeset 30 7efb52f76270
parent 24 a82e89aae050
child 98 9fb17a3cbbb6
permissions -rw-r--r--
Support for casts
     1 /**
     2  * Java 4 Browser Bytecode Translator
     3  * Copyright (C) 2012-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 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class InstanceTest {
    30     @Test public void verifyDefaultDoubleValue() throws Exception {
    31         assertExec(
    32             "Will be zero",
    33             "org_apidesign_vm4brwsr_Instance_defaultDblValueD",
    34             Double.valueOf(0)
    35         );
    36     }
    37     @Test public void verifyAssignedByteValue() throws Exception {
    38         assertExec(
    39             "Will one thirty one",
    40             "org_apidesign_vm4brwsr_Instance_assignedByteValueB",
    41             Double.valueOf(31)
    42         );
    43     }
    44     @Test public void verifyMagicOne() throws Exception {
    45         assertExec(
    46             "Should be three and something",
    47             "org_apidesign_vm4brwsr_Instance_magicOneD",
    48             Double.valueOf(3.3)
    49         );
    50     }
    51     @Test public void verifyInstanceMethods() throws Exception {
    52         assertExec(
    53             "Should be eleven as we invoke overwritten method, plus 44",
    54             "org_apidesign_vm4brwsr_Instance_virtualBytesI",
    55             Double.valueOf(55)
    56         );
    57     }
    58     @Test public void verifyInterfaceMethods() throws Exception {
    59         assertExec(
    60             "Retruns default value",
    61             "org_apidesign_vm4brwsr_Instance_interfaceBytesF",
    62             Double.valueOf(31)
    63         );
    64     }
    65 
    66     @Test public void isNull() throws Exception {
    67         assertExec(
    68             "Yes, we are instance",
    69             "org_apidesign_vm4brwsr_Instance_isNullZ",
    70             Double.valueOf(0.0)
    71         );
    72     }
    73 
    74     @Test public void isInstanceOf() throws Exception {
    75         assertExec(
    76             "Yes, we are instance",
    77             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    78             Double.valueOf(1.0), true
    79         );
    80     }
    81 
    82     @Test public void notInstanceOf() throws Exception {
    83         assertExec(
    84             "No, we are not an instance",
    85             "org_apidesign_vm4brwsr_Instance_instanceOfZZ",
    86             Double.valueOf(0.0), false
    87         );
    88     }
    89     
    90     @Test public void verifyCastToClass() throws Exception {
    91         assertExec(
    92             "Five signals all is good",
    93             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
    94             Double.valueOf(5.0), false
    95         );
    96     }
    97     @Test public void verifyCastToInterface() throws Exception {
    98         assertExec(
    99             "Five signals all is good",
   100             "org_apidesign_vm4brwsr_Instance_castsWorkIZ",
   101             Double.valueOf(5.0), true
   102         );
   103     }
   104     
   105     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   106         StringBuilder sb = new StringBuilder();
   107         Invocable i = StaticMethodTest.compileClass(sb, 
   108             "org/apidesign/vm4brwsr/Instance"
   109         );
   110         
   111         Object ret = null;
   112         try {
   113             ret = i.invokeFunction(methodName, args);
   114         } catch (ScriptException ex) {
   115             fail("Execution failed in " + sb, ex);
   116         } catch (NoSuchMethodException ex) {
   117             fail("Cannot find method in " + sb, ex);
   118         }
   119         if (ret == null && expRes == null) {
   120             return;
   121         }
   122         if (expRes.equals(ret)) {
   123             return;
   124         }
   125         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   126         
   127     }
   128     
   129 }