TestCase
動くっちゃあ動くけど微妙。
TestCaseであるfoo.js
var assert = Packages.junit.framework.Assert; function testFunction2(){ java.lang.System.out.println('TEST1'); assert.assertEquals(true, true); //assertEquals(true, true); } function testFunction1(){ java.lang.System.out.println('TEST2'); assert.assertEquals(true, true); //assertEquals(true, true); }
1ファイルで複数書けます。
testで始まるメソッドはテストケースとします。
TestCase
import junit.framework.TestCase; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.ScriptableObject; public class RhinoTestCase extends TestCase { private Function func; private Context cx; private ScriptableObject scope; public RhinoTestCase(String testName, Function func, Context cx, ScriptableObject scope) { super(testName); this.setName(testName); this.func = func; this.cx = cx; this.scope = scope; } /* * public void testScript() throws Exception { this.func.call(cx, scope, * scope, null); * } */ protected void runTest() throws Throwable { this.func.call(cx, scope, scope, null); } }
コールしてるだけ。
TestRunner
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import junit.framework.Test; import junit.framework.TestResult; import junit.framework.TestSuite; import junit.textui.TestRunner; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.Script; import org.mozilla.javascript.ScriptableObject; public class RhinoTestRunner extends TestRunner { public static void main(String args[]) { RhinoTestRunner runner = new RhinoTestRunner(); try { TestResult r = runner.start(args); if (!r.wasSuccessful()) System.exit(FAILURE_EXIT); System.exit(SUCCESS_EXIT); } catch (Exception e) { System.err.println(e.getMessage()); System.exit(EXCEPTION_EXIT); } } public Test getTest(String scriptName) { clearStatus(); String fileName = "C:\\foo.js"; Context cx = Context.enter(); FileReader filin = null; BufferedReader bufin = null; TestSuite suite = new TestSuite(); try { filin = new FileReader(fileName); bufin = new BufferedReader(filin); ScriptableObject scope = cx.initStandardObjects(); Script script = cx.compileReader(bufin, fileName, 0, null); Object result = script.exec(cx, scope); Object[] list = scope.getIds(); for (int i = 0; i < list.length; i++) { String name = list[i].toString(); if (name.toString().startsWith("test")) { Object o = scope.get(name, null); Function f = (Function) Function.class.cast(o); RhinoTestCase test = new RhinoTestCase(name, f, cx, scope); suite.addTest(test); } } return suite; } catch (IOException ex) { } finally { try { bufin.close(); filin.close(); } catch (Exception ex) { } } return null; } }
おおよその動きはこんなもん。
(固定でfoo.jsを呼んでるけど無視してね。)
Functionを取り出しTestCaseへ。
ばらしてるのでどのテストがこけたかわかります。
TextUIのみ動作します。
もっとスマートな案求む…。
うくく。