1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/script/basic/JDK-8072426.js Fri Feb 20 15:47:28 2015 +0100 1.3 @@ -0,0 +1,164 @@ 1.4 +/* 1.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * JDK-8072426: Can't compare Java objects to strings or numbers 1.29 + * 1.30 + * @test 1.31 + * @run 1.32 + */ 1.33 + 1.34 +Assert.assertTrue(java.math.RoundingMode.UP == "UP"); 1.35 + 1.36 +var JSObject = Java.type("jdk.nashorn.api.scripting.JSObject"); 1.37 + 1.38 +// Adds an "isFunction" member to the JSObject that returns the specified value 1.39 +function addIsFunction(isFunction, obj) { 1.40 + obj.isFunction = function() { 1.41 + return isFunction; 1.42 + }; 1.43 + return obj; 1.44 +} 1.45 + 1.46 +function makeJSObjectConstantFunction(value) { 1.47 + return new JSObject(addIsFunction(true, { 1.48 + call: function() { 1.49 + return value; 1.50 + } 1.51 + })); 1.52 +} 1.53 + 1.54 +function makeJSObjectWithMembers(mapping) { 1.55 + return new JSObject({ 1.56 + getMember: function(name) { 1.57 + Assert.assertTrue(mapping.hasOwnProperty(name)); 1.58 + return mapping[name]; 1.59 + }, 1.60 + toNumber: function() { 1.61 + // toNumber no longer invoked 1.62 + Assert.fail(); 1.63 + } 1.64 + }); 1.65 +} 1.66 + 1.67 +// Test JSObjectLinker toInt32/toLong/toNumber 1.68 +function testNumericJSObject(kind, value) { 1.69 + var obj = makeJSObjectWithMembers({ 1.70 + valueOf: makeJSObjectConstantFunction(value) 1.71 + }); 1.72 + 1.73 + if (kind === "double") { 1.74 + // There's no assertEquals(double actual, double expected). There's only 1.75 + // assertEquals(double actual, double expected, double delta). 1.76 + Assert["assertEquals(double,double,double)"](value, obj, 0); 1.77 + } else { 1.78 + Assert["assertEquals(" + kind + ", " + kind + ")"](value, obj); 1.79 + } 1.80 + Assert.assertTrue(value == Number(obj)); 1.81 +} 1.82 +testNumericJSObject("int", 42); 1.83 +testNumericJSObject("long", 4294967296); 1.84 +testNumericJSObject("double", 1.2); 1.85 + 1.86 +// Test fallback from toNumber to toString for numeric conversion when toNumber doesn't exist 1.87 +(function() { 1.88 + var obj = makeJSObjectWithMembers({ 1.89 + valueOf: null, // Explicitly no valueOf 1.90 + toString: makeJSObjectConstantFunction("123") 1.91 + }); 1.92 + Assert["assertEquals(int,int)"](123, obj); 1.93 +})(); 1.94 + 1.95 +// Test fallback from toNumber to toString for numeric conversion when toNumber isn't a callable 1.96 +(function() { 1.97 + var obj = makeJSObjectWithMembers({ 1.98 + valueOf: new JSObject(addIsFunction(false, {})), 1.99 + toString: makeJSObjectConstantFunction("124") 1.100 + }); 1.101 + Assert["assertEquals(int,int)"](124, obj); 1.102 +})(); 1.103 + 1.104 +// Test fallback from toNumber to toString for numeric conversion when toNumber returns a non-primitive 1.105 +(function() { 1.106 + var obj = makeJSObjectWithMembers({ 1.107 + valueOf: makeJSObjectConstantFunction({}), 1.108 + toString: makeJSObjectConstantFunction("125") 1.109 + }); 1.110 + Assert["assertEquals(int,int)"](125, obj); 1.111 +})(); 1.112 + 1.113 +// Test TypeError from toNumber to toString when both return a non-primitive 1.114 +(function() { 1.115 + var obj = makeJSObjectWithMembers({ 1.116 + valueOf: makeJSObjectConstantFunction({}), 1.117 + toString: makeJSObjectConstantFunction({}) 1.118 + }); 1.119 + try { 1.120 + Number(obj); 1.121 + Assert.fail(); // must throw 1.122 + } catch(e) { 1.123 + Assert.assertTrue(e instanceof TypeError); 1.124 + } 1.125 +})(); 1.126 + 1.127 +// Test toString for string conversion 1.128 +(function() { 1.129 + var obj = makeJSObjectWithMembers({ 1.130 + toString: makeJSObjectConstantFunction("Hello") 1.131 + }); 1.132 + Assert.assertTrue("Hello" === String(obj)); 1.133 + Assert["assertEquals(String,String)"]("Hello", obj); 1.134 +})(); 1.135 + 1.136 +// Test fallback from toString to valueOf for string conversion when toString doesn't exist 1.137 +(function() { 1.138 + var obj = makeJSObjectWithMembers({ 1.139 + toString: null, 1.140 + valueOf: makeJSObjectConstantFunction("Hello1") 1.141 + }); 1.142 + Assert.assertTrue("Hello1" === String(obj)); 1.143 + Assert["assertEquals(String,String)"]("Hello1", obj); 1.144 +})(); 1.145 + 1.146 +// Test fallback from toString to valueOf for string conversion when toString is not callable 1.147 +(function() { 1.148 + var obj = makeJSObjectWithMembers({ 1.149 + toString: new JSObject(addIsFunction(false, {})), 1.150 + valueOf: makeJSObjectConstantFunction("Hello2") 1.151 + }); 1.152 + Assert["assertEquals(String,String)"]("Hello2", obj); 1.153 +})(); 1.154 + 1.155 +// Test fallback from toString to valueOf for string conversion when toString returns non-primitive 1.156 +(function() { 1.157 + var obj = makeJSObjectWithMembers({ 1.158 + toString: makeJSObjectConstantFunction({}), 1.159 + valueOf: makeJSObjectConstantFunction("Hello3") 1.160 + }); 1.161 + Assert["assertEquals(String,String)"]("Hello3", obj); 1.162 +})(); 1.163 + 1.164 +// Test toBoolean for JSObject 1.165 +(function() { 1.166 + Assert["assertEquals(boolean,boolean)"](true, new JSObject({})); 1.167 +})();