rt/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 708 vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java@59d5596a9c6c
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 org.testng.annotations.Test;
    21 import org.testng.annotations.BeforeClass;
    22 
    23 /**
    24  *
    25  * @author Jaroslav Tulach <jtulach@netbeans.org>
    26  */
    27 public class InstanceTest {
    28     @Test public void verifyDefaultDoubleValue() throws Exception {
    29         assertExec(
    30             "Will be zero",
    31             Instance.class, "defaultDblValue__D",
    32             Double.valueOf(0)
    33         );
    34     }
    35     @Test public void verifyStaticMethodCall() throws Exception {
    36         assertExec(
    37             "Will be zero",
    38             InstanceSub.class, "recallDbl__D",
    39             Double.valueOf(0)
    40         );
    41     }
    42     @Test public void verifyAssignedByteValue() throws Exception {
    43         assertExec(
    44             "Will one thirty one",
    45             Instance.class, "assignedByteValue__B",
    46             Double.valueOf(31)
    47         );
    48     }
    49     @Test public void verifyMagicOne() throws Exception {
    50         assertExec(
    51             "Should be three and something",
    52             Instance.class, "magicOne__D",
    53             Double.valueOf(3.3)
    54         );
    55     }
    56     @Test public void verifyInstanceMethods() throws Exception {
    57         assertExec(
    58             "Should be eleven as we invoke overwritten method, plus 44",
    59             Instance.class, "virtualBytes__I",
    60             Double.valueOf(55)
    61         );
    62     }
    63     @Test public void verifyInterfaceMethods() throws Exception {
    64         assertExec(
    65             "Retruns default value",
    66             Instance.class, "interfaceBytes__F",
    67             Double.valueOf(31)
    68         );
    69     }
    70 
    71     @Test public void isNull() throws Exception {
    72         assertExec(
    73             "Yes, we are instance",
    74             Instance.class, "isNull__Z",
    75             Double.valueOf(0.0)
    76         );
    77     }
    78 
    79     @Test public void isInstanceOf() throws Exception {
    80         assertExec(
    81             "Yes, we are instance",
    82             Instance.class, "instanceOf__ZI",
    83             Double.valueOf(1.0), 2
    84         );
    85     }
    86 
    87     @Test public void notInstanceOf() throws Exception {
    88         assertExec(
    89             "No, we are not an instance",
    90             Instance.class, "instanceOf__ZI",
    91             Double.valueOf(0.0), 1
    92         );
    93     }
    94     @Test public void nullInstanceOf() throws Exception {
    95         assertExec(
    96             "No, null is not an instance",
    97             Instance.class, "instanceOf__ZI",
    98             Double.valueOf(0.0), 0
    99         );
   100     }
   101     
   102     @Test public void verifyCastToClass() throws Exception {
   103         assertExec(
   104             "Five signals all is good",
   105             Instance.class, "castsWork__IZ",
   106             Double.valueOf(5.0), false
   107         );
   108     }
   109     @Test public void verifyCastToInterface() throws Exception {
   110         assertExec(
   111             "Five signals all is good",
   112             Instance.class, "castsWork__IZ",
   113             Double.valueOf(5.0), true
   114         );
   115     }
   116     
   117     @Test public void sharedConstructor() throws Exception {
   118         assertExec(
   119             "Constructor of first and 2nd instance should be the same",
   120             Instance.class, "sharedConstructor__Z",
   121             Double.valueOf(1.0)
   122         );
   123     }
   124 
   125     @Test public void differentConstructor() throws Exception {
   126         assertExec(
   127             "Constructor of X and Y should be the different",
   128             Instance.class, "differentConstructor__Z",
   129             Double.valueOf(0)
   130         );
   131     }
   132 
   133     @Test public void jsObjectIsLikeJavaObject() throws Exception {
   134         assertExec(
   135             "JavaScript object is instance of Java Object",
   136             Instance.class, "iofObject__Z",
   137             Double.valueOf(1)
   138         );
   139     }
   140 
   141     @Test public void jsCallingConvention() throws Exception {
   142         assertExec(
   143             "Pointer to 'this' is passed automatically (and not as a first argument)",
   144             Instance.class, "jscall__I",
   145             Double.valueOf(31)
   146         );
   147     }
   148     
   149     protected String startCompilationWith() {
   150         return "org/apidesign/vm4brwsr/Instance";
   151     }
   152     
   153     private static TestVM code;
   154     
   155     @BeforeClass
   156     public void compileTheCode() throws Exception {
   157         code = TestVM.compileClass(startCompilationWith());
   158     }
   159     
   160     private void assertExec(
   161         String msg, Class clazz, String method, Object expRes, Object... args
   162     ) throws Exception {
   163         code.assertExec(msg, clazz, method, expRes, args);
   164     }
   165     
   166 }