| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import org.junit.AfterClass; |
| 8 |
import org.junit.BeforeClass; |
| 9 |
import org.junit.Test; |
| 10 |
import org.junit.runner.RunWith; |
| 11 |
import org.junit.runners.Parameterized; |
| 12 |
import org.junit.runners.Parameterized.Parameters; |
| 13 |
import org.mozilla.javascript.Context; |
| 14 |
import org.mozilla.javascript.EvaluatorException; |
| 15 |
import org.mozilla.javascript.RhinoException; |
| 16 |
import org.mozilla.javascript.Script; |
| 17 |
import org.mozilla.javascript.Scriptable; |
| 18 |
import org.mozilla.javascript.drivers.TestUtils; |
| 19 |
import org.mozilla.javascript.tools.SourceReader; |
| 20 |
import org.mozilla.javascript.tools.shell.ShellContextFactory; |
| 21 |
import org.yaml.snakeyaml.Yaml; |
| 22 |
|
| 23 |
import java.io.*; |
| 24 |
import java.util.*; |
| 25 |
import java.util.regex.Matcher; |
| 26 |
import java.util.regex.Pattern; |
| 27 |
import org.yaml.snakeyaml.error.YAMLException; |
| 28 |
|
| 29 |
import static org.junit.Assert.assertEquals; |
| 30 |
import static org.junit.Assert.fail; |
| 31 |
import static org.mozilla.javascript.drivers.TestUtils.JS_FILE_FILTER; |
| 32 |
import static org.mozilla.javascript.drivers.TestUtils.recursiveListFilesHelper; |
| 33 |
|
| 34 |
@RunWith(Parameterized.class) |
| 35 |
public class Test262SuiteTest { |
| 36 |
|
| 37 |
static final int[] OPT_LEVELS = {-1, 0, 9}; |
| 38 |
|
| 39 |
static Map<Integer, Map<String, Script>> HARNESS_SCRIPT_CACHE = new HashMap<Integer, Map<String, Script>>(); |
| 40 |
|
| 41 |
static ShellContextFactory CTX_FACTORY = new ShellContextFactory(); |
| 42 |
|
| 43 |
@BeforeClass |
| 44 |
public static void setUpClass() throws Exception { |
| 45 |
HARNESS_SCRIPT_CACHE.put(-1, new HashMap<String, Script>()); |
| 46 |
HARNESS_SCRIPT_CACHE.put(0, new HashMap<String, Script>()); |
| 47 |
HARNESS_SCRIPT_CACHE.put(9, new HashMap<String, Script>()); |
| 48 |
|
| 49 |
CTX_FACTORY.setLanguageVersion(Context.VERSION_ES6); |
| 50 |
TestUtils.setGlobalContextFactory(CTX_FACTORY); |
| 51 |
} |
| 52 |
|
| 53 |
@AfterClass |
| 54 |
public static void tearDownClass() throws Exception { |
| 55 |
TestUtils.setGlobalContextFactory(null); |
| 56 |
} |
| 57 |
|
| 58 |
private static final Pattern EXCLUDE_PATTERN = Pattern.compile("\\s{1,4}!\\s*(.+)"); |
| 59 |
|
| 60 |
private final String jsFilePath; |
| 61 |
private final String jsFileStr; |
| 62 |
private final int optLevel; |
| 63 |
private final boolean useStrict; |
| 64 |
private List<String> harnessFiles; |
| 65 |
private EcmaErrorType errorType; |
| 66 |
|
| 67 |
public Test262SuiteTest(String jsFilePath, String jsFileStr, List<String> harnessFiles, int optLevel, boolean useStrict, EcmaErrorType errorType) { |
| 68 |
this.jsFilePath = jsFilePath; |
| 69 |
this.jsFileStr = jsFileStr; |
| 70 |
this.optLevel = optLevel; |
| 71 |
this.useStrict = useStrict; |
| 72 |
this.harnessFiles = harnessFiles; |
| 73 |
this.errorType = errorType; |
| 74 |
} |
| 75 |
|
| 76 |
private Object executeRhinoScript() { |
| 77 |
Context cx = Context.enter(); |
| 78 |
|
| 79 |
try { |
| 80 |
cx.setOptimizationLevel(optLevel); |
| 81 |
|
| 82 |
Scriptable scope = cx.initStandardObjects(); |
| 83 |
for (String harnessFile : harnessFiles) { |
| 84 |
if (!HARNESS_SCRIPT_CACHE.get(optLevel).containsKey(harnessFile)) { |
| 85 |
HARNESS_SCRIPT_CACHE.get(optLevel).put( |
| 86 |
harnessFile, |
| 87 |
cx.compileReader(new FileReader("test262/harness/" + harnessFile), "test262/harness/" + harnessFile, 1, null) |
| 88 |
); |
| 89 |
} |
| 90 |
HARNESS_SCRIPT_CACHE.get(optLevel).get(harnessFile).exec(cx, scope); |
| 91 |
} |
| 92 |
|
| 93 |
String str = jsFileStr; |
| 94 |
if (useStrict) { |
| 95 |
str = "\"use strict\";\nvar strict_mode = true;\n" + jsFileStr; |
| 96 |
} |
| 97 |
|
| 98 |
Object result = cx.evaluateString(scope, str, jsFilePath.replaceAll("\\\\", "/"), 1, null); |
| 99 |
|
| 100 |
if (errorType != EcmaErrorType.NONE) { |
| 101 |
fail(String.format("failed negative test. expected error: %s", errorType)); |
| 102 |
return null; |
| 103 |
} |
| 104 |
|
| 105 |
return result; |
| 106 |
} catch (RhinoException ex) { |
| 107 |
if (errorType == EcmaErrorType.NONE) { |
| 108 |
fail(String.format("%s%n%s", ex.getMessage(), ex.getScriptStackTrace())); |
| 109 |
} else { |
| 110 |
if (errorType == EcmaErrorType.ANY) { |
| 111 |
|
| 112 |
} else { |
| 113 |
String exceptionName; |
| 114 |
if (ex instanceof EvaluatorException) { |
| 115 |
exceptionName = "SyntaxError"; |
| 116 |
} else { |
| 117 |
exceptionName = ex.details(); |
| 118 |
if (exceptionName.contains(":")) { |
| 119 |
exceptionName = exceptionName.substring(0, exceptionName.indexOf(":")); |
| 120 |
} |
| 121 |
} |
| 122 |
assertEquals(ex.details(), errorType.name(), exceptionName); |
| 123 |
} |
| 124 |
} |
| 125 |
return null; |
| 126 |
} catch (Exception e) { |
| 127 |
throw new RuntimeException(e); |
| 128 |
} finally { |
| 129 |
Context.exit(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private static final Yaml YAML = new Yaml(); |
| 134 |
|
| 135 |
public static List<File> getTestFiles() throws IOException { |
| 136 |
File testDir = new File("test262/test"); |
| 137 |
|
| 138 |
List<File> testFiles = new LinkedList<File>(); |
| 139 |
|
| 140 |
List<File> dirFiles = new LinkedList<File>(); |
| 141 |
|
| 142 |
Scanner scanner = new Scanner(new File("testsrc/test262.properties")); |
| 143 |
|
| 144 |
String curLine = "", nxtLine = ""; |
| 145 |
|
| 146 |
while (true) { |
| 147 |
curLine = nxtLine; |
| 148 |
nxtLine = scanner.hasNextLine() ? scanner.nextLine() : null; |
| 149 |
|
| 150 |
if (curLine == null) break; |
| 151 |
|
| 152 |
if (curLine.isEmpty() || curLine.startsWith("#")) continue; |
| 153 |
|
| 154 |
File file = new File(testDir, curLine); |
| 155 |
|
| 156 |
if (file.isFile()) { |
| 157 |
testFiles.add(file); |
| 158 |
} else if (file.isDirectory()) { |
| 159 |
recursiveListFilesHelper(file, JS_FILE_FILTER, dirFiles); |
| 160 |
|
| 161 |
while (true) { |
| 162 |
curLine = nxtLine; |
| 163 |
nxtLine = scanner.hasNextLine() ? scanner.nextLine() : null; |
| 164 |
|
| 165 |
if (curLine == null) { |
| 166 |
testFiles.addAll(dirFiles); |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
if (curLine.isEmpty() || curLine.startsWith("#")) continue; |
| 171 |
|
| 172 |
Matcher m = EXCLUDE_PATTERN.matcher(curLine); |
| 173 |
if (m.matches()) { |
| 174 |
Iterator<File> it = dirFiles.iterator(); |
| 175 |
while (it.hasNext()) { |
| 176 |
String path = it.next().getPath().replaceAll("\\\\", "/"); |
| 177 |
if (path.contains(m.group(1))) it.remove(); |
| 178 |
} |
| 179 |
} else { |
| 180 |
testFiles.addAll(dirFiles); |
| 181 |
dirFiles.clear(); |
| 182 |
file = new File(testDir, curLine); |
| 183 |
if (file.isFile()) { |
| 184 |
testFiles.add(file); |
| 185 |
break; |
| 186 |
} else if (file.isDirectory()) { |
| 187 |
recursiveListFilesHelper(file, JS_FILE_FILTER, dirFiles); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
scanner.close(); |
| 194 |
|
| 195 |
return testFiles; |
| 196 |
} |
| 197 |
|
| 198 |
@Parameters(name = "js={0}, opt={3}, strict={4}") |
| 199 |
public static Collection<Object[]> test262SuiteValues() throws IOException { |
| 200 |
List<Object[]> result = new ArrayList<Object[]>(); |
| 201 |
List<File> tests = getTestFiles(); |
| 202 |
for (File jsTest : tests) { |
| 203 |
String jsFileStr = (String) SourceReader.readFileOrUrl(jsTest.getPath(), true, "UTF-8"); |
| 204 |
List<String> harnessFiles = new ArrayList<String>(); |
| 205 |
harnessFiles.add("sta.js"); |
| 206 |
harnessFiles.add("assert.js"); |
| 207 |
|
| 208 |
Map header; |
| 209 |
String hdrStr = jsFileStr |
| 210 |
.substring(jsFileStr.indexOf("/*---") + 5, jsFileStr.indexOf("---*/")); |
| 211 |
try { |
| 212 |
header = (Map) YAML.load(hdrStr); |
| 213 |
if (header.containsKey("includes")) { |
| 214 |
harnessFiles.addAll((Collection) header.get("includes")); |
| 215 |
} |
| 216 |
} catch (YAMLException e) { |
| 217 |
String msg = "Error scanning \"" + hdrStr + "\" from " + jsTest.getPath() + ": " + e; |
| 218 |
YAMLException te = new YAMLException(msg); |
| 219 |
te.initCause(e); |
| 220 |
throw te; |
| 221 |
} |
| 222 |
|
| 223 |
EcmaErrorType errorType = EcmaErrorType._valueOf((String)header.get("negative")); |
| 224 |
List<String> flags = header.containsKey("flags") ? (List<String>) header.get("flags") : Collections.EMPTY_LIST; |
| 225 |
for (int optLevel : OPT_LEVELS) { |
| 226 |
if (!flags.contains("onlyStrict")) { |
| 227 |
result.add(new Object[]{jsTest.getPath(), jsFileStr, harnessFiles, optLevel, false, errorType}); |
| 228 |
} |
| 229 |
if (!flags.contains("noStrict")) { |
| 230 |
result.add(new Object[]{jsTest.getPath(), jsFileStr, harnessFiles, optLevel, true, errorType}); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
@Test |
| 238 |
public void test262() throws Exception { |
| 239 |
executeRhinoScript(); |
| 240 |
} |
| 241 |
|
| 242 |
static enum EcmaErrorType { |
| 243 |
NONE, |
| 244 |
ANY, |
| 245 |
NotEarlyError, |
| 246 |
ReferenceError, |
| 247 |
SyntaxError, |
| 248 |
Test262Error, |
| 249 |
TypeError, |
| 250 |
expected_message; |
| 251 |
|
| 252 |
static EcmaErrorType _valueOf(String s) { |
| 253 |
if (s == null || s.equals("")) { |
| 254 |
return NONE; |
| 255 |
} else if (s.equals("NotEarlyError")) { |
| 256 |
return NotEarlyError; |
| 257 |
} else if (s.equals("ReferenceError")) { |
| 258 |
return ReferenceError; |
| 259 |
} else if (s.equals("SyntaxError")) { |
| 260 |
return SyntaxError; |
| 261 |
} else if (s.equals("Test262Error")) { |
| 262 |
return Test262Error; |
| 263 |
} else if (s.equals("TypeError")) { |
| 264 |
return TypeError; |
| 265 |
} else if (s.equals("expected_message")) { |
| 266 |
return expected_message; |
| 267 |
} |
| 268 |
return ANY; |
| 269 |
} |
| 270 |
} |
| 271 |
} |