OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony |
| 6 |
| 7 (function TestBasics() { |
| 8 var C = class C {} |
| 9 assertEquals(typeof C, 'function'); |
| 10 assertEquals(C.__proto__, Function.prototype); |
| 11 assertEquals(Object.prototype, Object.getPrototypeOf(C.prototype)); |
| 12 assertEquals(Function.prototype, Object.getPrototypeOf(C)); |
| 13 assertEquals('C', C.name); |
| 14 |
| 15 class D {} |
| 16 assertEquals(typeof D, 'function'); |
| 17 assertEquals(D.__proto__, Function.prototype); |
| 18 assertEquals(Object.prototype, Object.getPrototypeOf(D.prototype)); |
| 19 assertEquals(Function.prototype, Object.getPrototypeOf(D)); |
| 20 assertEquals('D', D.name); |
| 21 |
| 22 var E = class {} |
| 23 assertEquals('', E.name); |
| 24 })(); |
| 25 |
| 26 |
| 27 (function TestBasicsExtends() { |
| 28 class C extends null {} |
| 29 assertEquals(typeof C, 'function'); |
| 30 assertEquals(C.__proto__, Function.prototype); |
| 31 assertEquals(null, Object.getPrototypeOf(C.prototype)); |
| 32 |
| 33 class D extends C {} |
| 34 assertEquals(typeof D, 'function'); |
| 35 assertEquals(D.__proto__, C); |
| 36 assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); |
| 37 })(); |
| 38 |
| 39 |
| 40 (function TestSideEffectInExtends() { |
| 41 var calls = 0; |
| 42 class C {} |
| 43 class D extends (calls++, C) {} |
| 44 assertEquals(1, calls); |
| 45 assertEquals(typeof D, 'function'); |
| 46 assertEquals(D.__proto__, C); |
| 47 assertEquals(C.prototype, Object.getPrototypeOf(D.prototype)); |
| 48 })(); |
| 49 |
| 50 |
| 51 (function TestInvalidExtends() { |
| 52 assertThrows(function() { |
| 53 class C extends 42 {} |
| 54 }, TypeError); |
| 55 |
| 56 assertThrows(function() { |
| 57 // Function but its .prototype is not null or a function. |
| 58 class C extends Math.abs {} |
| 59 }, TypeError); |
| 60 |
| 61 assertThrows(function() { |
| 62 Math.abs.prototype = 42; |
| 63 class C extends Math.abs {} |
| 64 }, TypeError); |
| 65 delete Math.abs.prototype; |
| 66 })(); |
| 67 |
| 68 |
| 69 (function TestConstructorProperty() { |
| 70 class C {} |
| 71 assertEquals(C, C.prototype.constructor); |
| 72 var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); |
| 73 assertTrue(descr.configurable); |
| 74 assertFalse(descr.enumerable); |
| 75 assertTrue(descr.writable); |
| 76 })(); |
| 77 |
| 78 |
| 79 (function TestPrototypeProperty() { |
| 80 class C {} |
| 81 var descr = Object.getOwnPropertyDescriptor(C, 'prototype'); |
| 82 assertFalse(descr.configurable); |
| 83 assertFalse(descr.enumerable); |
| 84 assertFalse(descr.writable); |
| 85 })(); |
| 86 |
| 87 |
| 88 (function TestConstructor() { |
| 89 var count = 0; |
| 90 class C { |
| 91 constructor() { |
| 92 assertEquals(Object.getPrototypeOf(this), C.prototype); |
| 93 count++; |
| 94 } |
| 95 } |
| 96 assertEquals(C, C.prototype.constructor); |
| 97 var descr = Object.getOwnPropertyDescriptor(C.prototype, 'constructor'); |
| 98 assertTrue(descr.configurable); |
| 99 assertFalse(descr.enumerable); |
| 100 assertTrue(descr.writable); |
| 101 |
| 102 var c = new C(); |
| 103 assertEquals(1, count); |
| 104 assertEquals(Object.getPrototypeOf(c), C.prototype); |
| 105 })(); |
| 106 |
| 107 |
| 108 (function TestImplicitConstructor() { |
| 109 class C {} |
| 110 var c = new C(); |
| 111 assertEquals(Object.getPrototypeOf(c), C.prototype); |
| 112 })(); |
| 113 |
| 114 |
| 115 (function TestConstructorStrict() { |
| 116 class C { |
| 117 constructor() { |
| 118 assertThrows(function() { |
| 119 nonExistingBinding = 42; |
| 120 }, ReferenceError); |
| 121 } |
| 122 } |
| 123 new C(); |
| 124 })(); |
| 125 |
| 126 |
| 127 (function TestSuperInConstructor() { |
| 128 var calls = 0; |
| 129 class B {} |
| 130 B.prototype.x = 42; |
| 131 |
| 132 class C extends B { |
| 133 constructor() { |
| 134 calls++; |
| 135 assertEquals(42, super.x); |
| 136 } |
| 137 } |
| 138 |
| 139 new C; |
| 140 assertEquals(1, calls); |
| 141 })(); |
| 142 |
| 143 |
| 144 (function TestStrictMode() { |
| 145 class C {} |
| 146 |
| 147 with ({a: 1}) { |
| 148 assertEquals(1, a); |
| 149 } |
| 150 |
| 151 assertThrows('class C extends function B() { with ({}); return B; }() {}', |
| 152 SyntaxError); |
| 153 |
| 154 })(); |
| 155 |
| 156 /* TODO(arv): Implement |
| 157 (function TestNameBindingInConstructor() { |
| 158 class C { |
| 159 constructor() { |
| 160 assertThrows(function() { |
| 161 C = 42; |
| 162 }, ReferenceError); |
| 163 } |
| 164 } |
| 165 new C(); |
| 166 })(); |
| 167 */ |
OLD | NEW |