diff -r 000000000000 -r a0505844750a vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Tue Oct 16 18:04:11 2012 +0200 @@ -0,0 +1,82 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import javax.script.Invocable; +import javax.script.ScriptException; +import org.testng.annotations.BeforeClass; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class NumberTest { + @Test public void integerFromString() throws Exception { + assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String", + Double.valueOf(333), "333" + ); + } + + @Test public void doubleFromString() throws Exception { + assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String", + Double.valueOf(33.3), "33.3" + ); + } + + @Test public void autoboxDouble() throws Exception { + assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String", + "3.3" + ); + } + + + private static CharSequence codeSeq; + private static Invocable code; + + @BeforeClass + public void compileTheCode() throws Exception { + if (codeSeq == null) { + StringBuilder sb = new StringBuilder(); + code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers"); + codeSeq = sb; + } + } + + private static void assertExec( + String msg, String methodName, Object expRes, Object... args) throws Exception { + + Object ret = null; + try { + ret = code.invokeFunction(methodName, args); + } catch (ScriptException ex) { + fail("Execution failed in\n" + codeSeq, ex); + } catch (NoSuchMethodException ex) { + fail("Cannot find method in\n" + codeSeq, ex); + } + if (ret == null && expRes == null) { + return; + } + if (expRes.equals(ret)) { + return; + } + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq); + } + +}