Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/drivers/JsTestsBase.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.drivers;
6
7 import java.io.File;
8 import java.io.FileReader;
9 import java.io.IOException;
10
11 import junit.framework.TestCase;
12
13 import org.mozilla.javascript.Context;
14 import org.mozilla.javascript.ContextFactory;
15 import org.mozilla.javascript.Scriptable;
16
17 public abstract class JsTestsBase extends TestCase {
18 private int optimizationLevel;
19
20 public void setOptimizationLevel(int level) {
21 this.optimizationLevel = level;
22 }
23
24 public void runJsTest(Context cx, Scriptable shared, String name, String source) {
25 // create a lightweight top-level scope
26 Scriptable scope = cx.newObject(shared);
27 scope.setPrototype(shared);
28 Object result;
29 try {
30 result = cx.evaluateString(scope, source, "jstest input: " + name, 1, null);
31 } catch (RuntimeException e) {
32 e.printStackTrace(System.err);
33 System.out.println("FAILED");
34 throw e;
35 }
36 assertTrue(result != null);
37 assertTrue("success".equals(result));
38 }
39
40 public void runJsTests(File[] tests) throws IOException {
41 ContextFactory factory = ContextFactory.getGlobal();
42 Context cx = factory.enterContext();
43 try {
44 cx.setOptimizationLevel(this.optimizationLevel);
45 Scriptable shared = cx.initStandardObjects();
46 for (File f : tests) {
47 int length = (int) f.length(); // don't worry about very long
48 // files
49 char[] buf = new char[length];
50 new FileReader(f).read(buf, 0, length);
51 String session = new String(buf);
52 runJsTest(cx, shared, f.getName(), session);
53 }
54 } finally {
55 Context.exit();
56 }
57 }
58 }