js/src/tests/ecma_5/String/defaultvalue.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 var BUGNUMBER = 645464;
michael@0 7 var summary =
michael@0 8 "[[DefaultValue]] behavior wrong for String with overridden valueOf/toString";
michael@0 9
michael@0 10 print(BUGNUMBER + ": " + summary);
michael@0 11
michael@0 12 /**************
michael@0 13 * BEGIN TEST *
michael@0 14 **************/
michael@0 15
michael@0 16 // equality
michael@0 17
michael@0 18 var s = new String("c");
michael@0 19 assertEq(s == "c", true);
michael@0 20
michael@0 21 var s2 = new String();
michael@0 22 s2.valueOf = function() { return "foo"; };
michael@0 23 assertEq(s2 == "foo", true);
michael@0 24
michael@0 25 var s3 = new String();
michael@0 26 s3.toString = function() { return "bar"; };
michael@0 27 assertEq(s3 == "", true);
michael@0 28
michael@0 29 function testEquality()
michael@0 30 {
michael@0 31 var s = new String("c");
michael@0 32 assertEq(s == "c", true);
michael@0 33
michael@0 34 var s2 = new String();
michael@0 35 s2.valueOf = function() { return "foo"; };
michael@0 36 assertEq(s2 == "foo", true);
michael@0 37
michael@0 38 var s3 = new String();
michael@0 39 s3.toString = function() { return "bar"; };
michael@0 40 assertEq(s3 == "", true);
michael@0 41 }
michael@0 42 testEquality();
michael@0 43
michael@0 44
michael@0 45 // addition of String to string
michael@0 46
michael@0 47 var s = new String();
michael@0 48 assertEq(s + "", "");
michael@0 49
michael@0 50 var s2 = new String();
michael@0 51 s2.toString = function() { return "bar"; };
michael@0 52 assertEq(s2 + "", "");
michael@0 53
michael@0 54 var s3 = new String();
michael@0 55 s3.valueOf = function() { return "baz"; };
michael@0 56 assertEq(s3 + "", "baz");
michael@0 57
michael@0 58 function testStringAddition()
michael@0 59 {
michael@0 60 var s = new String();
michael@0 61 assertEq(s + "", "");
michael@0 62
michael@0 63 var s2 = new String();
michael@0 64 s2.toString = function() { return "bar"; };
michael@0 65 assertEq(s2 + "", "");
michael@0 66
michael@0 67 var s3 = new String();
michael@0 68 s3.valueOf = function() { return "baz"; };
michael@0 69 assertEq(s3 + "", "baz");
michael@0 70 }
michael@0 71 testStringAddition();
michael@0 72
michael@0 73
michael@0 74 // addition of String to String
michael@0 75
michael@0 76 var s = new String();
michael@0 77 assertEq(s + s, "");
michael@0 78
michael@0 79 var s2 = new String();
michael@0 80 s2.toString = function() { return "baz"; };
michael@0 81 assertEq(s2 + s2, "");
michael@0 82
michael@0 83 var s3 = new String();
michael@0 84 s3.valueOf = function() { return "quux"; };
michael@0 85 assertEq(s3 + s3, "quuxquux");
michael@0 86
michael@0 87 function testNonStringAddition()
michael@0 88 {
michael@0 89 var s = new String();
michael@0 90 assertEq(s + s, "");
michael@0 91
michael@0 92 var s2 = new String();
michael@0 93 s2.toString = function() { return "baz"; };
michael@0 94 assertEq(s2 + s2, "");
michael@0 95
michael@0 96 var s3 = new String();
michael@0 97 s3.valueOf = function() { return "quux"; };
michael@0 98 assertEq(s3 + s3, "quuxquux");
michael@0 99 }
michael@0 100 testNonStringAddition();
michael@0 101
michael@0 102
michael@0 103 // String as bracketed property name
michael@0 104
michael@0 105 var obj = { primitive: 17, valueOf: 42, toString: 8675309 };
michael@0 106
michael@0 107 var s = new String("primitive");
michael@0 108 assertEq(obj[s], 17);
michael@0 109
michael@0 110 var s2 = new String("primitive");
michael@0 111 s2.valueOf = function() { return "valueOf"; }
michael@0 112 assertEq(obj[s2], 17);
michael@0 113
michael@0 114 var s3 = new String("primitive");
michael@0 115 s3.toString = function() { return "toString"; };
michael@0 116 assertEq(obj[s3], 8675309);
michael@0 117
michael@0 118 function testPropertyNameToString()
michael@0 119 {
michael@0 120 var obj = { primitive: 17, valueOf: 42, toString: 8675309 };
michael@0 121
michael@0 122 var s = new String("primitive");
michael@0 123 assertEq(obj[s], 17);
michael@0 124
michael@0 125 var s2 = new String("primitive");
michael@0 126 s2.valueOf = function() { return "valueOf"; }
michael@0 127 assertEq(obj[s2], 17);
michael@0 128
michael@0 129 var s3 = new String("primitive");
michael@0 130 s3.toString = function() { return "toString"; };
michael@0 131 assertEq(obj[s3], 8675309);
michael@0 132 }
michael@0 133 testPropertyNameToString();
michael@0 134
michael@0 135
michael@0 136 // String as property name with |in| operator
michael@0 137
michael@0 138 var s = new String();
michael@0 139 assertEq(s in { "": 5 }, true);
michael@0 140
michael@0 141 var s2 = new String();
michael@0 142 s.toString = function() { return "baz"; };
michael@0 143 assertEq(s in { baz: 42 }, true);
michael@0 144
michael@0 145 var s3 = new String();
michael@0 146 s3.valueOf = function() { return "quux"; };
michael@0 147 assertEq(s3 in { "": 17 }, true);
michael@0 148
michael@0 149 function testInOperatorName()
michael@0 150 {
michael@0 151 var s = new String();
michael@0 152 assertEq(s in { "": 5 }, true);
michael@0 153
michael@0 154 var s2 = new String();
michael@0 155 s.toString = function() { return "baz"; };
michael@0 156 assertEq(s in { baz: 42 }, true);
michael@0 157
michael@0 158 var s3 = new String();
michael@0 159 s3.valueOf = function() { return "quux"; };
michael@0 160 assertEq(s3 in { "": 17 }, true);
michael@0 161 }
michael@0 162 testInOperatorName();
michael@0 163
michael@0 164 /******************************************************************************/
michael@0 165
michael@0 166 if (typeof reportCompare === "function")
michael@0 167 reportCompare(true, true);
michael@0 168
michael@0 169 print("All tests passed!");

mercurial