1 # 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 34 def 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(v){}, o: {set $id2(v){} } }; 85 """, replacement, None) 86 87 88 def TestBothWays(name, propa, propb, allow_strict = True): 89 PropertyTest(name + "-1", propa, propb, allow_strict) 90 PropertyTest(name + "-2", propb, propa, allow_strict) 91 92 def TestSame(name, prop, allow_strict = True): 93 PropertyTest(name, prop, prop, allow_strict) 94 95 #----------------------------------------------------------------------- 96 97 # Simple identifier property 98 TestSame("a", "a") 99 100 # Get/set identifiers 101 TestSame("get-id", "get") 102 TestSame("set-id", "set") 103 104 # Number properties 105 TestSame("0", "0") 106 TestSame("0.1", "0.1") 107 TestSame("1.0", "1.0") 108 TestSame("42.33", "42.33") 109 TestSame("2^32-2", "4294967294") 110 TestSame("2^32", "4294967296") 111 TestSame("2^53", "9007199254740992") 112 TestSame("Hex20", "0x20") 113 TestSame("exp10", "1e10") 114 TestSame("exp20", "1e20") 115 TestSame("Oct40", "040", False); 116 117 118 # String properties 119 TestSame("str-a", '"a"') 120 TestSame("str-0", '"0"') 121 TestSame("str-42", '"42"') 122 TestSame("str-empty", '""') 123 124 # Keywords 125 TestSame("if", "if") 126 TestSame("case", "case") 127 128 # Future reserved keywords 129 TestSame("public", "public") 130 TestSame("class", "class") 131 132 133 # Test that numbers are converted to string correctly. 134 135 TestBothWays("hex-int", "0x20", "32") 136 TestBothWays("oct-int", "040", "32", False) # Octals disallowed in strict mode. 137 TestBothWays("dec-int", "32.00", "32") 138 TestBothWays("dec-underflow-int", 139 "32.00000000000000000000000000000000000000001", "32") 140 TestBothWays("exp-int", "3.2e1", "32") 141 TestBothWays("exp-int", "3200e-2", "32") 142 TestBothWays("overflow-inf", "1e2000", "Infinity") 143 TestBothWays("overflow-inf-exact", "1.797693134862315808e+308", "Infinity") 144 TestBothWays("non-overflow-inf-exact", "1.797693134862315807e+308", 145 "1.7976931348623157e+308") 146 TestBothWays("underflow-0", "1e-2000", "0") 147 TestBothWays("underflow-0-exact", "2.4703282292062E-324", "0") 148 TestBothWays("non-underflow-0-exact", "2.4703282292063E-324", "5e-324") 149 TestBothWays("precission-loss-high", "9007199254740992", "9007199254740993") 150 TestBothWays("precission-loss-low", "1.9999999999999998", "1.9999999999999997") 151 TestBothWays("non-canonical-literal-int", "1.0", "1") 152 TestBothWays("non-canonical-literal-frac", "1.50", "1.5") 153 TestBothWays("rounding-down", "1.12512512512512452", "1.1251251251251244") 154 TestBothWays("rounding-up", "1.12512512512512453", "1.1251251251251246") 155 156 TestBothWays("hex-int-str", "0x20", '"32"') 157 TestBothWays("dec-int-str", "32.00", '"32"') 158 TestBothWays("exp-int-str", "3.2e1", '"32"') 159 TestBothWays("overflow-inf-str", "1e2000", '"Infinity"') 160 TestBothWays("underflow-0-str", "1e-2000", '"0"') 161 TestBothWays("non-canonical-literal-int-str", "1.0", '"1"') 162 TestBothWays("non-canonical-literal-frac-str", "1.50", '"1.5"') 163