class-basic.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. load("test-common.js");
  2. try {
  3. class X {
  4. constructor() {
  5. this.x = 3;
  6. }
  7. getX() {
  8. return 3;
  9. }
  10. init() {
  11. this.y = 3;
  12. }
  13. }
  14. assert(X.name === "X");
  15. assert(X.length === 0);
  16. class Y extends X {
  17. init() {
  18. super.init();
  19. this.y += 3;
  20. }
  21. }
  22. assert(new Y().getX() === 3);
  23. assert(new Y().x === 3);
  24. let x = new X();
  25. assert(x.x === 3);
  26. assert(x.getX() === 3);
  27. let y = new Y();
  28. assert(y.x === 3);
  29. assert(y.y === undefined);
  30. y.init();
  31. assert(y.y === 6);
  32. assert(y.hasOwnProperty("y"));
  33. class Foo {
  34. constructor(x) {
  35. this.x = x;
  36. }
  37. }
  38. assert(Foo.length === 1);
  39. class Bar extends Foo {
  40. constructor() {
  41. super(5);
  42. }
  43. }
  44. class Baz {
  45. "constructor"() {
  46. this.foo = 55;
  47. this._bar = 33;
  48. }
  49. get bar() {
  50. return this._bar;
  51. }
  52. set bar(value) {
  53. this._bar = value;
  54. }
  55. ["get" + "Foo"]() {
  56. return this.foo;
  57. }
  58. static get staticFoo() {
  59. assert(this === Baz);
  60. return 11;
  61. }
  62. }
  63. let barPropertyDescriptor = Object.getOwnPropertyDescriptor(Baz.prototype, "bar");
  64. assert(barPropertyDescriptor.get.name === "get bar");
  65. assert(barPropertyDescriptor.set.name === "set bar");
  66. let baz = new Baz();
  67. assert(baz.foo === 55);
  68. assert(baz.bar === 33);
  69. baz.bar = 22;
  70. assert(baz.bar === 22);
  71. assert(baz.getFoo() === 55);
  72. assert(Baz.staticFoo === 11);
  73. assert(new Bar().x === 5);
  74. class ExtendsFunction extends function () { this.foo = 22; } { }
  75. assert(new ExtendsFunction().foo === 22);
  76. class ExtendsString extends String { }
  77. assert(new ExtendsString() instanceof String);
  78. assert(new ExtendsString() instanceof ExtendsString);
  79. assert(new ExtendsString("abc").charAt(1) === "b");
  80. class MyWeirdString extends ExtendsString {
  81. charAt(i) {
  82. return "#" + super.charAt(i);
  83. }
  84. }
  85. assert(new MyWeirdString("abc").charAt(1) === "#b")
  86. class ExtendsNull extends null { }
  87. assertThrowsError(() => {
  88. new ExtendsNull();
  89. }, {
  90. error: ReferenceError
  91. });
  92. assert(Object.getPrototypeOf(ExtendsNull.prototype) === null);
  93. class ExtendsClassExpression extends class { constructor(x) { this.x = x; } } {
  94. constructor(y) {
  95. super(5);
  96. this.y = 6;
  97. }
  98. }
  99. let extendsClassExpression = new ExtendsClassExpression();
  100. assert(extendsClassExpression.x === 5);
  101. assert(extendsClassExpression.y === 6);
  102. class InStrictMode {
  103. constructor() {
  104. assert(isStrictMode());
  105. }
  106. method() {
  107. assert(isStrictMode());
  108. }
  109. }
  110. let resultOfAnExpression = new (class {
  111. constructor(x) {
  112. this.x = x;
  113. }
  114. getX() {
  115. return this.x + 10;
  116. }
  117. })(55);
  118. assert(resultOfAnExpression.x === 55);
  119. assert(resultOfAnExpression.getX() === 65);
  120. let ClassExpression = class Foo { };
  121. assert(ClassExpression.name === "Foo");
  122. new InStrictMode().method();
  123. assert(!isStrictMode());
  124. assertIsSyntaxError(`
  125. class GetterWithArgument {
  126. get foo(bar) {
  127. return 0;
  128. }
  129. }
  130. `);
  131. assertIsSyntaxError(`
  132. class SetterWithNoArgumetns {
  133. set foo() {
  134. }
  135. }
  136. `);
  137. assertIsSyntaxError(`
  138. class SetterWithMoreThanOneArgument {
  139. set foo(bar, baz) {
  140. }
  141. }
  142. `);
  143. assertIsSyntaxError(`
  144. class FooBase {}
  145. class IsASyntaxError extends FooBase {
  146. bar() {
  147. function f() { super.baz; }
  148. }
  149. }
  150. `);
  151. assertIsSyntaxError(`
  152. class NoBaseSuper {
  153. constructor() {
  154. super();
  155. }
  156. }
  157. `);
  158. assertThrowsError(() => {
  159. class BadExtends extends 3 { }
  160. }, {
  161. error: TypeError
  162. });
  163. assertThrowsError(() => {
  164. class BadExtends extends undefined { }
  165. }, {
  166. error: TypeError
  167. });
  168. class SuperNotASyntaxError {
  169. bar() {
  170. () => { super.baz };
  171. }
  172. }
  173. class SuperNoBasePropertyLookup {
  174. constructor() {
  175. super.foo;
  176. }
  177. }
  178. assertThrowsError(() => {
  179. class Base { }
  180. class DerivedDoesntCallSuper extends Base {
  181. constructor() {
  182. this;
  183. }
  184. }
  185. new DerivedDoesntCallSuper();
  186. }, {
  187. error: ReferenceError,
  188. });
  189. assertThrowsError(() => {
  190. class Base { }
  191. class CallsSuperTwice extends Base {
  192. constructor() {
  193. super();
  194. super();
  195. }
  196. }
  197. new CallsSuperTwice();
  198. }, {
  199. error: ReferenceError,
  200. });
  201. console.log("PASS");
  202. } catch (e) {
  203. console.log("FAIL: " + e);
  204. }