michael@0: /* michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/licenses/publicdomain/ michael@0: */ michael@0: michael@0: var BUGNUMBER = 645464; michael@0: var summary = michael@0: "[[DefaultValue]] behavior wrong for String with overridden valueOf/toString"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: // equality michael@0: michael@0: var s = new String("c"); michael@0: assertEq(s == "c", true); michael@0: michael@0: var s2 = new String(); michael@0: s2.valueOf = function() { return "foo"; }; michael@0: assertEq(s2 == "foo", true); michael@0: michael@0: var s3 = new String(); michael@0: s3.toString = function() { return "bar"; }; michael@0: assertEq(s3 == "", true); michael@0: michael@0: function testEquality() michael@0: { michael@0: var s = new String("c"); michael@0: assertEq(s == "c", true); michael@0: michael@0: var s2 = new String(); michael@0: s2.valueOf = function() { return "foo"; }; michael@0: assertEq(s2 == "foo", true); michael@0: michael@0: var s3 = new String(); michael@0: s3.toString = function() { return "bar"; }; michael@0: assertEq(s3 == "", true); michael@0: } michael@0: testEquality(); michael@0: michael@0: michael@0: // addition of String to string michael@0: michael@0: var s = new String(); michael@0: assertEq(s + "", ""); michael@0: michael@0: var s2 = new String(); michael@0: s2.toString = function() { return "bar"; }; michael@0: assertEq(s2 + "", ""); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "baz"; }; michael@0: assertEq(s3 + "", "baz"); michael@0: michael@0: function testStringAddition() michael@0: { michael@0: var s = new String(); michael@0: assertEq(s + "", ""); michael@0: michael@0: var s2 = new String(); michael@0: s2.toString = function() { return "bar"; }; michael@0: assertEq(s2 + "", ""); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "baz"; }; michael@0: assertEq(s3 + "", "baz"); michael@0: } michael@0: testStringAddition(); michael@0: michael@0: michael@0: // addition of String to String michael@0: michael@0: var s = new String(); michael@0: assertEq(s + s, ""); michael@0: michael@0: var s2 = new String(); michael@0: s2.toString = function() { return "baz"; }; michael@0: assertEq(s2 + s2, ""); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "quux"; }; michael@0: assertEq(s3 + s3, "quuxquux"); michael@0: michael@0: function testNonStringAddition() michael@0: { michael@0: var s = new String(); michael@0: assertEq(s + s, ""); michael@0: michael@0: var s2 = new String(); michael@0: s2.toString = function() { return "baz"; }; michael@0: assertEq(s2 + s2, ""); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "quux"; }; michael@0: assertEq(s3 + s3, "quuxquux"); michael@0: } michael@0: testNonStringAddition(); michael@0: michael@0: michael@0: // String as bracketed property name michael@0: michael@0: var obj = { primitive: 17, valueOf: 42, toString: 8675309 }; michael@0: michael@0: var s = new String("primitive"); michael@0: assertEq(obj[s], 17); michael@0: michael@0: var s2 = new String("primitive"); michael@0: s2.valueOf = function() { return "valueOf"; } michael@0: assertEq(obj[s2], 17); michael@0: michael@0: var s3 = new String("primitive"); michael@0: s3.toString = function() { return "toString"; }; michael@0: assertEq(obj[s3], 8675309); michael@0: michael@0: function testPropertyNameToString() michael@0: { michael@0: var obj = { primitive: 17, valueOf: 42, toString: 8675309 }; michael@0: michael@0: var s = new String("primitive"); michael@0: assertEq(obj[s], 17); michael@0: michael@0: var s2 = new String("primitive"); michael@0: s2.valueOf = function() { return "valueOf"; } michael@0: assertEq(obj[s2], 17); michael@0: michael@0: var s3 = new String("primitive"); michael@0: s3.toString = function() { return "toString"; }; michael@0: assertEq(obj[s3], 8675309); michael@0: } michael@0: testPropertyNameToString(); michael@0: michael@0: michael@0: // String as property name with |in| operator michael@0: michael@0: var s = new String(); michael@0: assertEq(s in { "": 5 }, true); michael@0: michael@0: var s2 = new String(); michael@0: s.toString = function() { return "baz"; }; michael@0: assertEq(s in { baz: 42 }, true); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "quux"; }; michael@0: assertEq(s3 in { "": 17 }, true); michael@0: michael@0: function testInOperatorName() michael@0: { michael@0: var s = new String(); michael@0: assertEq(s in { "": 5 }, true); michael@0: michael@0: var s2 = new String(); michael@0: s.toString = function() { return "baz"; }; michael@0: assertEq(s in { baz: 42 }, true); michael@0: michael@0: var s3 = new String(); michael@0: s3.valueOf = function() { return "quux"; }; michael@0: assertEq(s3 in { "": 17 }, true); michael@0: } michael@0: testInOperatorName(); michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("All tests passed!");