vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
branchlazy
changeset 203 c6a0b5b64133
parent 201 f180d72cc7a4
child 205 0256fac49ea5
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Sun Nov 25 21:24:03 2012 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Sun Nov 25 23:14:58 2012 +0100
     1.3 @@ -36,7 +36,7 @@
     1.4      @Test public void threePlusFour() throws Exception {
     1.5          assertExec(
     1.6              "Should be seven", 
     1.7 -            "org_apidesign_vm4brwsr_StaticMethod_sumIII", 
     1.8 +            StaticMethod.class, "sumIII", 
     1.9              Double.valueOf(7), 
    1.10              3, 4
    1.11          );
    1.12 @@ -45,7 +45,7 @@
    1.13      @Test public void checkReallyInitializedValues() throws Exception {
    1.14          assertExec(
    1.15              "Return true", 
    1.16 -            "org_apidesign_vm4brwsr_StaticMethod_isNullZ", 
    1.17 +            StaticMethod.class, "isNullZ", 
    1.18              Double.valueOf(1)
    1.19          );
    1.20      }
    1.21 @@ -53,7 +53,7 @@
    1.22      @Test public void powerOfThree() throws Exception {
    1.23          assertExec(
    1.24              "Should be nine", 
    1.25 -            "org_apidesign_vm4brwsr_StaticMethod_powerFF", 
    1.26 +            StaticMethod.class, "powerFF", 
    1.27              Double.valueOf(9),
    1.28              3.0f
    1.29          );
    1.30 @@ -62,7 +62,7 @@
    1.31      @Test public void minusOne() throws Exception {
    1.32          assertExec(
    1.33              "Should be minus one", 
    1.34 -            "org_apidesign_vm4brwsr_StaticMethod_minusOneI", 
    1.35 +            StaticMethod.class, "minusOneI", 
    1.36              Double.valueOf(-1)
    1.37          );
    1.38      }
    1.39 @@ -70,7 +70,7 @@
    1.40      @Test public void doubleWithoutLong() throws Exception {
    1.41          assertExec(
    1.42              "Should be two",
    1.43 -            "org_apidesign_vm4brwsr_StaticMethod_minusDDJ", 
    1.44 +            StaticMethod.class, "minusDDJ", 
    1.45              Double.valueOf(2),
    1.46              3.0d, 1l
    1.47          );
    1.48 @@ -79,7 +79,7 @@
    1.49      @Test public void divAndRound() throws Exception {
    1.50          assertExec(
    1.51              "Should be rounded to one",
    1.52 -            "org_apidesign_vm4brwsr_StaticMethod_divIBD", 
    1.53 +            StaticMethod.class, "divIBD", 
    1.54              Double.valueOf(1),
    1.55              3, 3.75
    1.56          );
    1.57 @@ -87,7 +87,7 @@
    1.58      @Test public void mixedMethodFourParams() throws Exception {
    1.59          assertExec(
    1.60              "Should be two",
    1.61 -            "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD", 
    1.62 +            StaticMethod.class, "mixIIJBD", 
    1.63              Double.valueOf(20),
    1.64              2, 10l, 5, 2.0
    1.65          );
    1.66 @@ -95,7 +95,7 @@
    1.67      @Test public void factRec() throws Exception {
    1.68          assertExec(
    1.69              "Factorial of 5 is 120",
    1.70 -            "org_apidesign_vm4brwsr_StaticMethod_factRecJI", 
    1.71 +            StaticMethod.class, "factRecJI", 
    1.72              Double.valueOf(120),
    1.73              5
    1.74          );
    1.75 @@ -103,7 +103,7 @@
    1.76      @Test public void factIter() throws Exception {
    1.77          assertExec(
    1.78              "Factorial of 5 is 120",
    1.79 -            "org_apidesign_vm4brwsr_StaticMethod_factIterJI", 
    1.80 +            StaticMethod.class, "factIterJI", 
    1.81              Double.valueOf(120),
    1.82              5
    1.83          );
    1.84 @@ -112,7 +112,7 @@
    1.85      @Test public void xor() throws Exception {
    1.86          assertExec(
    1.87              "Xor is 4",
    1.88 -            "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
    1.89 +            StaticMethod.class, "xorJIJ",
    1.90              Double.valueOf(4),
    1.91              7,
    1.92              3
    1.93 @@ -122,7 +122,7 @@
    1.94      @Test public void or() throws Exception {
    1.95          assertExec(
    1.96              "Or will be 7",
    1.97 -            "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
    1.98 +            StaticMethod.class, "orOrAndJZII",
    1.99              Double.valueOf(7),
   1.100              true,
   1.101              4,
   1.102 @@ -132,14 +132,14 @@
   1.103      @Test public void nullCheck() throws Exception {
   1.104          assertExec(
   1.105              "Returns nothing",
   1.106 -            "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
   1.107 +            StaticMethod.class, "noneLjava_lang_ObjectII",
   1.108              null, 1, 3
   1.109          );
   1.110      }
   1.111      @Test public void and() throws Exception {
   1.112          assertExec(
   1.113              "And will be 3",
   1.114 -            "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   1.115 +            StaticMethod.class, "orOrAndJZII",
   1.116              Double.valueOf(3),
   1.117              false,
   1.118              7,
   1.119 @@ -149,7 +149,7 @@
   1.120      @Test public void inc4() throws Exception {
   1.121          assertExec(
   1.122              "It will be 4",
   1.123 -            "org_apidesign_vm4brwsr_StaticMethod_inc4I",
   1.124 +            StaticMethod.class, "inc4I",
   1.125              Double.valueOf(4)
   1.126          );
   1.127      }
   1.128 @@ -162,7 +162,7 @@
   1.129      @Test public void shiftLeftInJS() throws Exception {
   1.130          assertExec(
   1.131              "Setting 9th bit",
   1.132 -            "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
   1.133 +            StaticMethod.class, "shiftLeftIII",
   1.134              Double.valueOf(256),
   1.135              1, 8
   1.136          );
   1.137 @@ -176,7 +176,7 @@
   1.138      @Test public void shiftRightInJS() throws Exception {
   1.139          assertExec(
   1.140              "Get -1",
   1.141 -            "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   1.142 +            StaticMethod.class, "shiftArithmRightIIIZ",
   1.143              Double.valueOf(-1),
   1.144              -8, 3, true
   1.145          );
   1.146 @@ -189,7 +189,7 @@
   1.147      @Test public void unsignedShiftRightInJS() throws Exception {
   1.148          assertExec(
   1.149              "Get -1",
   1.150 -            "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
   1.151 +            StaticMethod.class, "shiftArithmRightIIIZ",
   1.152              Double.valueOf(1),
   1.153              8, 3, false
   1.154          );
   1.155 @@ -198,7 +198,7 @@
   1.156      @Test public void javaScriptBody() throws Exception {
   1.157          assertExec(
   1.158              "JavaScript string",
   1.159 -            "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
   1.160 +            StaticMethod.class, "i2sLjava_lang_StringII",
   1.161              "333",
   1.162              330, 3
   1.163          );
   1.164 @@ -207,7 +207,7 @@
   1.165      @Test public void switchJarda() throws Exception {
   1.166          assertExec(
   1.167              "The expected value",
   1.168 -            "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   1.169 +            StaticMethod.class, "swtchLjava_lang_StringI",
   1.170              "Jarda",
   1.171              0
   1.172          );
   1.173 @@ -216,7 +216,7 @@
   1.174      @Test public void switchDarda() throws Exception {
   1.175          assertExec(
   1.176              "The expected value",
   1.177 -            "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   1.178 +            StaticMethod.class, "swtchLjava_lang_StringI",
   1.179              "Darda",
   1.180              1
   1.181          );
   1.182 @@ -224,7 +224,7 @@
   1.183      @Test public void switchParda() throws Exception {
   1.184          assertExec(
   1.185              "The expected value",
   1.186 -            "org_apidesign_vm4brwsr_StaticMethod_swtch2Ljava_lang_StringI",
   1.187 +            StaticMethod.class, "swtch2Ljava_lang_StringI",
   1.188              "Parda",
   1.189              22
   1.190          );
   1.191 @@ -232,7 +232,7 @@
   1.192      @Test public void switchMarda() throws Exception {
   1.193          assertExec(
   1.194              "The expected value",
   1.195 -            "org_apidesign_vm4brwsr_StaticMethod_swtchLjava_lang_StringI",
   1.196 +            StaticMethod.class, "swtchLjava_lang_StringI",
   1.197              "Marda",
   1.198              -433
   1.199          );
   1.200 @@ -249,16 +249,25 @@
   1.201      }
   1.202      
   1.203      
   1.204 -    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   1.205 -        StringBuilder sb = new StringBuilder();
   1.206 -        
   1.207 +    private static void assertExec(
   1.208 +        String msg, Class clazz, String method, 
   1.209 +        Object expRes, Object... args
   1.210 +    ) throws Exception {
   1.211 +        assertExec(code, codeSeq, msg, clazz, method, expRes, args);
   1.212 +    }
   1.213 +    static void assertExec(
   1.214 +        Invocable toRun, CharSequence theCode,
   1.215 +        String msg, Class clazz, String method, 
   1.216 +        Object expRes, Object... args
   1.217 +    ) throws Exception {
   1.218          Object ret = null;
   1.219          try {
   1.220 -            ret = code.invokeFunction(methodName, args);
   1.221 +            ret = toRun.invokeFunction(clazz.getName().replace('.', '_') + "_proto");
   1.222 +            ret = toRun.invokeMethod(ret, method, args);
   1.223          } catch (ScriptException ex) {
   1.224 -            fail("Execution failed in\n" + codeSeq, ex);
   1.225 +            fail("Execution failed in\n" + dumpJS(theCode), ex);
   1.226          } catch (NoSuchMethodException ex) {
   1.227 -            fail("Cannot find method in\n" + codeSeq, ex);
   1.228 +            fail("Cannot find method in\n" + dumpJS(theCode), ex);
   1.229          }
   1.230          if (ret == null && expRes == null) {
   1.231              return;
   1.232 @@ -266,7 +275,7 @@
   1.233          if (expRes != null && expRes.equals(ret)) {
   1.234              return;
   1.235          }
   1.236 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   1.237 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
   1.238          
   1.239      }
   1.240  
   1.241 @@ -293,7 +302,7 @@
   1.242              if (sb.length() > 2000) {
   1.243                  sb = dumpJS(sb);
   1.244              }
   1.245 -            fail("Could not compile:\n" + sb, ex);
   1.246 +            fail("Could not evaluate:\n" + sb, ex);
   1.247              return null;
   1.248          }
   1.249      }