| 1 | package org.mozilla.javascript.tests.commonjs.module; |
| 2 | |
| 3 | import junit.framework.AssertionFailedError; |
| 4 | import org.junit.runner.RunWith; |
| 5 | import org.junit.runners.Parameterized; |
| 6 | import org.mozilla.javascript.Context; |
| 7 | import org.mozilla.javascript.Function; |
| 8 | import org.mozilla.javascript.Scriptable; |
| 9 | import org.mozilla.javascript.ScriptableObject; |
| 10 | import org.mozilla.javascript.commonjs.module.Require; |
| 11 | import org.mozilla.javascript.commonjs.module.provider.StrongCachingModuleScriptProvider; |
| 12 | import org.mozilla.javascript.commonjs.module.provider.UrlModuleSourceProvider; |
| 13 | |
| 14 | import java.io.File; |
| 15 | import java.net.URI; |
| 16 | import java.net.URISyntaxException; |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Collection; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | |
| 22 | @RunWith(Parameterized.class) |
| 23 | public class ComplianceTest { |
| 24 | |
| 25 | private File testDir; |
| 26 | |
| 27 | public ComplianceTest(String name, File testDir) { |
| 28 | this.testDir = testDir; |
| 29 | } |
| 30 | |
| 31 | @Parameterized.Parameters(name = "/{0}") |
| 32 | public static Collection<Object[]> data() { |
| 33 | List<Object[]> retval = new ArrayList<Object[]>(16); |
| 34 | final File[] files = new File("testsrc/org/mozilla/javascript/tests/commonjs/module/1.0").listFiles(); |
| 35 | for (File file : files) { |
| 36 | if (file.isDirectory()) { |
| 37 | retval.add(new Object[]{file.getName(), file}); |
| 38 | } |
| 39 | } |
| 40 | return retval; |
| 41 | } |
| 42 | |
| 43 | private static Require createRequire(File dir, Context cx, Scriptable scope) |
| 44 | throws URISyntaxException { |
| 45 | return new Require(cx, scope, new StrongCachingModuleScriptProvider( |
| 46 | new UrlModuleSourceProvider( |
| 47 | Collections.singleton(dir.getAbsoluteFile().toURI()), |
| 48 | Collections.singleton(new URI(ComplianceTest.class.getResource(".").toExternalForm() + "/")))), |
| 49 | null, null, false); |
| 50 | } |
| 51 | |
| 52 | @org.junit.Test |
| 53 | public void testRequire() throws Throwable { |
| 54 | final Context cx = Context.enter(); |
| 55 | try { |
| 56 | cx.setOptimizationLevel(-1); |
| 57 | final Scriptable scope = cx.initStandardObjects(); |
| 58 | ScriptableObject.putProperty(scope, "print", new Print(scope)); |
| 59 | createRequire(testDir, cx, scope).requireMain(cx, "program"); |
| 60 | } finally { |
| 61 | Context.exit(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private static class Print extends ScriptableObject implements Function { |
| 66 | Print(Scriptable scope) { |
| 67 | setPrototype(ScriptableObject.getFunctionPrototype(scope)); |
| 68 | } |
| 69 | |
| 70 | public Object call(Context cx, Scriptable scope, Scriptable thisObj, |
| 71 | Object[] args) { |
| 72 | if (args.length > 1 && "fail".equals(args[1])) { |
| 73 | throw new AssertionFailedError(String.valueOf(args[0])); |
| 74 | } |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | public Scriptable construct(Context cx, Scriptable scope, Object[] args) { |
| 79 | throw new AssertionFailedError("Shouldn't be invoked as constructor"); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public String getClassName() { |
| 84 | return "Function"; |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | } |