vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
branchlazy
changeset 203 c6a0b5b64133
parent 187 391a5d25c0e1
child 205 0256fac49ea5
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Sun Nov 18 19:54:40 2012 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Sun Nov 25 23:14:58 2012 +0100
     1.3 @@ -17,11 +17,6 @@
     1.4   */
     1.5  package org.apidesign.vm4brwsr;
     1.6  
     1.7 -import java.io.ByteArrayInputStream;
     1.8 -import java.io.ByteArrayOutputStream;
     1.9 -import java.io.DataInputStream;
    1.10 -import java.io.DataOutputStream;
    1.11 -import java.io.IOException;
    1.12  import javax.script.Invocable;
    1.13  import javax.script.ScriptException;
    1.14  import static org.testng.Assert.*;
    1.15 @@ -34,19 +29,19 @@
    1.16   */
    1.17  public class NumberTest {
    1.18      @Test public void integerFromString() throws Exception {
    1.19 -        assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
    1.20 +        assertExec("Can convert string to integer", Integer.class, "parseIntILjava_lang_String",
    1.21              Double.valueOf(333), "333"
    1.22          );
    1.23      }
    1.24  
    1.25      @Test public void doubleFromString() throws Exception {
    1.26 -        assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
    1.27 +        assertExec("Can convert string to double", Double.class, "parseDoubleDLjava_lang_String",
    1.28              Double.valueOf(33.3), "33.3"
    1.29          );
    1.30      }
    1.31  
    1.32      @Test public void autoboxDouble() throws Exception {
    1.33 -        assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
    1.34 +        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToStringLjava_lang_String",
    1.35              "3.3"
    1.36          );
    1.37      }
    1.38 @@ -56,7 +51,7 @@
    1.39      }
    1.40  
    1.41      @Test public void jslog1000() throws Exception {
    1.42 -        assertExec("log_10(1000) == 3", "java_lang_Math_log10DD", 
    1.43 +        assertExec("log_10(1000) == 3", Math.class, "log10DD", 
    1.44              Double.valueOf(3.0), 1000.0
    1.45          );
    1.46      }
    1.47 @@ -65,20 +60,20 @@
    1.48          assertEquals(3, Numbers.rem(303, 10));
    1.49      }
    1.50      @Test public void jsRem() throws Exception {
    1.51 -        assertExec("Should be three", "org_apidesign_vm4brwsr_Numbers_remIII", 
    1.52 +        assertExec("Should be three", Numbers.class, "remIII", 
    1.53              Double.valueOf(3.0), 303, 10
    1.54          );
    1.55      }
    1.56      
    1.57      @Test public void deserializeInt() throws Exception {
    1.58          int exp = Numbers.deserInt();
    1.59 -        assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserIntI", 
    1.60 +        assertExec("Should be the same", Numbers.class, "deserIntI", 
    1.61              Double.valueOf(exp)
    1.62          );
    1.63      }
    1.64  
    1.65      @Test public void deserializeSimpleLong() throws Exception {
    1.66 -        assertExec("Should be 3454", "org_apidesign_vm4brwsr_Numbers_deserLongJAB", 
    1.67 +        assertExec("Should be 3454", Numbers.class, "deserLongJAB", 
    1.68              Double.valueOf(3454), 
    1.69              new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    1.70          );
    1.71 @@ -102,7 +97,7 @@
    1.72      
    1.73      @Test public void deserializeFloatInJS() throws Exception {
    1.74          float f = 54324.32423f;
    1.75 -        assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF", 
    1.76 +        assertExec("Should be the same", Numbers.class, "deserFloatF", 
    1.77              Double.valueOf(f)
    1.78          );
    1.79      }
    1.80 @@ -115,7 +110,7 @@
    1.81      
    1.82      @Test public void deserializeDoubleInJS() throws Exception {
    1.83          double f = 3.0;
    1.84 -        assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserDoubleD", f);
    1.85 +        assertExec("Should be the same", Numbers.class, "deserDoubleD", f);
    1.86      }
    1.87      /*
    1.88      @Test public void serDouble() throws IOException {
    1.89 @@ -136,7 +131,7 @@
    1.90      @Test public void fiveInStringJS() throws Exception {
    1.91          String s = Numbers.intToString();
    1.92          assertExec("Should be the same: " + s, 
    1.93 -            "org_apidesign_vm4brwsr_Numbers_intToStringLjava_lang_String", 
    1.94 +            Numbers.class, "intToStringLjava_lang_String", 
    1.95              s
    1.96          );
    1.97      }
    1.98 @@ -144,7 +139,7 @@
    1.99      @Test public void sevenInStringJS() throws Exception {
   1.100          String s = Numbers.floatToString();
   1.101          assertExec("Should be the same: " + s, 
   1.102 -            "org_apidesign_vm4brwsr_Numbers_floatToStringLjava_lang_String", 
   1.103 +            Numbers.class, "floatToStringLjava_lang_String", 
   1.104              s
   1.105          );
   1.106      }
   1.107 @@ -162,15 +157,16 @@
   1.108      }
   1.109  
   1.110      private static void assertExec(
   1.111 -        String msg, String methodName, Object expRes, Object... args) throws Exception {
   1.112 +        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception {
   1.113  
   1.114          Object ret = null;
   1.115          try {
   1.116 -            ret = code.invokeFunction(methodName, args);
   1.117 +            ret = code.invokeFunction(clazz.getName().replace('.', '_') + "_proto");
   1.118 +            ret = code.invokeMethod(ret, method, args);
   1.119          } catch (ScriptException ex) {
   1.120 -            fail("Execution failed in\n" + codeSeq, ex);
   1.121 +            fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
   1.122          } catch (NoSuchMethodException ex) {
   1.123 -            fail("Cannot find method in\n" + codeSeq, ex);
   1.124 +            fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
   1.125          }
   1.126          if (ret == null && expRes == null) {
   1.127              return;
   1.128 @@ -181,10 +177,10 @@
   1.129          if (expRes instanceof Double && ret instanceof Double) {
   1.130              double expD = ((Double)expRes).doubleValue();
   1.131              double retD = ((Double)ret).doubleValue();
   1.132 -            assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
   1.133 +            assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
   1.134              return;
   1.135          }
   1.136 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   1.137 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
   1.138      }
   1.139      
   1.140  }