1 /* |
|
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.nashorn.internal.parser; |
|
27 |
|
28 import static jdk.nashorn.internal.runtime.Source.readFully; |
|
29 import static jdk.nashorn.internal.runtime.Source.sourceFor; |
|
30 import java.io.File; |
|
31 import jdk.nashorn.internal.runtime.Context; |
|
32 import jdk.nashorn.internal.runtime.ErrorManager; |
|
33 import jdk.nashorn.internal.runtime.Source; |
|
34 import jdk.nashorn.internal.runtime.options.Options; |
|
35 import org.testng.Assert; |
|
36 import org.testng.annotations.AfterClass; |
|
37 import org.testng.annotations.BeforeClass; |
|
38 import org.testng.annotations.Test; |
|
39 |
|
40 /** |
|
41 * Run tests to check Nashorn's parser. |
|
42 */ |
|
43 @SuppressWarnings("javadoc") |
|
44 public class ParserTest { |
|
45 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("parsertest.verbose")); |
|
46 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("parsertest.test262")); |
|
47 |
|
48 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); |
|
49 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); |
|
50 |
|
51 |
|
52 interface TestFilter { |
|
53 public boolean exclude(File file, String content); |
|
54 } |
|
55 |
|
56 private static void log(final String msg) { |
|
57 org.testng.Reporter.log(msg, true); |
|
58 } |
|
59 |
|
60 private Context context; |
|
61 |
|
62 @BeforeClass |
|
63 public void setupTest() { |
|
64 final Options options = new Options("nashorn"); |
|
65 options.set("parse.only", true); |
|
66 options.set("scripting", true); |
|
67 options.set("const.as.var", true); |
|
68 |
|
69 final ErrorManager errors = new ErrorManager(); |
|
70 this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
|
71 } |
|
72 |
|
73 @AfterClass |
|
74 public void tearDownTest() { |
|
75 this.context = null; |
|
76 } |
|
77 |
|
78 @Test |
|
79 public void parseAllTests() { |
|
80 if (TEST262) { |
|
81 parseTestSet(TEST262_SUITE_DIR, new TestFilter() { |
|
82 @Override |
|
83 public boolean exclude(final File file, final String content) { |
|
84 return content != null && content.contains("@negative"); |
|
85 } |
|
86 }); |
|
87 } |
|
88 parseTestSet(TEST_BASIC_DIR, new TestFilter() { |
|
89 @Override |
|
90 public boolean exclude(final File file, final String content) { |
|
91 return file.getName().equals("es6"); |
|
92 } |
|
93 }); |
|
94 } |
|
95 |
|
96 private void parseTestSet(final String testSet, final TestFilter filter) { |
|
97 passed = 0; |
|
98 failed = 0; |
|
99 skipped = 0; |
|
100 |
|
101 final File testSetDir = new File(testSet); |
|
102 if (! testSetDir.isDirectory()) { |
|
103 log("WARNING: " + testSetDir + " not found or not a directory"); |
|
104 return; |
|
105 } |
|
106 log(testSetDir.getAbsolutePath()); |
|
107 parseJSDirectory(testSetDir, filter); |
|
108 |
|
109 log(testSet + " parse done!"); |
|
110 log("parse ok: " + passed); |
|
111 log("parse failed: " + failed); |
|
112 log("parse skipped: " + skipped); |
|
113 if (failed != 0) { |
|
114 Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); |
|
115 } |
|
116 } |
|
117 |
|
118 // number of scripts that parsed fine |
|
119 private int passed; |
|
120 // number of scripts resulting in parse failure |
|
121 private int failed; |
|
122 // scripts that were skipped - all tests with @negative are |
|
123 // skipped for now. |
|
124 private int skipped; |
|
125 |
|
126 private void parseJSDirectory(final File dir, final TestFilter filter) { |
|
127 if (filter != null && filter.exclude(dir, null)) { |
|
128 return; |
|
129 } |
|
130 for (final File f : dir.listFiles()) { |
|
131 if (f.isDirectory()) { |
|
132 parseJSDirectory(f, filter); |
|
133 } else if (f.getName().endsWith(".js")) { |
|
134 parseJSFile(f, filter); |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 private void parseJSFile(final File file, final TestFilter filter) { |
|
140 if (VERBOSE) { |
|
141 log("Begin parsing " + file.getAbsolutePath()); |
|
142 } |
|
143 |
|
144 try { |
|
145 final char[] buffer = readFully(file); |
|
146 boolean excluded = false; |
|
147 if (filter != null) { |
|
148 final String content = new String(buffer); |
|
149 excluded = filter.exclude(file, content); |
|
150 } |
|
151 |
|
152 if (excluded) { |
|
153 if (VERBOSE) { |
|
154 log("Skipping " + file.getAbsolutePath()); |
|
155 } |
|
156 skipped++; |
|
157 return; |
|
158 } |
|
159 |
|
160 final ErrorManager errors = new ErrorManager() { |
|
161 @Override |
|
162 public void error(final String msg) { |
|
163 log(msg); |
|
164 } |
|
165 }; |
|
166 errors.setLimit(0); |
|
167 final Source source = sourceFor(file.getAbsolutePath(), buffer); |
|
168 new Parser(context.getEnv(), source, errors, context.getEnv()._strict, null).parse(); |
|
169 if (errors.getNumberOfErrors() > 0) { |
|
170 log("Parse failed: " + file.getAbsolutePath()); |
|
171 failed++; |
|
172 } else { |
|
173 passed++; |
|
174 } |
|
175 } catch (final Throwable exp) { |
|
176 exp.printStackTrace(); |
|
177 log("Parse failed: " + file.getAbsolutePath() + " : " + exp); |
|
178 if (VERBOSE) { |
|
179 exp.printStackTrace(System.out); |
|
180 } |
|
181 failed++; |
|
182 } |
|
183 |
|
184 if (VERBOSE) { |
|
185 log("Done parsing " + file.getAbsolutePath()); |
|
186 } |
|
187 } |
|
188 } |
|