Martin@582: /** Martin@582: * Back 2 Browser Bytecode Translator Martin@582: * Copyright (C) 2012 Jaroslav Tulach Martin@582: * Martin@582: * This program is free software: you can redistribute it and/or modify Martin@582: * it under the terms of the GNU General Public License as published by Martin@582: * the Free Software Foundation, version 2 of the License. Martin@582: * Martin@582: * This program is distributed in the hope that it will be useful, Martin@582: * but WITHOUT ANY WARRANTY; without even the implied warranty of Martin@582: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Martin@582: * GNU General Public License for more details. Martin@582: * Martin@582: * You should have received a copy of the GNU General Public License Martin@582: * along with this program. Look for COPYING file in the top folder. Martin@582: * If not, see http://opensource.org/licenses/GPL-2.0. Martin@582: */ Martin@582: package org.apidesign.vm4brwsr; Martin@582: Martin@582: import javax.script.Invocable; Martin@582: import javax.script.ScriptException; Martin@582: import static org.testng.Assert.*; Martin@582: Martin@582: class TestUtils { Martin@582: Martin@582: static Object execCode(Invocable code, CharSequence codeSeq, Martin@582: String msg, Class clazz, String method, Object expRes, Object... args) Martin@582: throws Exception Martin@582: { Martin@582: Object ret = null; Martin@582: try { Martin@582: ret = code.invokeFunction("bck2brwsr"); Martin@582: ret = code.invokeMethod(ret, "loadClass", clazz.getName()); Martin@582: ret = code.invokeMethod(ret, method, args); Martin@582: } catch (ScriptException ex) { Martin@582: fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex); Martin@582: } catch (NoSuchMethodException ex) { Martin@582: fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex); Martin@582: } Martin@582: if (ret == null && expRes == null) { Martin@582: return null; Martin@582: } jaroslav@704: if (expRes != null && expRes.equals(ret)) { Martin@582: return null; Martin@582: } Martin@582: if (expRes instanceof Number) { Martin@582: // in case of Long it is necessary convert it to number Martin@582: // since the Long is represented by two numbers in JavaScript Martin@582: try { Martin@615: ret = code.invokeMethod(ret, "toFP"); Martin@582: ret = code.invokeFunction("Number", ret); Martin@582: } catch (ScriptException ex) { Martin@582: fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex); Martin@582: } catch (NoSuchMethodException ex) { Martin@582: fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex); Martin@582: } Martin@582: } Martin@582: return ret; Martin@582: } Martin@582: }