| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 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(); |
| 48 |
|
| 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 |
} |