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 java.io.File; 29 import jdk.nashorn.internal.codegen.Compiler; 30 import jdk.nashorn.internal.codegen.CompilerConstants; 31 import jdk.nashorn.internal.runtime.Context; 32 import jdk.nashorn.internal.runtime.ErrorManager; 33 import jdk.nashorn.internal.runtime.ScriptObject; 34 import jdk.nashorn.internal.runtime.Source; 35 import jdk.nashorn.internal.runtime.options.Options; 36 import org.testng.Assert; 37 import org.testng.annotations.Test; 38 39 /** 40 * Run tests to check Nashorn's parser. 41 */ 42 public class ParserTest { 43 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("parsertest.verbose")); 44 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("parsertest.test262")); 45 46 private static final String ES5CONFORM_DIR = System.getProperty("es5conform.testcases.dir"); 47 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 48 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 49 50 51 interface TestFilter { 52 public boolean exclude(File file, String content); 53 } 54 55 private void log(String msg) { 56 org.testng.Reporter.log(msg, true); 57 } 58 59 private Context context; 60 private ScriptObject global; 61 62 public ParserTest() { 63 final Options options = new Options("nashorn"); 64 options.set("anon.functions", true); 65 options.set("parse.only", true); 66 options.set("scripting", true); 67 68 ErrorManager errors = new ErrorManager(); 69 this.context = new Context(options, errors); 70 this.global = context.createGlobal(); 71 } 72 73 @Test 74 public void parseAllTests() { 75 if (TEST262) { 76 parseTestSet(TEST262_SUITE_DIR, new TestFilter() { 77 @Override 78 public boolean exclude(final File file, final String content) { 79 return content.indexOf("@negative") != -1; 80 } 81 }); 82 } 83 parseTestSet(ES5CONFORM_DIR, null); 84 parseTestSet(TEST_BASIC_DIR, null); 85 } 86 87 private void parseTestSet(final String testSet, final TestFilter filter) { 88 passed = 0; 89 failed = 0; 90 skipped = 0; 91 92 final File testSetDir = new File(testSet); 93 if (! testSetDir.isDirectory()) { 94 log("WARNING: " + testSetDir + " not found or not a directory"); 95 return; 96 } 97 log(testSetDir.getAbsolutePath()); 98 parseJSDirectory(testSetDir, filter); 99 100 log(testSet + " parse done!"); 101 log("parse ok: " + passed); 102 log("parse failed: " + failed); 103 log("parse skipped: " + skipped); 104 if (failed != 0) { 105 Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 106 } 107 } 108 109 // number of scripts that parsed fine 110 private int passed; 111 // number of scripts resulting in parse failure 112 private int failed; 113 // scripts that were skipped - all tests with @negative are 114 // skipped for now. 115 private int skipped; 116 117 private void parseJSDirectory(final File dir, final TestFilter filter) { 118 for (final File f : dir.listFiles()) { 119 if (f.isDirectory()) { 120 parseJSDirectory(f, filter); 121 } else if (f.getName().endsWith(".js")) { 122 parseJSFile(f, filter); 123 } 124 } 125 } 126 127 private void parseJSFile(final File file, final TestFilter filter) { 128 if (VERBOSE) { 129 log("Begin parsing " + file.getAbsolutePath()); 130 } 131 132 final ScriptObject oldGlobal = Context.getGlobal(); 133 final boolean globalChanged = (oldGlobal != global); 134 try { 135 final char[] buffer = Source.readFully(file); 136 boolean excluded = false; 137 if (filter != null) { 138 final String content = new String(buffer); 139 excluded = filter.exclude(file, content); 140 } 141 142 if (excluded) { 143 if (VERBOSE) { 144 log("Skipping " + file.getAbsolutePath()); 145 } 146 skipped++; 147 return; 148 } 149 150 final ErrorManager errors = new ErrorManager() { 151 @Override 152 public void error(final String msg) { 153 log(msg); 154 } 155 }; 156 errors.setLimit(0); 157 if (globalChanged) { 158 Context.setGlobal(global); 159 } 160 final Source source = new Source(file.getAbsolutePath(), buffer); 161 final Compiler compiler = Compiler.compiler(source, context, errors, context._strict); 162 163 final Parser parser = new Parser(compiler); 164 parser.parse(CompilerConstants.RUN_SCRIPT.tag()); 165 if (errors.getNumberOfErrors() > 0) { 166 log("Parse failed: " + file.getAbsolutePath()); 167 failed++; 168 } else { 169 passed++; 170 } 171 } catch (final Throwable exp) { 172 log("Parse failed: " + file.getAbsolutePath() + " : " + exp); 173 if (VERBOSE) { 174 exp.printStackTrace(System.out); 175 } 176 failed++; 177 } finally { 178 if (globalChanged) { 179 Context.setGlobal(oldGlobal); 180 } 181 } 182 183 if (VERBOSE) { 184 log("Done parsing " + file.getAbsolutePath()); 185 } 186 } 187 }