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