summaryrefslogtreecommitdiffstats
path: root/js/src/tests/ecma_5/String/defaultvalue.js
blob: 50b4fe00df3c6d9f2cbba76125e232dbf69e6529 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/licenses/publicdomain/
 */

var BUGNUMBER = 645464;
var summary =
  "[[DefaultValue]] behavior wrong for String with overridden valueOf/toString";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

// equality

var s = new String("c");
assertEq(s == "c", true);

var s2 = new String();
s2.valueOf = function() { return "foo"; };
assertEq(s2 == "foo", true);

var s3 = new String();
s3.toString = function() { return "bar"; };
assertEq(s3 == "", true);

function testEquality()
{
  var s = new String("c");
  assertEq(s == "c", true);

  var s2 = new String();
  s2.valueOf = function() { return "foo"; };
  assertEq(s2 == "foo", true);

  var s3 = new String();
  s3.toString = function() { return "bar"; };
  assertEq(s3 == "", true);
}
testEquality();


// addition of String to string

var s = new String();
assertEq(s + "", "");

var s2 = new String();
s2.toString = function() { return "bar"; };
assertEq(s2 + "", "");

var s3 = new String();
s3.valueOf = function() { return "baz"; };
assertEq(s3 + "", "baz");

function testStringAddition()
{
  var s = new String();
  assertEq(s + "", "");

  var s2 = new String();
  s2.toString = function() { return "bar"; };
  assertEq(s2 + "", "");

  var s3 = new String();
  s3.valueOf = function() { return "baz"; };
  assertEq(s3 + "", "baz");
}
testStringAddition();


// addition of String to String

var s = new String();
assertEq(s + s, "");

var s2 = new String();
s2.toString = function() { return "baz"; };
assertEq(s2 + s2, "");

var s3 = new String();
s3.valueOf = function() { return "quux"; };
assertEq(s3 + s3, "quuxquux");

function testNonStringAddition()
{
  var s = new String();
  assertEq(s + s, "");

  var s2 = new String();
  s2.toString = function() { return "baz"; };
  assertEq(s2 + s2, "");

  var s3 = new String();
  s3.valueOf = function() { return "quux"; };
  assertEq(s3 + s3, "quuxquux");
}
testNonStringAddition();


// String as bracketed property name

var obj = { primitive: 17, valueOf: 42, toString: 8675309 };

var s = new String("primitive");
assertEq(obj[s], 17);

var s2 = new String("primitive");
s2.valueOf = function() { return "valueOf"; }
assertEq(obj[s2], 17);

var s3 = new String("primitive");
s3.toString = function() { return "toString"; };
assertEq(obj[s3], 8675309);

function testPropertyNameToString()
{
  var obj = { primitive: 17, valueOf: 42, toString: 8675309 };

  var s = new String("primitive");
  assertEq(obj[s], 17);

  var s2 = new String("primitive");
  s2.valueOf = function() { return "valueOf"; }
  assertEq(obj[s2], 17);

  var s3 = new String("primitive");
  s3.toString = function() { return "toString"; };
  assertEq(obj[s3], 8675309);
}
testPropertyNameToString();


// String as property name with |in| operator

var s = new String();
assertEq(s in { "": 5 }, true);

var s2 = new String();
s.toString = function() { return "baz"; };
assertEq(s in { baz: 42 }, true);

var s3 = new String();
s3.valueOf = function() { return "quux"; };
assertEq(s3 in { "": 17 }, true);

function testInOperatorName()
{
  var s = new String();
  assertEq(s in { "": 5 }, true);

  var s2 = new String();
  s.toString = function() { return "baz"; };
  assertEq(s in { baz: 42 }, true);

  var s3 = new String();
  s3.valueOf = function() { return "quux"; };
  assertEq(s3 in { "": 17 }, true);
}
testInOperatorName();

/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("All tests passed!");