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