rt/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 22 Jun 2014 00:09:56 +0200
branchdefprop
changeset 1632 8ac637d7d62a
parent 789 bb7506513353
child 1633 a34e2191b6be
permissions -rw-r--r--
Using Object.defineProperty to hide attributes from list of enumerable properties
     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.AfterClass;
    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 noInstOfExposed() throws Exception {
    51         assertExec(
    52             "No instOf properties found",
    53             Instance.class, "noInstOfExposed__I",
    54             Double.valueOf(0)
    55         );
    56     }
    57     @Test public void verifyMagicOne() throws Exception {
    58         assertExec(
    59             "Should be three and something",
    60             Instance.class, "magicOne__D",
    61             Double.valueOf(3.3)
    62         );
    63     }
    64     @Test public void verifyInstanceMethods() throws Exception {
    65         assertExec(
    66             "Should be eleven as we invoke overwritten method, plus 44",
    67             Instance.class, "virtualBytes__I",
    68             Double.valueOf(55)
    69         );
    70     }
    71     @Test public void verifyInterfaceMethods() throws Exception {
    72         assertExec(
    73             "Retruns default value",
    74             Instance.class, "interfaceBytes__F",
    75             Double.valueOf(31)
    76         );
    77     }
    78 
    79     @Test public void isNull() throws Exception {
    80         assertExec(
    81             "Yes, we are instance",
    82             Instance.class, "isNull__Z",
    83             Double.valueOf(0.0)
    84         );
    85     }
    86 
    87     @Test public void isInstanceOf() throws Exception {
    88         assertExec(
    89             "Yes, we are instance",
    90             Instance.class, "instanceOf__ZI",
    91             Double.valueOf(1.0), 2
    92         );
    93     }
    94 
    95     @Test public void notInstanceOf() throws Exception {
    96         assertExec(
    97             "No, we are not an instance",
    98             Instance.class, "instanceOf__ZI",
    99             Double.valueOf(0.0), 1
   100         );
   101     }
   102     @Test public void nullInstanceOf() throws Exception {
   103         assertExec(
   104             "No, null is not an instance",
   105             Instance.class, "instanceOf__ZI",
   106             Double.valueOf(0.0), 0
   107         );
   108     }
   109     
   110     @Test public void verifyCastToClass() throws Exception {
   111         assertExec(
   112             "Five signals all is good",
   113             Instance.class, "castsWork__IZ",
   114             Double.valueOf(5.0), false
   115         );
   116     }
   117     @Test public void verifyCastToInterface() throws Exception {
   118         assertExec(
   119             "Five signals all is good",
   120             Instance.class, "castsWork__IZ",
   121             Double.valueOf(5.0), true
   122         );
   123     }
   124     
   125     @Test public void sharedConstructor() throws Exception {
   126         assertExec(
   127             "Constructor of first and 2nd instance should be the same",
   128             Instance.class, "sharedConstructor__Z",
   129             Double.valueOf(1.0)
   130         );
   131     }
   132 
   133     @Test public void differentConstructor() throws Exception {
   134         assertExec(
   135             "Constructor of X and Y should be the different",
   136             Instance.class, "differentConstructor__Z",
   137             Double.valueOf(0)
   138         );
   139     }
   140 
   141     @Test public void jsObjectIsLikeJavaObject() throws Exception {
   142         assertExec(
   143             "JavaScript object is instance of Java Object",
   144             Instance.class, "iofObject__Z",
   145             Double.valueOf(1)
   146         );
   147     }
   148 
   149     @Test public void jsCallingConvention() throws Exception {
   150         assertExec(
   151             "Pointer to 'this' is passed automatically (and not as a first argument)",
   152             Instance.class, "jscall__I",
   153             Double.valueOf(31)
   154         );
   155     }
   156     
   157     protected String startCompilationWith() {
   158         return "org/apidesign/vm4brwsr/Instance";
   159     }
   160     
   161     private static TestVM code;
   162     
   163     @BeforeClass
   164     public void compileTheCode() throws Exception {
   165         code = TestVM.compileClass(startCompilationWith());
   166     }
   167     @AfterClass
   168     public static void releaseTheCode() {
   169         code = null;
   170     }
   171     
   172     private void assertExec(
   173         String msg, Class clazz, String method, Object expRes, Object... args
   174     ) throws Exception {
   175         code.assertExec(msg, clazz, method, expRes, args);
   176     }
   177     
   178 }