vm/src/test/java/org/apidesign/vm4brwsr/TestUtils.java
author Martin Soch <Martin.Soch@oracle.com>
Fri, 25 Jan 2013 11:00:52 +0100
brancharithmetic
changeset 582 8e546d108658
child 615 e3f671b50e93
permissions -rw-r--r--
Long arithmetic prototype, Long currently represented by separate JavaScript object with two JS-Numbers.
Just few operation implemented to pass tests. Tests under vmtest directory are still failing.
     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 static org.testng.Assert.*;
    23 
    24 class TestUtils {
    25 
    26     static Object execCode(Invocable code, CharSequence codeSeq,
    27         String msg, Class<?> clazz, String method, Object expRes, Object... args) 
    28             throws Exception
    29     {
    30         Object ret = null;
    31         try {
    32             ret = code.invokeFunction("bck2brwsr");
    33             ret = code.invokeMethod(ret, "loadClass", clazz.getName());
    34             ret = code.invokeMethod(ret, method, args);
    35         } catch (ScriptException ex) {
    36             fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
    37         } catch (NoSuchMethodException ex) {
    38             fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex);
    39         }
    40         if (ret == null && expRes == null) {
    41             return null;
    42         }
    43         if (expRes.equals(ret)) {
    44             return null;
    45         }
    46         if (expRes instanceof Number) {
    47             // in case of Long it is necessary convert it to number
    48             // since the Long is represented by two numbers in JavaScript
    49             try {
    50                 ret = code.invokeFunction("Number", ret);
    51             } catch (ScriptException ex) {
    52                 fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
    53             } catch (NoSuchMethodException ex) {
    54                 fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex);
    55             }
    56         }
    57         return ret;
    58     }
    59 }