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