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("anon.functions", true);
  66         options.set("parse.only", true);
  67         options.set("scripting", true);
  68         options.set("const.as.var", true);
  69 
  70         final ErrorManager errors = new ErrorManager();
  71         this.context = new Context(options, errors, Thread.currentThread().getContextClassLoader());
  72     }
  73 
  74     @AfterClass
  75     public void tearDownTest() {
  76         this.context = null;
  77     }
  78 
  79     @Test
  80     public void parseAllTests() {
  81         if (TEST262) {
  82             parseTestSet(TEST262_SUITE_DIR, new TestFilter() {
  83                 @Override
  84                 public boolean exclude(final File file, final String content) {
  85                     return content != null && content.contains("@negative");
  86                 }
  87             });
  88         }
  89         parseTestSet(TEST_BASIC_DIR,  new TestFilter() {
  90             @Override
  91             public boolean exclude(final File file, final String content) {
  92                 return file.getName().equals("es6");
  93             }
  94         });
  95     }
  96 
  97     private void parseTestSet(final String testSet, final TestFilter filter) {
  98         passed  = 0;
  99         failed  = 0;
 100         skipped = 0;
 101 
 102         final File testSetDir = new File(testSet);
 103         if (! testSetDir.isDirectory()) {
 104             log("WARNING: " + testSetDir + " not found or not a directory");
 105             return;
 106         }
 107         log(testSetDir.getAbsolutePath());
 108         parseJSDirectory(testSetDir, filter);
 109 
 110         log(testSet + " parse done!");
 111         log("parse ok: " + passed);
 112         log("parse failed: " + failed);
 113         log("parse skipped: " + skipped);
 114         if (failed != 0) {
 115             Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath());
 116         }
 117     }
 118 
 119     // number of scripts that parsed fine
 120     private int passed;
 121     // number of scripts resulting in parse failure
 122     private int failed;
 123     // scripts that were skipped - all tests with @negative are
 124     // skipped for now.
 125     private int skipped;
 126 
 127     private void parseJSDirectory(final File dir, final TestFilter filter) {
 128         if (filter != null && filter.exclude(dir, null)) {
 129             return;
 130         }
 131         for (final File f : dir.listFiles()) {
 132             if (f.isDirectory()) {
 133                 parseJSDirectory(f, filter);
 134             } else if (f.getName().endsWith(".js")) {
 135                 parseJSFile(f, filter);
 136             }
 137         }
 138     }
 139 
 140     private void parseJSFile(final File file, final TestFilter filter) {
 141         if (VERBOSE) {
 142             log("Begin parsing " + file.getAbsolutePath());
 143         }
 144 
 145         try {
 146             final char[] buffer = readFully(file);
 147             boolean excluded = false;
 148             if (filter != null) {
 149                 final String content = new String(buffer);
 150                 excluded = filter.exclude(file, content);
 151             }
 152 
 153             if (excluded) {
 154                 if (VERBOSE) {
 155                     log("Skipping " + file.getAbsolutePath());
 156                 }
 157                 skipped++;
 158                 return;
 159             }
 160 
 161             final ErrorManager errors = new ErrorManager() {
 162                 @Override
 163                 public void error(final String msg) {
 164                     log(msg);
 165                 }
 166             };
 167             errors.setLimit(0);
 168             final Source source = sourceFor(file.getAbsolutePath(), buffer);
 169             new Parser(context.getEnv(), source, errors, context.getEnv()._strict, null).parse();
 170             if (errors.getNumberOfErrors() > 0) {
 171                 log("Parse failed: " + file.getAbsolutePath());
 172                 failed++;
 173             } else {
 174                 passed++;
 175             }
 176         } catch (final Throwable exp) {
 177             exp.printStackTrace();
 178             log("Parse failed: " + file.getAbsolutePath() + " : " + exp);
 179             if (VERBOSE) {
 180                 exp.printStackTrace(System.out);
 181             }
 182             failed++;
 183         }
 184 
 185         if (VERBOSE) {
 186             log("Done parsing " + file.getAbsolutePath());
 187         }
 188     }
 189 }