vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 18:04:11 +0200
changeset 114 a0505844750a
child 132 2377bb30dd1b
permissions -rw-r--r--
Same basic operations with Number subclasses now work
jaroslav@114
     1
/**
jaroslav@114
     2
 * Back 2 Browser Bytecode Translator
jaroslav@114
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@114
     4
 *
jaroslav@114
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@114
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@114
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@114
     8
 *
jaroslav@114
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@114
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@114
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@114
    12
 * GNU General Public License for more details.
jaroslav@114
    13
 *
jaroslav@114
    14
 * You should have received a copy of the GNU General Public License
jaroslav@114
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@114
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@114
    17
 */
jaroslav@114
    18
package org.apidesign.vm4brwsr;
jaroslav@114
    19
jaroslav@114
    20
import javax.script.Invocable;
jaroslav@114
    21
import javax.script.ScriptException;
jaroslav@114
    22
import org.testng.annotations.BeforeClass;
jaroslav@114
    23
import static org.testng.Assert.*;
jaroslav@114
    24
import org.testng.annotations.Test;
jaroslav@114
    25
jaroslav@114
    26
/**
jaroslav@114
    27
 *
jaroslav@114
    28
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@114
    29
 */
jaroslav@114
    30
public class NumberTest {
jaroslav@114
    31
    @Test public void integerFromString() throws Exception {
jaroslav@114
    32
        assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
jaroslav@114
    33
            Double.valueOf(333), "333"
jaroslav@114
    34
        );
jaroslav@114
    35
    }
jaroslav@114
    36
jaroslav@114
    37
    @Test public void doubleFromString() throws Exception {
jaroslav@114
    38
        assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
jaroslav@114
    39
            Double.valueOf(33.3), "33.3"
jaroslav@114
    40
        );
jaroslav@114
    41
    }
jaroslav@114
    42
jaroslav@114
    43
    @Test public void autoboxDouble() throws Exception {
jaroslav@114
    44
        assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
jaroslav@114
    45
            "3.3"
jaroslav@114
    46
        );
jaroslav@114
    47
    }
jaroslav@114
    48
jaroslav@114
    49
    
jaroslav@114
    50
    private static CharSequence codeSeq;
jaroslav@114
    51
    private static Invocable code;
jaroslav@114
    52
jaroslav@114
    53
    @BeforeClass
jaroslav@114
    54
    public void compileTheCode() throws Exception {
jaroslav@114
    55
        if (codeSeq == null) {
jaroslav@114
    56
            StringBuilder sb = new StringBuilder();
jaroslav@114
    57
            code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
jaroslav@114
    58
            codeSeq = sb;
jaroslav@114
    59
        }
jaroslav@114
    60
    }
jaroslav@114
    61
jaroslav@114
    62
    private static void assertExec(
jaroslav@114
    63
        String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@114
    64
jaroslav@114
    65
        Object ret = null;
jaroslav@114
    66
        try {
jaroslav@114
    67
            ret = code.invokeFunction(methodName, args);
jaroslav@114
    68
        } catch (ScriptException ex) {
jaroslav@114
    69
            fail("Execution failed in\n" + codeSeq, ex);
jaroslav@114
    70
        } catch (NoSuchMethodException ex) {
jaroslav@114
    71
            fail("Cannot find method in\n" + codeSeq, ex);
jaroslav@114
    72
        }
jaroslav@114
    73
        if (ret == null && expRes == null) {
jaroslav@114
    74
            return;
jaroslav@114
    75
        }
jaroslav@114
    76
        if (expRes.equals(ret)) {
jaroslav@114
    77
            return;
jaroslav@114
    78
        }
jaroslav@114
    79
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
jaroslav@114
    80
    }
jaroslav@114
    81
    
jaroslav@114
    82
}