test/src/jdk/nashorn/internal/parser/ParserTest.java

Fri, 15 Feb 2013 18:30:19 +0530

author
sundar
date
Fri, 15 Feb 2013 18:30:19 +0530
changeset 97
757a49aaad02
parent 89
43e32b36153c
child 118
927fba6785b0
permissions
-rw-r--r--

8008291: Add more tests for better coverage of objects, scripting and parser packages
Reviewed-by: lagergren, jlaskey

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

mercurial