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
jaroslav@24
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@24
     4
 *
jaroslav@24
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@24
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@24
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@24
     8
 *
jaroslav@24
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@24
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@24
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@24
    12
 * GNU General Public License for more details.
jaroslav@24
    13
 *
jaroslav@24
    14
 * You should have received a copy of the GNU General Public License
jaroslav@24
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@24
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@8
    17
 */
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@8
    19
jaroslav@8
    20
import javax.script.Invocable;
jaroslav@8
    21
import org.testng.annotations.Test;
jaroslav@103
    22
import org.testng.annotations.BeforeClass;
jaroslav@8
    23
jaroslav@8
    24
/**
jaroslav@8
    25
 *
jaroslav@8
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@8
    27
 */
jaroslav@8
    28
public class InstanceTest {
jaroslav@10
    29
    @Test public void verifyDefaultDoubleValue() throws Exception {
jaroslav@10
    30
        assertExec(
jaroslav@10
    31
            "Will be zero",
jaroslav@203
    32
            Instance.class, "defaultDblValueD",
jaroslav@10
    33
            Double.valueOf(0)
jaroslav@10
    34
        );
jaroslav@10
    35
    }
jaroslav@102
    36
    @Test public void verifyStaticMethodCall() throws Exception {
jaroslav@102
    37
        assertExec(
jaroslav@102
    38
            "Will be zero",
jaroslav@203
    39
            InstanceSub.class, "recallDblD",
jaroslav@102
    40
            Double.valueOf(0)
jaroslav@102
    41
        );
jaroslav@102
    42
    }
jaroslav@10
    43
    @Test public void verifyAssignedByteValue() throws Exception {
jaroslav@10
    44
        assertExec(
jaroslav@10
    45
            "Will one thirty one",
jaroslav@203
    46
            Instance.class, "assignedByteValueB",
jaroslav@10
    47
            Double.valueOf(31)
jaroslav@10
    48
        );
jaroslav@10
    49
    }
jaroslav@8
    50
    @Test public void verifyMagicOne() throws Exception {
jaroslav@8
    51
        assertExec(
jaroslav@10
    52
            "Should be three and something",
jaroslav@203
    53
            Instance.class, "magicOneD",
jaroslav@8
    54
            Double.valueOf(3.3)
jaroslav@8
    55
        );
jaroslav@8
    56
    }
jaroslav@12
    57
    @Test public void verifyInstanceMethods() throws Exception {
jaroslav@12
    58
        assertExec(
jaroslav@14
    59
            "Should be eleven as we invoke overwritten method, plus 44",
jaroslav@203
    60
            Instance.class, "virtualBytesI",
jaroslav@14
    61
            Double.valueOf(55)
jaroslav@12
    62
        );
jaroslav@12
    63
    }
jaroslav@15
    64
    @Test public void verifyInterfaceMethods() throws Exception {
jaroslav@15
    65
        assertExec(
jaroslav@15
    66
            "Retruns default value",
jaroslav@203
    67
            Instance.class, "interfaceBytesF",
jaroslav@15
    68
            Double.valueOf(31)
jaroslav@15
    69
        );
jaroslav@15
    70
    }
jaroslav@16
    71
jaroslav@16
    72
    @Test public void isNull() throws Exception {
jaroslav@16
    73
        assertExec(
jaroslav@16
    74
            "Yes, we are instance",
jaroslav@203
    75
            Instance.class, "isNullZ",
jaroslav@16
    76
            Double.valueOf(0.0)
jaroslav@16
    77
        );
jaroslav@16
    78
    }
jaroslav@17
    79
jaroslav@16
    80
    @Test public void isInstanceOf() throws Exception {
jaroslav@16
    81
        assertExec(
jaroslav@16
    82
            "Yes, we are instance",
jaroslav@203
    83
            Instance.class, "instanceOfZZ",
jaroslav@16
    84
            Double.valueOf(1.0), true
jaroslav@16
    85
        );
jaroslav@16
    86
    }
jaroslav@16
    87
jaroslav@16
    88
    @Test public void notInstanceOf() throws Exception {
jaroslav@16
    89
        assertExec(
jaroslav@16
    90
            "No, we are not an instance",
jaroslav@203
    91
            Instance.class, "instanceOfZZ",
jaroslav@16
    92
            Double.valueOf(0.0), false
jaroslav@16
    93
        );
jaroslav@16
    94
    }
jaroslav@17
    95
    
jaroslav@30
    96
    @Test public void verifyCastToClass() throws Exception {
jaroslav@30
    97
        assertExec(
jaroslav@30
    98
            "Five signals all is good",
jaroslav@203
    99
            Instance.class, "castsWorkIZ",
jaroslav@30
   100
            Double.valueOf(5.0), false
jaroslav@30
   101
        );
jaroslav@30
   102
    }
jaroslav@30
   103
    @Test public void verifyCastToInterface() throws Exception {
jaroslav@30
   104
        assertExec(
jaroslav@30
   105
            "Five signals all is good",
jaroslav@203
   106
            Instance.class, "castsWorkIZ",
jaroslav@30
   107
            Double.valueOf(5.0), true
jaroslav@30
   108
        );
jaroslav@30
   109
    }
jaroslav@30
   110
    
jaroslav@226
   111
    @Test public void sharedConstructor() throws Exception {
jaroslav@226
   112
        assertExec(
jaroslav@226
   113
            "Constructor of first and 2nd instance should be the same",
jaroslav@226
   114
            Instance.class, "sharedConstructorZ",
jaroslav@226
   115
            Double.valueOf(1.0)
jaroslav@226
   116
        );
jaroslav@226
   117
    }
jaroslav@226
   118
jaroslav@226
   119
    @Test public void differentConstructor() throws Exception {
jaroslav@226
   120
        assertExec(
jaroslav@226
   121
            "Constructor of X and Y should be the different",
jaroslav@226
   122
            Instance.class, "differentConstructorZ",
jaroslav@226
   123
            Double.valueOf(0)
jaroslav@226
   124
        );
jaroslav@226
   125
    }
jaroslav@226
   126
    
jaroslav@98
   127
    protected String startCompilationWith() {
jaroslav@98
   128
        return "org/apidesign/vm4brwsr/Instance";
jaroslav@98
   129
    }
jaroslav@98
   130
    
jaroslav@103
   131
    private static CharSequence codeSeq;
jaroslav@103
   132
    private static Invocable code;
jaroslav@103
   133
    
jaroslav@113
   134
    @BeforeClass
jaroslav@103
   135
    public void compileTheCode() throws Exception {
jaroslav@103
   136
        if (codeSeq == null) {
jaroslav@103
   137
            StringBuilder sb = new StringBuilder();
jaroslav@103
   138
            code = StaticMethodTest.compileClass(sb, startCompilationWith());
jaroslav@103
   139
            codeSeq = sb;
jaroslav@103
   140
        }
jaroslav@103
   141
    }
jaroslav@103
   142
    
jaroslav@98
   143
    private void assertExec(
jaroslav@203
   144
        String msg, Class clazz, String method, Object expRes, Object... args
jaroslav@98
   145
    ) throws Exception {
jaroslav@203
   146
        StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
jaroslav@8
   147
    }
jaroslav@8
   148
    
jaroslav@8
   149
}