8008291: Add more tests for better coverage of objects, scripting and parser packages
authorsundar
Fri, 15 Feb 2013 18:30:19 +0530
changeset 16241 e23ed61e75a2
parent 16240 e1468b33e201
child 16242 c928b16d6ccd
8008291: Add more tests for better coverage of objects, scripting and parser packages Reviewed-by: lagergren, jlaskey
nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java
nashorn/src/jdk/nashorn/internal/parser/Scanner.java
nashorn/src/jdk/nashorn/internal/runtime/BitVector.java
nashorn/src/jdk/nashorn/internal/runtime/Context.java
nashorn/src/jdk/nashorn/internal/runtime/QuotedStringTokenizer.java
nashorn/src/jdk/nashorn/internal/runtime/RegExpScanner.java
nashorn/src/jdk/nashorn/internal/runtime/Source.java
nashorn/src/jdk/nashorn/tools/Shell.java
nashorn/test/script/basic/NASHORN-401.js
nashorn/test/script/basic/NASHORN-401.js.EXPECTED
nashorn/test/script/basic/assign_builtin_func_props.js
nashorn/test/script/basic/debugger.js
nashorn/test/script/basic/yield.js
nashorn/test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java
nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
nashorn/test/src/jdk/nashorn/api/scripting/Window.java
nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java
--- a/nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/parser/AbstractParser.java	Fri Feb 15 18:30:19 2013 +0530
@@ -101,15 +101,6 @@
     }
 
     /**
-     * Get the Source
-     *
-     * @return the Source
-     */
-    public Source getSource() {
-        return source;
-    }
-
-    /**
      * Get the ith token.
      *
      * @param i Index of token.
@@ -261,7 +252,7 @@
      * @return the message string
      */
     protected final String expectMessage(final TokenType expected) {
-        final String tokenString = Token.toString(source, token, false);
+        final String tokenString = Token.toString(source, token);
         String msg;
 
         if (expected == null) {
--- a/nashorn/src/jdk/nashorn/internal/parser/Scanner.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/parser/Scanner.java	Fri Feb 15 18:30:19 2013 +0530
@@ -59,7 +59,7 @@
      * @param start   position index in content where to start
      * @param length  length of input
      */
-    public Scanner(final char[] content, final int line, final int start, final int length) {
+    protected Scanner(final char[] content, final int line, final int start, final int length) {
         this.content  = content;
         this.position = start;
         this.limit    = start + length;
@@ -75,7 +75,7 @@
      *
      * @param content content to scan
      */
-    public Scanner(final String content) {
+    protected Scanner(final String content) {
         this(content.toCharArray(), 0, 0, content.length());
     }
 
--- a/nashorn/src/jdk/nashorn/internal/runtime/BitVector.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/runtime/BitVector.java	Fri Feb 15 18:30:19 2013 +0530
@@ -67,7 +67,7 @@
      * @param bits a bits array from another bit vector
      */
     public BitVector(final long[] bits) {
-        this.bits = bits;
+        this.bits = bits.clone();
     }
 
     /**
@@ -75,7 +75,7 @@
      * @param other the source
      */
     public void copy(final BitVector other) {
-        bits = Arrays.copyOf(other.bits, other.bits.length);
+        bits = other.bits.clone();
     }
 
     /**
--- a/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Context.java	Fri Feb 15 18:30:19 2013 +0530
@@ -672,7 +672,7 @@
             return evaluateSource(source, scope, scope);
         }
 
-        typeError("cant.load.script", ScriptRuntime.safeToString(source));
+        typeError("cant.load.script", ScriptRuntime.safeToString(from));
 
         return UNDEFINED;
     }
--- a/nashorn/src/jdk/nashorn/internal/runtime/QuotedStringTokenizer.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/runtime/QuotedStringTokenizer.java	Fri Feb 15 18:30:19 2013 +0530
@@ -34,7 +34,7 @@
  * the separators are quoted either by ' and ", or whatever quotes the user
  * supplies they will be ignored and considered part of another token
  */
-public class QuotedStringTokenizer {
+public final class QuotedStringTokenizer {
     private final LinkedList<String> tokens;
 
     private final char quotes[];
@@ -72,7 +72,7 @@
      *            all the characters that should be accepted as quotes, default
      *            is ' or "
      */
-    public QuotedStringTokenizer(final String str, final String delim, final char[] quotes) {
+    private QuotedStringTokenizer(final String str, final String delim, final char[] quotes) {
         this.quotes = quotes;
 
         boolean delimIsWhitespace = true;
--- a/nashorn/src/jdk/nashorn/internal/runtime/RegExpScanner.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/runtime/RegExpScanner.java	Fri Feb 15 18:30:19 2013 +0530
@@ -614,11 +614,7 @@
         final Token token = new Token(Token.Type.PATTERN);
 
         final Token child = disjunction();
-        if (child != null) {
-            return token.add(child);
-        }
-
-        return null;
+        return token.add(child);
     }
 
     /*
--- a/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/runtime/Source.java	Fri Feb 15 18:30:19 2013 +0530
@@ -201,18 +201,6 @@
     }
 
     /**
-     * Fetch a portion of source content associated with a range of tokens.
-     * @param startTokenDesc Starting token descriptor.
-     * @param endTokenDesc   Ending token descriptor.
-     * @return Source content portion.
-     */
-    public String getString(final long startTokenDesc, final long endTokenDesc) {
-        final int start = Token.descPosition(startTokenDesc);
-        final int end   = Token.descPosition(endTokenDesc) + Token.descLength(endTokenDesc);
-        return new String(content, start, end - start);
-    }
-
-    /**
      * Returns the source URL of this script Source. Can be null if Source
      * was created from a String or a char[].
      *
--- a/nashorn/src/jdk/nashorn/tools/Shell.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/src/jdk/nashorn/tools/Shell.java	Fri Feb 15 18:30:19 2013 +0530
@@ -192,12 +192,14 @@
         final Options options = new Options("nashorn", werr);
 
         // parse options
-        try {
-            options.process(args);
-        } catch (final IllegalArgumentException e) {
-            werr.println(bundle.getString("shell.usage"));
-            options.displayHelp(e);
-            return null;
+        if (args != null) {
+            try {
+                options.process(args);
+            } catch (final IllegalArgumentException e) {
+                werr.println(bundle.getString("shell.usage"));
+                options.displayHelp(e);
+                return null;
+            }
         }
 
         // detect scripting mode by any source's first character being '#'
--- a/nashorn/test/script/basic/NASHORN-401.js	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/script/basic/NASHORN-401.js	Fri Feb 15 18:30:19 2013 +0530
@@ -34,6 +34,7 @@
 print(t.method2(10.2));
 print(t.method2("a"));
 
+print(t.method3(10));
 print(t.method3("10"));
 print(t.method3("10.2"));
 print(t.method3("foo"));
--- a/nashorn/test/script/basic/NASHORN-401.js.EXPECTED	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/script/basic/NASHORN-401.js.EXPECTED	Fri Feb 15 18:30:19 2013 +0530
@@ -1,6 +1,7 @@
 int method 2
 double method 2
 string method 2
+int method 3: 10
 double method 3: 10.0
 double method 3: 10.2
 double method 3: NaN
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/assign_builtin_func_props.js	Fri Feb 15 18:30:19 2013 +0530
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+/**
+ * Check that we can assign to all properties from builtin functions and their
+ * prototypes. This is to make sure all such properties are writable.
+ *
+ * @test
+ * @run
+ */
+
+(function() {
+    var PropNamesGetter = Object.getOwnPropertyNames;
+    var ObjectType = Object;
+
+    function assignAll(obj) {
+        if (! (obj instanceof ObjectType)) {
+            return;
+        }
+        var props = PropNamesGetter(obj);
+        for (var p in props) {
+            var value = obj[props[p]];
+            obj[props[p]] = value;
+        }
+    }
+
+    var globalProps = PropNamesGetter(this);
+    for (var i in globalProps) {
+        var prop = globalProps[i];
+        if (typeof this[prop] == 'function') {
+            assignAll(this[prop].prototype);
+            assignAll(this[prop]);
+        }
+    }
+})();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/debugger.js	Fri Feb 15 18:30:19 2013 +0530
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+/**
+ * Check that the debugger statement is parsed and does nothing as of now.
+ *
+ * @test
+ * @run
+ */
+
+debugger;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/yield.js	Fri Feb 15 18:30:19 2013 +0530
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+/**
+ * Check yield keyword is parsed and yield statement does nothing (yet).
+ *
+ * @test
+ * @run
+ */
+
+function func() {
+    yield 2;
+}
--- a/nashorn/test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/MultipleEngineTest.java	Fri Feb 15 18:30:19 2013 +0530
@@ -28,7 +28,6 @@
 import javax.script.ScriptEngine;
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
-import org.testng.TestNG;
 import org.testng.annotations.Test;
 
 /**
@@ -37,10 +36,6 @@
  */
 
 public class MultipleEngineTest {
-    public static void main(final String[] args) {
-        TestNG.main(args);
-    }
-
     @Test
     public void createAndUseManyEngine() throws ScriptException {
         final ScriptEngineManager m = new ScriptEngineManager();
--- a/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java	Fri Feb 15 18:30:19 2013 +0530
@@ -50,7 +50,6 @@
 import jdk.nashorn.internal.runtime.Version;
 import netscape.javascript.JSObject;
 import org.testng.Assert;
-import org.testng.TestNG;
 import org.testng.annotations.Test;
 
 /**
@@ -58,10 +57,6 @@
  */
 public class ScriptEngineTest {
 
-    public static void main(final String[] args) {
-        TestNG.main(args);
-    }
-
     private void log(String msg) {
         org.testng.Reporter.log(msg, true);
     }
@@ -132,6 +127,7 @@
         assertEquals(fac.getEngineName(), "Oracle Nashorn");
         assertEquals(fac.getEngineVersion(), Version.version());
         assertEquals(fac.getOutputStatement("context"), "print(context)");
+        assertEquals(fac.getProgram("print('hello')", "print('world')"), "print('hello');print('world');");
         assertEquals(fac.getParameter(ScriptEngine.NAME), "javascript");
 
         boolean seenJS = false;
@@ -808,6 +804,9 @@
                 fail("obj.prop is not deleted!");
             }
 
+            // Simple eval tests
+            assertEquals(obj.eval("typeof Object"), "function");
+            assertEquals(obj.eval("'nashorn'.substring(3)"), "horn");
         } catch (final Exception exp) {
             exp.printStackTrace();
             fail(exp.getMessage());
--- a/nashorn/test/src/jdk/nashorn/api/scripting/Window.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/src/jdk/nashorn/api/scripting/Window.java	Fri Feb 15 18:30:19 2013 +0530
@@ -59,17 +59,8 @@
         return self.setTimeout(code, delay);
     }
 
-    public static void clearTimeout(final Window self, final int id) {
-        self.clearTimeout(id);
-    }
-
     public int setTimeout(final String code, final int delay) {
         System.out.println("window.setTimeout: " + delay + ", code: " + code);
         return 0;
     }
-
-    public void clearTimeout(final int id) {
-        System.out.println("window.clearTimeout: " + id);
-    }
-
 }
--- a/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Fri Feb 15 09:44:15 2013 +0100
+++ b/nashorn/test/src/jdk/nashorn/internal/parser/ParserTest.java	Fri Feb 15 18:30:19 2013 +0530
@@ -26,8 +26,6 @@
 package jdk.nashorn.internal.parser;
 
 import java.io.File;
-import jdk.nashorn.internal.codegen.Compiler;
-import jdk.nashorn.internal.codegen.CompilerConstants;
 import jdk.nashorn.internal.runtime.Context;
 import jdk.nashorn.internal.runtime.ErrorManager;
 import jdk.nashorn.internal.runtime.ScriptObject;