vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 25 Sep 2012 12:11:03 +0200
changeset 29 dcb98731b000
parent 22 b9318fe303cd
child 46 b07c7c256771
permissions -rw-r--r--
Main method to compile some classes
     1 /*
     2 Java 4 Browser Bytecode Translator
     3 Copyright (C) 2012-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 java.io.IOException;
    21 import javax.script.Invocable;
    22 import javax.script.ScriptEngine;
    23 import javax.script.ScriptEngineManager;
    24 import javax.script.ScriptException;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.Test;
    27 
    28 /** Checks the basic behavior of the translator.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class StaticMethodTest {
    33     @Test public void threePlusFour() throws Exception {
    34         assertExec(
    35             "Should be seven", 
    36             "org_apidesign_vm4brwsr_StaticMethod_sumIII", 
    37             Double.valueOf(7), 
    38             3, 4
    39         );
    40     }
    41 
    42     @Test public void powerOfThree() throws Exception {
    43         assertExec(
    44             "Should be nine", 
    45             "org_apidesign_vm4brwsr_StaticMethod_powerFF", 
    46             Double.valueOf(9),
    47             3.0f
    48         );
    49     }
    50 
    51     @Test public void doubleWithoutLong() throws Exception {
    52         assertExec(
    53             "Should be two",
    54             "org_apidesign_vm4brwsr_StaticMethod_minusDDJ", 
    55             Double.valueOf(2),
    56             3.0d, 1l
    57         );
    58     }
    59 
    60     @Test public void divAndRound() throws Exception {
    61         assertExec(
    62             "Should be rounded to one",
    63             "org_apidesign_vm4brwsr_StaticMethod_divIBD", 
    64             Double.valueOf(1),
    65             3, 3.75
    66         );
    67     }
    68     @Test public void mixedMethodFourParams() throws Exception {
    69         assertExec(
    70             "Should be two",
    71             "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD", 
    72             Double.valueOf(20),
    73             2, 10l, 5, 2.0
    74         );
    75     }
    76     @Test public void factRec() throws Exception {
    77         assertExec(
    78             "Factorial of 5 is 120",
    79             "org_apidesign_vm4brwsr_StaticMethod_factRecJI", 
    80             Double.valueOf(120),
    81             5
    82         );
    83     }
    84     @Test public void factIter() throws Exception {
    85         assertExec(
    86             "Factorial of 5 is 120",
    87             "org_apidesign_vm4brwsr_StaticMethod_factIterJI", 
    88             Double.valueOf(120),
    89             5
    90         );
    91     }
    92     
    93     @Test public void xor() throws Exception {
    94         assertExec(
    95             "Xor is 4",
    96             "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
    97             Double.valueOf(4),
    98             7,
    99             3
   100         );
   101     }
   102     
   103     @Test public void or() throws Exception {
   104         assertExec(
   105             "Or will be 7",
   106             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   107             Double.valueOf(7),
   108             true,
   109             4,
   110             3
   111         );
   112     }
   113     @Test public void and() throws Exception {
   114         assertExec(
   115             "And will be 3",
   116             "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
   117             Double.valueOf(3),
   118             false,
   119             7,
   120             3
   121         );
   122     }
   123     @Test public void inc4() throws Exception {
   124         assertExec(
   125             "It will be 4",
   126             "org_apidesign_vm4brwsr_StaticMethod_inc4I",
   127             Double.valueOf(4)
   128         );
   129     }
   130     
   131     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
   132         StringBuilder sb = new StringBuilder();
   133         Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   134         
   135         Object ret = null;
   136         try {
   137             ret = i.invokeFunction(methodName, args);
   138         } catch (ScriptException ex) {
   139             fail("Execution failed in " + sb, ex);
   140         } catch (NoSuchMethodException ex) {
   141             fail("Cannot find method in " + sb, ex);
   142         }
   143         if (ret == null && expRes == null) {
   144             return;
   145         }
   146         if (expRes.equals(ret)) {
   147             return;
   148         }
   149         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
   150         
   151     }
   152 
   153     static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
   154         if (sb == null) {
   155             sb = new StringBuilder();
   156         }
   157         GenJS.compile(sb, names);
   158         ScriptEngineManager sem = new ScriptEngineManager();
   159         ScriptEngine js = sem.getEngineByExtension("js");
   160         try {
   161             Object res = js.eval(sb.toString());
   162             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   163             return (Invocable)js;
   164         } catch (ScriptException ex) {
   165             fail("Could not compile:\n" + sb, ex);
   166             return null;
   167         }
   168     }
   169 }