blob: 5abf9adbcfd96b6ac7ef00808fd9b03e0eac8d87 [file] [log] [blame]
ricow@chromium.org55ee8072011-09-08 16:33:10 +00001# Copyright 2011 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
28# Tests of duplicate properties in object literals.
29
30# ----------------------------------------------------------------------
31# Utility functions to generate a number of tests for each property
32# name pair.
33
34def PropertyTest(name, propa, propb, allow_strict = True):
35 replacement = {"id1": propa, "id2": propb, "name": name}
36
37 # Tests same test in both strict and non-strict context.
38 def StrictTest(name, source, replacement, expectation):
39 if (allow_strict):
40 Template("strict-" + name,
41 "\"use strict\";\n" + source)(replacement, expectation)
42 Template(name, source)(replacement, expectation)
43
44 # This one only fails in non-strict context.
45 if (allow_strict):
46 Template("strict-$name-data-data", """
47 "use strict";
48 var o = {$id1: 42, $id2: 42};
49 """)(replacement, "strict_duplicate_property")
50
51 Template("$name-data-data", """
52 var o = {$id1: 42, $id2: 42};
53 """)(replacement, None)
54
55 StrictTest("$name-data-get", """
56 var o = {$id1: 42, get $id2(){}};
57 """, replacement, "accessor_data_property")
58
59 StrictTest("$name-data-set", """
60 var o = {$id1: 42, set $id2(v){}};
61 """, replacement, "accessor_data_property")
62
63 StrictTest("$name-get-data", """
64 var o = {get $id1(){}, $id2: 42};
65 """, replacement, "accessor_data_property")
66
67 StrictTest("$name-set-data", """
68 var o = {set $id1(v){}, $id2: 42};
69 """, replacement, "accessor_data_property")
70
71 StrictTest("$name-get-get", """
72 var o = {get $id1(){}, get $id2(){}};
73 """, replacement, "accessor_get_set")
74
75 StrictTest("$name-set-set", """
76 var o = {set $id1(v){}, set $id2(v){}};
77 """, replacement, "accessor_get_set")
78
79 StrictTest("$name-nested-get", """
80 var o = {get $id1(){}, o: {get $id2(){} } };
81 """, replacement, None)
82
83 StrictTest("$name-nested-set", """
84 var o = {set $id1(){}, o: {set $id2(){} } };
85 """, replacement, None)
86
87
88def TestBothWays(name, propa, propb, allow_strict = True):
89 PropertyTest(name + "-1", propa, propb, allow_strict)
90 PropertyTest(name + "-2", propb, propa, allow_strict)
91
92def TestSame(name, prop, allow_strict = True):
93 PropertyTest(name, prop, prop, allow_strict)
94
95#-----------------------------------------------------------------------
96
97# Simple identifier property
98TestSame("a", "a")
99
100# Get/set identifiers
101TestSame("get-id", "get")
102TestSame("set-id", "set")
103
104# Number properties
105TestSame("0", "0")
106TestSame("0.1", "0.1")
107TestSame("1.0", "1.0")
108TestSame("42.33", "42.33")
109TestSame("2^32-2", "4294967294")
110TestSame("2^32", "4294967296")
111TestSame("2^53", "9007199254740992")
112TestSame("Hex20", "0x20")
113TestSame("exp10", "1e10")
114TestSame("exp20", "1e20")
115TestSame("Oct40", "040", False);
116
117
118# String properties
119TestSame("str-a", '"a"')
120TestSame("str-0", '"0"')
121TestSame("str-42", '"42"')
122TestSame("str-empty", '""')
123
124# Keywords
125TestSame("if", "if")
126TestSame("case", "case")
127
128# Future reserved keywords
129TestSame("public", "public")
130TestSame("class", "class")
131
132
133# Test that numbers are converted to string correctly.
134
135TestBothWays("hex-int", "0x20", "32")
136TestBothWays("oct-int", "040", "32", False) # Octals disallowed in strict mode.
137TestBothWays("dec-int", "32.00", "32")
138TestBothWays("dec-underflow-int",
139 "32.00000000000000000000000000000000000000001", "32")
140TestBothWays("exp-int", "3.2e1", "32")
141TestBothWays("exp-int", "3200e-2", "32")
142TestBothWays("overflow-inf", "1e2000", "Infinity")
143TestBothWays("overflow-inf-exact", "1.797693134862315808e+308", "Infinity")
144TestBothWays("non-overflow-inf-exact", "1.797693134862315807e+308",
145 "1.7976931348623157e+308")
146TestBothWays("underflow-0", "1e-2000", "0")
147TestBothWays("underflow-0-exact", "2.4703282292062E-324", "0")
148TestBothWays("non-underflow-0-exact", "2.4703282292063E-324", "5e-324")
149TestBothWays("precission-loss-high", "9007199254740992", "9007199254740993")
150TestBothWays("precission-loss-low", "1.9999999999999998", "1.9999999999999997")
151TestBothWays("non-canonical-literal-int", "1.0", "1")
152TestBothWays("non-canonical-literal-frac", "1.50", "1.5")
153TestBothWays("rounding-down", "1.12512512512512452", "1.1251251251251244")
154TestBothWays("rounding-up", "1.12512512512512453", "1.1251251251251246")
155
156TestBothWays("hex-int-str", "0x20", '"32"')
157TestBothWays("dec-int-str", "32.00", '"32"')
158TestBothWays("exp-int-str", "3.2e1", '"32"')
159TestBothWays("overflow-inf-str", "1e2000", '"Infinity"')
160TestBothWays("underflow-0-str", "1e-2000", '"0"')
161TestBothWays("non-canonical-literal-int-str", "1.0", '"1"')
162TestBothWays("non-canonical-literal-frac-str", "1.50", '"1.5"')