vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 11:55:56 +0200
changeset 104 1376481f15e7
parent 103 e8438996d406
child 106 346633cd13d6
permissions -rw-r--r--
Concatenation of strings works
jaroslav@21
     1
/*
jaroslav@21
     2
Java 4 Browser Bytecode Translator
jaroslav@21
     3
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@21
     4
jaroslav@21
     5
This program is free software: you can redistribute it and/or modify
jaroslav@21
     6
it under the terms of the GNU General Public License as published by
jaroslav@21
     7
the Free Software Foundation, version 2 of the License.
jaroslav@21
     8
jaroslav@21
     9
This program is distributed in the hope that it will be useful,
jaroslav@21
    10
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@21
    11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@21
    12
GNU General Public License for more details.
jaroslav@21
    13
jaroslav@21
    14
You should have received a copy of the GNU General Public License
jaroslav@21
    15
along with this program. Look for COPYING file in the top folder.
jaroslav@21
    16
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@21
    17
*/
jaroslav@22
    18
package org.apidesign.vm4brwsr;
jaroslav@21
    19
jaroslav@21
    20
import javax.script.Invocable;
jaroslav@21
    21
import javax.script.ScriptException;
jaroslav@21
    22
import org.testng.annotations.Test;
jaroslav@21
    23
import static org.testng.Assert.*;
jaroslav@103
    24
import org.testng.annotations.BeforeClass;
jaroslav@21
    25
jaroslav@21
    26
/**
jaroslav@21
    27
 *
jaroslav@21
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@21
    29
 */
jaroslav@21
    30
public class ArrayTest {
jaroslav@21
    31
    @Test public void verifySimpleIntOperation() throws Exception {
jaroslav@103
    32
            assertExec("CheckTheSum", "org_apidesign_vm4brwsr_Array_simpleI", 
jaroslav@21
    33
            Double.valueOf(15)
jaroslav@21
    34
        );
jaroslav@21
    35
    }
jaroslav@21
    36
    @Test public void verifyOperationsOnArrays() throws Exception {
jaroslav@22
    37
        assertExec("The sum is 105", "org_apidesign_vm4brwsr_Array_sumD", 
jaroslav@21
    38
            Double.valueOf(105)
jaroslav@21
    39
        );
jaroslav@21
    40
    }
jaroslav@21
    41
    
jaroslav@104
    42
    @Test public void doesCopyArrayWork() throws Exception {
jaroslav@104
    43
        assertExec("Returns 'a'", "org_apidesign_vm4brwsr_Array_copyArrayC", Double.valueOf('a'));
jaroslav@104
    44
    }
jaroslav@104
    45
    
jaroslav@103
    46
    private static CharSequence codeSeq;
jaroslav@103
    47
    private static Invocable code;
jaroslav@103
    48
    
jaroslav@103
    49
    @BeforeClass 
jaroslav@103
    50
    public void compileTheCode() throws Exception {
jaroslav@21
    51
        StringBuilder sb = new StringBuilder();
jaroslav@103
    52
        code = StaticMethodTest.compileClass(sb, 
jaroslav@22
    53
            "org/apidesign/vm4brwsr/Array"
jaroslav@21
    54
        );
jaroslav@103
    55
        codeSeq = sb;
jaroslav@103
    56
    }
jaroslav@103
    57
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@21
    58
        Object ret = null;
jaroslav@21
    59
        try {
jaroslav@103
    60
            ret = code.invokeFunction(methodName, args);
jaroslav@21
    61
        } catch (ScriptException ex) {
jaroslav@103
    62
            fail("Execution failed in\n" + codeSeq, ex);
jaroslav@21
    63
        } catch (NoSuchMethodException ex) {
jaroslav@103
    64
            fail("Cannot find method in\n" + codeSeq, ex);
jaroslav@21
    65
        }
jaroslav@21
    66
        if (ret == null && expRes == null) {
jaroslav@21
    67
            return;
jaroslav@21
    68
        }
jaroslav@21
    69
        if (expRes.equals(ret)) {
jaroslav@21
    70
            return;
jaroslav@21
    71
        }
jaroslav@103
    72
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
jaroslav@21
    73
    }
jaroslav@21
    74
}