vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
changeset 114 a0505844750a
child 132 2377bb30dd1b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Tue Oct 16 18:04:11 2012 +0200
     1.3 @@ -0,0 +1,82 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import javax.script.Invocable;
    1.24 +import javax.script.ScriptException;
    1.25 +import org.testng.annotations.BeforeClass;
    1.26 +import static org.testng.Assert.*;
    1.27 +import org.testng.annotations.Test;
    1.28 +
    1.29 +/**
    1.30 + *
    1.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.32 + */
    1.33 +public class NumberTest {
    1.34 +    @Test public void integerFromString() throws Exception {
    1.35 +        assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
    1.36 +            Double.valueOf(333), "333"
    1.37 +        );
    1.38 +    }
    1.39 +
    1.40 +    @Test public void doubleFromString() throws Exception {
    1.41 +        assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
    1.42 +            Double.valueOf(33.3), "33.3"
    1.43 +        );
    1.44 +    }
    1.45 +
    1.46 +    @Test public void autoboxDouble() throws Exception {
    1.47 +        assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
    1.48 +            "3.3"
    1.49 +        );
    1.50 +    }
    1.51 +
    1.52 +    
    1.53 +    private static CharSequence codeSeq;
    1.54 +    private static Invocable code;
    1.55 +
    1.56 +    @BeforeClass
    1.57 +    public void compileTheCode() throws Exception {
    1.58 +        if (codeSeq == null) {
    1.59 +            StringBuilder sb = new StringBuilder();
    1.60 +            code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
    1.61 +            codeSeq = sb;
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +    private static void assertExec(
    1.66 +        String msg, String methodName, Object expRes, Object... args) throws Exception {
    1.67 +
    1.68 +        Object ret = null;
    1.69 +        try {
    1.70 +            ret = code.invokeFunction(methodName, args);
    1.71 +        } catch (ScriptException ex) {
    1.72 +            fail("Execution failed in\n" + codeSeq, ex);
    1.73 +        } catch (NoSuchMethodException ex) {
    1.74 +            fail("Cannot find method in\n" + codeSeq, ex);
    1.75 +        }
    1.76 +        if (ret == null && expRes == null) {
    1.77 +            return;
    1.78 +        }
    1.79 +        if (expRes.equals(ret)) {
    1.80 +            return;
    1.81 +        }
    1.82 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    1.83 +    }
    1.84 +    
    1.85 +}