Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-collections | 28 // Flags: --harmony-collections |
| 29 // Flags: --expose-gc --allow-natives-syntax | 29 // Flags: --expose-gc --allow-natives-syntax |
| 30 | 30 |
| 31 | 31 |
| 32 var originalSetPrototypeAdd = Set.prototype.add; | |
|
rossberg
2014/06/23 14:16:00
Nit: I'd prefer to localise these declarations int
arv (Not doing code reviews)
2014/06/23 15:43:56
Done.
| |
| 33 var originalMapPrototypeSet = Map.prototype.set; | |
| 34 | |
| 35 | |
| 32 // Test valid getter and setter calls on Sets and WeakSets | 36 // Test valid getter and setter calls on Sets and WeakSets |
| 33 function TestValidSetCalls(m) { | 37 function TestValidSetCalls(m) { |
| 34 assertDoesNotThrow(function () { m.add(new Object) }); | 38 assertDoesNotThrow(function () { m.add(new Object) }); |
| 35 assertDoesNotThrow(function () { m.has(new Object) }); | 39 assertDoesNotThrow(function () { m.has(new Object) }); |
| 36 assertDoesNotThrow(function () { m.delete(new Object) }); | 40 assertDoesNotThrow(function () { m.delete(new Object) }); |
| 37 } | 41 } |
| 38 TestValidSetCalls(new Set); | 42 TestValidSetCalls(new Set); |
| 39 TestValidSetCalls(new WeakSet); | 43 TestValidSetCalls(new WeakSet); |
| 40 | 44 |
| 41 | 45 |
| (...skipping 938 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 980 | 984 |
| 981 if (v === 4) { | 985 if (v === 4) { |
| 982 for (var i = 5; i < 16; i++) { | 986 for (var i = 5; i < 16; i++) { |
| 983 map.delete(i); | 987 map.delete(i); |
| 984 } | 988 } |
| 985 } | 989 } |
| 986 }); | 990 }); |
| 987 | 991 |
| 988 assertArrayEquals([0, 1, 2, 3, 4], buffer); | 992 assertArrayEquals([0, 1, 2, 3, 4], buffer); |
| 989 })(); | 993 })(); |
| 994 | |
| 995 | |
| 996 (function TestSetConstructor() { | |
| 997 var s = new Set(null); | |
| 998 assertEquals(s.size, 0); | |
| 999 | |
| 1000 s = new Set(undefined); | |
| 1001 assertEquals(s.size, 0); | |
| 1002 | |
| 1003 // No @@iterator | |
| 1004 assertThrows(function() { | |
| 1005 new Set({}); | |
| 1006 }, TypeError); | |
| 1007 | |
| 1008 // @@iterator not callable | |
| 1009 assertThrows(function() { | |
| 1010 var object = {}; | |
| 1011 object[Symbol.iterator] = 42; | |
| 1012 new Set(object); | |
| 1013 }, TypeError); | |
| 1014 | |
| 1015 // @@iterator result not object | |
| 1016 assertThrows(function() { | |
| 1017 var object = {}; | |
| 1018 object[Symbol.iterator] = function() { | |
| 1019 return 42; | |
| 1020 }; | |
| 1021 new Set(object); | |
| 1022 }, TypeError); | |
| 1023 | |
| 1024 var s2 = new Set(); | |
| 1025 s2.add('a'); | |
| 1026 s2.add('b'); | |
| 1027 s2.add('c'); | |
| 1028 s = new Set(s2.values()); | |
| 1029 assertEquals(s.size, 3); | |
| 1030 assertTrue(s.has('a')); | |
| 1031 assertTrue(s.has('b')); | |
| 1032 assertTrue(s.has('c')); | |
| 1033 })(); | |
| 1034 | |
| 1035 | |
| 1036 (function TestSetConstructorAddNotCallable() { | |
| 1037 var twoSet = new Set(); | |
|
rossberg
2014/06/23 14:16:00
Nit: can't you simply use the array [1, 2] here? (
arv (Not doing code reviews)
2014/06/23 15:43:56
OK. I didn't do that because I didn't want a depen
| |
| 1038 twoSet.add(1); | |
| 1039 twoSet.add(2); | |
| 1040 | |
| 1041 assertThrows(function() { | |
| 1042 Set.prototype.add = 42; | |
| 1043 new Set(twoSet); | |
| 1044 }, TypeError); | |
| 1045 Set.prototype.add = originalSetPrototypeAdd; | |
| 1046 })(); | |
| 1047 | |
| 1048 | |
| 1049 (function TestSetConstructorGetAddOnce() { | |
| 1050 var twoSet = new Set(); | |
| 1051 twoSet.add(1); | |
| 1052 twoSet.add(2); | |
| 1053 | |
| 1054 var getAddCount = 0; | |
| 1055 Object.defineProperty(Set.prototype, 'add', { | |
| 1056 get: function() { | |
| 1057 getAddCount++; | |
| 1058 return function() {}; | |
| 1059 } | |
| 1060 }); | |
| 1061 var s = new Set(twoSet); | |
| 1062 assertEquals(getAddCount, 1); | |
| 1063 assertEquals(s.size, 0); | |
| 1064 Object.defineProperty(Set.prototype, 'add', { | |
| 1065 value: originalSetPrototypeAdd, | |
| 1066 writable: true | |
| 1067 }); | |
| 1068 })(); | |
| 1069 | |
| 1070 | |
| 1071 (function TestSetConstructorAddReplaced() { | |
| 1072 var twoSet = new Set(); | |
| 1073 twoSet.add(1); | |
| 1074 twoSet.add(2); | |
| 1075 | |
| 1076 var addCount = 0; | |
| 1077 Set.prototype.add = function(value) { | |
| 1078 addCount++; | |
| 1079 originalSetPrototypeAdd.call(this, value); | |
| 1080 Set.prototype.add = null; | |
| 1081 }; | |
| 1082 | |
| 1083 var s = new Set(twoSet); | |
| 1084 assertEquals(addCount, 2); | |
| 1085 assertEquals(s.size, 2); | |
| 1086 Set.prototype.add = originalSetPrototypeAdd; | |
| 1087 })(); | |
| 1088 | |
| 1089 | |
| 1090 (function TestSetConstructorOrderOfDoneValue() { | |
| 1091 var valueCount = 0, doneCount = 0; | |
| 1092 var iterator = { | |
| 1093 next: function() { | |
| 1094 return { | |
| 1095 get value() { | |
| 1096 valueCount++; | |
| 1097 }, | |
| 1098 get done() { | |
| 1099 doneCount++; | |
| 1100 throw new Error(); | |
| 1101 } | |
| 1102 }; | |
| 1103 } | |
| 1104 }; | |
| 1105 iterator[Symbol.iterator] = function() { | |
| 1106 return this; | |
| 1107 }; | |
| 1108 assertThrows(function() { | |
| 1109 new Set(iterator); | |
| 1110 }); | |
| 1111 assertEquals(doneCount, 1); | |
| 1112 assertEquals(valueCount, 0); | |
| 1113 })(); | |
| 1114 | |
| 1115 | |
| 1116 (function TestSetConstructorNextNotAnObject() { | |
| 1117 var iterator = { | |
| 1118 next: function() { | |
| 1119 return 'abc'; | |
| 1120 } | |
| 1121 }; | |
| 1122 iterator[Symbol.iterator] = function() { | |
| 1123 return this; | |
| 1124 }; | |
| 1125 assertThrows(function() { | |
| 1126 new Set(iterator); | |
| 1127 }); | |
|
rossberg
2014/06/23 14:16:00
Assert TypeError, specifically.
arv (Not doing code reviews)
2014/06/23 15:43:57
Done.
| |
| 1128 })(); | |
| 1129 | |
| 1130 | |
| 1131 (function TestMapConstructor() { | |
| 1132 var m = new Map(null); | |
| 1133 assertEquals(m.size, 0); | |
| 1134 | |
| 1135 m = new Map(undefined); | |
| 1136 assertEquals(m.size, 0); | |
| 1137 | |
| 1138 // No @@iterator | |
| 1139 assertThrows(function() { | |
| 1140 new Map({}); | |
| 1141 }, TypeError); | |
| 1142 | |
| 1143 // @@iterator not callable | |
| 1144 assertThrows(function() { | |
| 1145 var object = {}; | |
| 1146 object[Symbol.iterator] = 42; | |
| 1147 new Map(object); | |
| 1148 }, TypeError); | |
| 1149 | |
| 1150 // @@iterator result not object | |
| 1151 assertThrows(function() { | |
| 1152 var object = {}; | |
| 1153 object[Symbol.iterator] = function() { | |
| 1154 return 42; | |
| 1155 }; | |
| 1156 new Map(object); | |
| 1157 }, TypeError); | |
| 1158 | |
| 1159 var m2 = new Map(); | |
| 1160 m2.set(0, 'a'); | |
| 1161 m2.set(1, 'b'); | |
| 1162 m2.set(2, 'c'); | |
| 1163 m = new Map(m2.entries()); | |
| 1164 assertEquals(m.size, 3); | |
| 1165 assertEquals(m.get(0), 'a'); | |
| 1166 assertEquals(m.get(1), 'b'); | |
| 1167 assertEquals(m.get(2), 'c'); | |
| 1168 })(); | |
| 1169 | |
| 1170 | |
| 1171 (function TestMapConstructorSetNotCallable() { | |
| 1172 var twoMap = new Map(); | |
| 1173 twoMap.set(1, 1); | |
| 1174 twoMap.set(2, 2); | |
| 1175 | |
| 1176 assertThrows(function() { | |
| 1177 Map.prototype.set = 42; | |
| 1178 new Map(twoMap); | |
| 1179 }, TypeError); | |
| 1180 Map.prototype.set = originalMapPrototypeSet; | |
| 1181 })(); | |
| 1182 | |
| 1183 | |
| 1184 (function TestMapConstructorGetAddOnce() { | |
| 1185 var twoMap = new Map(); | |
| 1186 twoMap.set(1, 1); | |
| 1187 twoMap.set(2, 1); | |
| 1188 | |
| 1189 var getSetCount = 0; | |
| 1190 Object.defineProperty(Map.prototype, 'set', { | |
| 1191 get: function() { | |
| 1192 getSetCount++; | |
| 1193 return function() {}; | |
| 1194 } | |
| 1195 }); | |
| 1196 var m = new Map(twoMap); | |
| 1197 assertEquals(getSetCount, 1); | |
| 1198 assertEquals(m.size, 0); | |
| 1199 Object.defineProperty(Map.prototype, 'set', { | |
| 1200 value: originalMapPrototypeSet, | |
| 1201 writable: true | |
| 1202 }); | |
| 1203 })(); | |
| 1204 | |
| 1205 | |
| 1206 (function TestMapConstructorSetReplaced() { | |
| 1207 var twoMap = new Map(); | |
| 1208 twoMap.set(1, 1); | |
| 1209 twoMap.set(2, 2); | |
| 1210 | |
| 1211 var setCount = 0; | |
| 1212 Map.prototype.set = function(key, value) { | |
| 1213 setCount++; | |
| 1214 originalMapPrototypeSet.call(this, key, value); | |
| 1215 Map.prototype.set = null; | |
| 1216 }; | |
| 1217 | |
| 1218 var m = new Map(twoMap); | |
| 1219 assertEquals(setCount, 2); | |
| 1220 assertEquals(m.size, 2); | |
| 1221 Map.prototype.set = originalMapPrototypeSet; | |
| 1222 })(); | |
| 1223 | |
| 1224 | |
| 1225 (function TestMapConstructorOrderOfDoneValue() { | |
| 1226 var valueCount = 0, doneCount = 0; | |
| 1227 var iterator = { | |
| 1228 next: function() { | |
| 1229 return { | |
| 1230 get value() { | |
| 1231 valueCount++; | |
| 1232 }, | |
| 1233 get done() { | |
| 1234 doneCount++; | |
| 1235 throw new Error(); | |
| 1236 } | |
| 1237 }; | |
| 1238 } | |
| 1239 }; | |
| 1240 iterator[Symbol.iterator] = function() { | |
| 1241 return this; | |
| 1242 }; | |
| 1243 assertThrows(function() { | |
| 1244 new Map(iterator); | |
| 1245 }); | |
| 1246 assertEquals(doneCount, 1); | |
| 1247 assertEquals(valueCount, 0); | |
| 1248 })(); | |
| 1249 | |
| 1250 | |
| 1251 (function TestMapConstructorNextNotAnObject() { | |
| 1252 var iterator = { | |
| 1253 next: function() { | |
| 1254 return 'abc'; | |
| 1255 } | |
| 1256 }; | |
| 1257 iterator[Symbol.iterator] = function() { | |
| 1258 return this; | |
| 1259 }; | |
| 1260 assertThrows(function() { | |
| 1261 new Map(iterator); | |
| 1262 }); | |
| 1263 })(); | |
| 1264 | |
| 1265 | |
| 1266 (function TestMapConstructorIteratorNotObjectValues() { | |
| 1267 var twoMap = new Map(); | |
| 1268 twoMap.set(1, 1); | |
| 1269 twoMap.set(2, 1); | |
| 1270 | |
| 1271 assertThrows(function() { | |
| 1272 new Map(twoMap.values()); | |
| 1273 }); | |
| 1274 })(); | |
| OLD | NEW |