OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony | 5 // Flags: --harmony |
6 | 6 |
7 (function TestBasics() { | 7 (function TestBasics() { |
8 var C = class C {} | 8 var C = class C {} |
9 assertEquals(typeof C, 'function'); | 9 assertEquals(typeof C, 'function'); |
10 assertEquals(C.__proto__, Function.prototype); | 10 assertEquals(C.__proto__, Function.prototype); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 146 |
147 with ({a: 1}) { | 147 with ({a: 1}) { |
148 assertEquals(1, a); | 148 assertEquals(1, a); |
149 } | 149 } |
150 | 150 |
151 assertThrows('class C extends function B() { with ({}); return B; }() {}', | 151 assertThrows('class C extends function B() { with ({}); return B; }() {}', |
152 SyntaxError); | 152 SyntaxError); |
153 | 153 |
154 })(); | 154 })(); |
155 | 155 |
| 156 |
| 157 (function TestToString() { |
| 158 class C {} |
| 159 assertEquals('class C {}', C.toString()); |
| 160 |
| 161 class D { constructor() { 42; } } |
| 162 assertEquals('class D { constructor() { 42; } }', D.toString()); |
| 163 |
| 164 class E { x() { 42; } } |
| 165 assertEquals('class E { x() { 42; } }', E.toString()); |
| 166 })(); |
| 167 |
| 168 |
| 169 function assertMethodDescriptor(object, name) { |
| 170 var descr = Object.getOwnPropertyDescriptor(object, name); |
| 171 assertTrue(descr.configurable); |
| 172 assertTrue(descr.enumerable); |
| 173 assertTrue(descr.writable); |
| 174 assertEquals('function', typeof descr.value); |
| 175 } |
| 176 |
| 177 function assertGetterDescriptor(object, name) { |
| 178 var descr = Object.getOwnPropertyDescriptor(object, name); |
| 179 assertTrue(descr.configurable); |
| 180 assertTrue(descr.enumerable); |
| 181 assertEquals('function', typeof descr.get); |
| 182 assertEquals(undefined, descr.set); |
| 183 } |
| 184 |
| 185 |
| 186 function assertSetterDescriptor(object, name) { |
| 187 var descr = Object.getOwnPropertyDescriptor(object, name); |
| 188 assertTrue(descr.configurable); |
| 189 assertTrue(descr.enumerable); |
| 190 assertEquals(undefined, descr.get); |
| 191 assertEquals('function', typeof descr.set); |
| 192 } |
| 193 |
| 194 |
| 195 function assertAccessorDescriptor(object, name) { |
| 196 var descr = Object.getOwnPropertyDescriptor(object, name); |
| 197 assertTrue(descr.configurable); |
| 198 assertTrue(descr.enumerable); |
| 199 assertEquals('function', typeof descr.get); |
| 200 assertEquals('function', typeof descr.set); |
| 201 } |
| 202 |
| 203 |
| 204 (function TestMethods() { |
| 205 class C { |
| 206 method() { return 1; } |
| 207 static staticMethod() { return 2; } |
| 208 method2() { return 3; } |
| 209 static staticMethod2() { return 4; } |
| 210 } |
| 211 |
| 212 assertMethodDescriptor(C.prototype, 'method'); |
| 213 assertMethodDescriptor(C.prototype, 'method2'); |
| 214 assertMethodDescriptor(C, 'staticMethod'); |
| 215 assertMethodDescriptor(C, 'staticMethod2'); |
| 216 |
| 217 assertEquals(1, new C().method()); |
| 218 assertEquals(2, C.staticMethod()); |
| 219 assertEquals(3, new C().method2()); |
| 220 assertEquals(4, C.staticMethod2()); |
| 221 })(); |
| 222 |
| 223 |
| 224 (function TestGetters() { |
| 225 class C { |
| 226 get x() { return 1; } |
| 227 static get staticX() { return 2; } |
| 228 get y() { return 3; } |
| 229 static get staticY() { return 4; } |
| 230 } |
| 231 |
| 232 assertGetterDescriptor(C.prototype, 'x'); |
| 233 assertGetterDescriptor(C.prototype, 'y'); |
| 234 assertGetterDescriptor(C, 'staticX'); |
| 235 assertGetterDescriptor(C, 'staticY'); |
| 236 |
| 237 assertEquals(1, new C().x); |
| 238 assertEquals(2, C.staticX); |
| 239 assertEquals(3, new C().y); |
| 240 assertEquals(4, C.staticY); |
| 241 })(); |
| 242 |
| 243 |
| 244 |
| 245 (function TestSetters() { |
| 246 var x, staticX, y, staticY; |
| 247 class C { |
| 248 set x(v) { x = v; } |
| 249 static set staticX(v) { staticX = v; } |
| 250 set y(v) { y = v; } |
| 251 static set staticY(v) { staticY = v; } |
| 252 } |
| 253 |
| 254 assertSetterDescriptor(C.prototype, 'x'); |
| 255 assertSetterDescriptor(C.prototype, 'y'); |
| 256 assertSetterDescriptor(C, 'staticX'); |
| 257 assertSetterDescriptor(C, 'staticY'); |
| 258 |
| 259 assertEquals(1, new C().x = 1); |
| 260 assertEquals(1, x); |
| 261 assertEquals(2, C.staticX = 2); |
| 262 assertEquals(2, staticX); |
| 263 assertEquals(3, new C().y = 3); |
| 264 assertEquals(3, y); |
| 265 assertEquals(4, C.staticY = 4); |
| 266 assertEquals(4, staticY); |
| 267 })(); |
| 268 |
| 269 |
| 270 (function TestSideEffectsInPropertyDefine() { |
| 271 function B() {} |
| 272 B.prototype = { |
| 273 constructor: B, |
| 274 set m(v) { |
| 275 throw Error(); |
| 276 } |
| 277 }; |
| 278 |
| 279 class C extends B { |
| 280 m() { return 1; } |
| 281 } |
| 282 |
| 283 assertEquals(1, new C().m()); |
| 284 })(); |
| 285 |
| 286 |
| 287 (function TestAccessors() { |
| 288 class C { |
| 289 constructor(x) { |
| 290 this._x = x; |
| 291 } |
| 292 |
| 293 get x() { return this._x; } |
| 294 set x(v) { this._x = v; } |
| 295 |
| 296 static get staticX() { return this._x; } |
| 297 static set staticX(v) { this._x = v; } |
| 298 } |
| 299 |
| 300 assertAccessorDescriptor(C.prototype, 'x'); |
| 301 assertAccessorDescriptor(C, 'staticX'); |
| 302 |
| 303 var c = new C(1); |
| 304 c._x = 1; |
| 305 assertEquals(1, c.x); |
| 306 c.x = 2; |
| 307 assertEquals(2, c._x); |
| 308 |
| 309 C._x = 3; |
| 310 assertEquals(3, C.staticX); |
| 311 C._x = 4; |
| 312 assertEquals(4, C.staticX ); |
| 313 })(); |
| 314 |
| 315 |
| 316 (function TestProto() { |
| 317 class C { |
| 318 __proto__() { return 1; } |
| 319 } |
| 320 assertMethodDescriptor(C.prototype, '__proto__'); |
| 321 assertEquals(1, new C().__proto__()); |
| 322 })(); |
| 323 |
| 324 |
| 325 (function TestProtoStatic() { |
| 326 class C { |
| 327 static __proto__() { return 1; } |
| 328 } |
| 329 assertMethodDescriptor(C, '__proto__'); |
| 330 assertEquals(1, C.__proto__()); |
| 331 })(); |
| 332 |
| 333 |
| 334 (function TestProtoAccessor() { |
| 335 class C { |
| 336 get __proto__() { return this._p; } |
| 337 set __proto__(v) { this._p = v; } |
| 338 } |
| 339 assertAccessorDescriptor(C.prototype, '__proto__'); |
| 340 var c = new C(); |
| 341 c._p = 1; |
| 342 assertEquals(1, c.__proto__); |
| 343 c.__proto__ = 2; |
| 344 assertEquals(2, c.__proto__); |
| 345 })(); |
| 346 |
| 347 |
| 348 (function TestStaticProtoAccessor() { |
| 349 class C { |
| 350 static get __proto__() { return this._p; } |
| 351 static set __proto__(v) { this._p = v; } |
| 352 } |
| 353 assertAccessorDescriptor(C, '__proto__'); |
| 354 C._p = 1; |
| 355 assertEquals(1, C.__proto__); |
| 356 C.__proto__ = 2; |
| 357 assertEquals(2, C.__proto__); |
| 358 })(); |
| 359 |
| 360 |
| 361 (function TestSettersOnProto() { |
| 362 function Base() {} |
| 363 Base.prototype = { |
| 364 set constructor(_) { |
| 365 assertUnreachable(); |
| 366 }, |
| 367 set m(_) { |
| 368 assertUnreachable(); |
| 369 } |
| 370 }; |
| 371 Object.defineProperty(Base, 'staticM', { |
| 372 set: function() { |
| 373 assertUnreachable(); |
| 374 } |
| 375 }); |
| 376 |
| 377 class C extends Base { |
| 378 m() { |
| 379 return 1; |
| 380 } |
| 381 static staticM() { |
| 382 return 2; |
| 383 } |
| 384 } |
| 385 |
| 386 assertEquals(1, new C().m()); |
| 387 assertEquals(2, C.staticM()); |
| 388 })(); |
| 389 |
156 /* TODO(arv): Implement | 390 /* TODO(arv): Implement |
157 (function TestNameBindingInConstructor() { | 391 (function TestNameBindingInConstructor() { |
158 class C { | 392 class C { |
159 constructor() { | 393 constructor() { |
160 assertThrows(function() { | 394 assertThrows(function() { |
161 C = 42; | 395 C = 42; |
162 }, ReferenceError); | 396 }, ReferenceError); |
163 } | 397 } |
164 } | 398 } |
165 new C(); | 399 new C(); |
166 })(); | 400 })(); |
167 */ | 401 */ |
168 | |
169 | |
170 (function TestToString() { | |
171 class C {} | |
172 assertEquals('class C {}', C.toString()); | |
173 | |
174 class D { constructor() { 42; } } | |
175 assertEquals('class D { constructor() { 42; } }', D.toString()); | |
176 | |
177 class E { x() { 42; } } | |
178 assertEquals('class E { x() { 42; } }', E.toString()); | |
179 })(); | |
OLD | NEW |