blob: 391ef3d6d14b24f41d5d5ad2d80e469c8901dffb [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28function props(x) {
29 var array = [];
30 for (var p in x) array.push(p);
31 return array.sort();
32}
33
34function f1() {
35 this.x = 1;
36}
37
38function f2(x) {
39 this.x = x;
40}
41
42function f3(x) {
43 this.x = x;
44 this.y = 1;
45 this.z = f1;
46}
47
48function f4(x) {
49 this.x = x;
50 this.y = 1;
51 if (x == 1) return;
52 this.z = f1;
53}
54
55o1_1 = new f1();
56assertEquals(1, o1_1.x, "1");
57o1_2 = new f1();
58assertEquals(1, o1_1.x, "2");
59assertArrayEquals(["x"], props(o1_1), "3");
60assertArrayEquals(["x"], props(o1_2), "4");
61
62o2_1 = new f2(0);
63o2_2 = new f2(0);
64assertArrayEquals(["x"], props(o2_1));
65assertArrayEquals(["x"], props(o2_2));
66
67o3_1 = new f3(0);
68o3_2 = new f3(0);
69assertArrayEquals(["x", "y", "z"], props(o3_1));
70assertArrayEquals(["x", "y", "z"], props(o3_2));
71
72o4_0_1 = new f4(0);
73o4_0_2 = new f4(0);
74assertArrayEquals(["x", "y", "z"], props(o4_0_1));
75assertArrayEquals(["x", "y", "z"], props(o4_0_2));
76
77o4_1_1 = new f4(1);
78o4_1_2 = new f4(1);
79assertArrayEquals(["x", "y"], props(o4_1_1));
80assertArrayEquals(["x", "y"], props(o4_1_2));
81
82function f5(x, y) {
83 this.x = x;
84 this.y = y;
85}
86
87function f6(x, y) {
88 this.y = y;
89 this.x = x;
90}
91
92function f7(x, y, z) {
93 this.x = x;
94 this.y = y;
95}
96
97function testArgs(fun) {
98 obj = new fun();
99 assertArrayEquals(["x", "y"], props(obj));
100 assertEquals(void 0, obj.x);
101 assertEquals(void 0, obj.y);
102
103 obj = new fun("x");
104 assertArrayEquals(["x", "y"], props(obj));
105 assertEquals("x", obj.x);
106 assertEquals(void 0, obj.y);
107
108 obj = new fun("x", "y");
109 assertArrayEquals(["x", "y"], props(obj));
110 assertEquals("x", obj.x);
111 assertEquals("y", obj.y);
112
113 obj = new fun("x", "y", "z");
114 assertArrayEquals(["x", "y"], props(obj));
115 assertEquals("x", obj.x);
116 assertEquals("y", obj.y);
117}
118
119for (var i = 0; i < 10; i++) {
120 testArgs(f5);
121 testArgs(f6);
122 testArgs(f7);
123}
124
125function g(){
126 this.x=1
127}
128
129o = new g();
130assertEquals(1, o.x);
131o = new g();
132assertEquals(1, o.x);
133g.prototype = {y:2}
134o = new g();
135assertEquals(1, o.x);
136assertEquals(2, o.y);
137o = new g();
138assertEquals(1, o.x);
139assertEquals(2, o.y);