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