vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 01 Dec 2012 10:35:24 +0100
changeset 226 907a52ed10e3
parent 203 c6a0b5b64133
child 239 8ceee38f5840
permissions -rw-r--r--
Make sure the constructor property points to the real function that creates the 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.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, "defaultDblValueD",
    33             Double.valueOf(0)
    34         );
    35     }
    36     @Test public void verifyStaticMethodCall() throws Exception {
    37         assertExec(
    38             "Will be zero",
    39             InstanceSub.class, "recallDblD",
    40             Double.valueOf(0)
    41         );
    42     }
    43     @Test public void verifyAssignedByteValue() throws Exception {
    44         assertExec(
    45             "Will one thirty one",
    46             Instance.class, "assignedByteValueB",
    47             Double.valueOf(31)
    48         );
    49     }
    50     @Test public void verifyMagicOne() throws Exception {
    51         assertExec(
    52             "Should be three and something",
    53             Instance.class, "magicOneD",
    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, "virtualBytesI",
    61             Double.valueOf(55)
    62         );
    63     }
    64     @Test public void verifyInterfaceMethods() throws Exception {
    65         assertExec(
    66             "Retruns default value",
    67             Instance.class, "interfaceBytesF",
    68             Double.valueOf(31)
    69         );
    70     }
    71 
    72     @Test public void isNull() throws Exception {
    73         assertExec(
    74             "Yes, we are instance",
    75             Instance.class, "isNullZ",
    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, "instanceOfZZ",
    84             Double.valueOf(1.0), true
    85         );
    86     }
    87 
    88     @Test public void notInstanceOf() throws Exception {
    89         assertExec(
    90             "No, we are not an instance",
    91             Instance.class, "instanceOfZZ",
    92             Double.valueOf(0.0), false
    93         );
    94     }
    95     
    96     @Test public void verifyCastToClass() throws Exception {
    97         assertExec(
    98             "Five signals all is good",
    99             Instance.class, "castsWorkIZ",
   100             Double.valueOf(5.0), false
   101         );
   102     }
   103     @Test public void verifyCastToInterface() throws Exception {
   104         assertExec(
   105             "Five signals all is good",
   106             Instance.class, "castsWorkIZ",
   107             Double.valueOf(5.0), true
   108         );
   109     }
   110     
   111     @Test public void sharedConstructor() throws Exception {
   112         assertExec(
   113             "Constructor of first and 2nd instance should be the same",
   114             Instance.class, "sharedConstructorZ",
   115             Double.valueOf(1.0)
   116         );
   117     }
   118 
   119     @Test public void differentConstructor() throws Exception {
   120         assertExec(
   121             "Constructor of X and Y should be the different",
   122             Instance.class, "differentConstructorZ",
   123             Double.valueOf(0)
   124         );
   125     }
   126     
   127     protected String startCompilationWith() {
   128         return "org/apidesign/vm4brwsr/Instance";
   129     }
   130     
   131     private static CharSequence codeSeq;
   132     private static Invocable code;
   133     
   134     @BeforeClass
   135     public void compileTheCode() throws Exception {
   136         if (codeSeq == null) {
   137             StringBuilder sb = new StringBuilder();
   138             code = StaticMethodTest.compileClass(sb, startCompilationWith());
   139             codeSeq = sb;
   140         }
   141     }
   142     
   143     private void assertExec(
   144         String msg, Class clazz, String method, Object expRes, Object... args
   145     ) throws Exception {
   146         StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   147     }
   148     
   149 }