OLD | NEW |
| (Empty) |
1 <sky> | |
2 <import src="../resources/chai.sky" /> | |
3 <import src="../resources/mocha.sky" /> | |
4 <script> | |
5 describe("ES6 classes", function() { | |
6 it("should create instances", function() { | |
7 class Example {} | |
8 var instance = new Example(); | |
9 assert.instanceOf(instance, Example); | |
10 }); | |
11 | |
12 it("should create subclasses", function() { | |
13 class Parent {} | |
14 class Child extends Parent {} | |
15 var instance = new Child(); | |
16 assert.instanceOf(instance, Child); | |
17 assert.instanceOf(instance, Parent); | |
18 }); | |
19 | |
20 it("should create anonymous classes", function() { | |
21 var Parent = class {} | |
22 var Child = class extends Parent {} | |
23 var instance = new Child(); | |
24 assert.instanceOf(instance, Child); | |
25 assert.instanceOf(instance, Parent); | |
26 }); | |
27 | |
28 it("should put methods on the prototype", function() { | |
29 class Test { | |
30 exampleMethod() { } | |
31 } | |
32 var instance = new Test(); | |
33 assert.isFalse(instance.hasOwnProperty("exampleMethod")); | |
34 var proto = Object.getPrototypeOf(instance); | |
35 assert.isTrue(proto.hasOwnProperty("exampleMethod")); | |
36 }); | |
37 | |
38 it("should call methods with |this|", function() { | |
39 class Adder { | |
40 constructor(value) { | |
41 this.value = value; | |
42 } | |
43 add(other) { | |
44 return this.value + other; | |
45 } | |
46 } | |
47 | |
48 var adder = new Adder(10); | |
49 assert.equal(adder.add(15), 25); | |
50 }); | |
51 | |
52 it("should let toMethod rebind super", function() { | |
53 var getValue = function() { | |
54 assert.isFunction(this.value); | |
55 assert.isFunction(this.superValue); | |
56 return this.value() + super.superValue(); | |
57 } | |
58 | |
59 class HolderParent { | |
60 superValue() { | |
61 return 5; | |
62 } | |
63 } | |
64 | |
65 class Holder extends HolderParent { | |
66 constructor() { | |
67 this.value_ = 5; | |
68 } | |
69 value() { | |
70 return this.value_; | |
71 } | |
72 } | |
73 | |
74 var holder = new Holder(); | |
75 var getValueMethod = getValue.toMethod(Holder.prototype); | |
76 assert.equal(getValueMethod.call(holder), 10); | |
77 }); | |
78 | |
79 it("should support super() constructor calls", function() { | |
80 class Parent { | |
81 constructor(value) { | |
82 this.value = value; | |
83 } | |
84 } | |
85 | |
86 class Child extends Parent { | |
87 constructor(value) { | |
88 super(value + 5); | |
89 } | |
90 } | |
91 | |
92 var child = new Child(10); | |
93 assert.equal(child.value, 15); | |
94 }); | |
95 | |
96 it("should automatically call super() in default constructor", function() { | |
97 class Parent { | |
98 constructor() { | |
99 this.value = 10; | |
100 } | |
101 } | |
102 | |
103 class Child extends Parent { | |
104 } | |
105 | |
106 var child = new Child(); | |
107 assert.equal(child.value, 10); | |
108 }); | |
109 | |
110 it("should call super.method()", function() { | |
111 class Parent { | |
112 value() { | |
113 return 10; | |
114 } | |
115 } | |
116 | |
117 class Child extends Parent { | |
118 value() { | |
119 return super.value() + 5; | |
120 } | |
121 } | |
122 | |
123 var child = new Child(); | |
124 assert.equal(child.value(), 15); | |
125 }); | |
126 | |
127 it("should support getters and setters", function() { | |
128 class Variable { | |
129 constructor(value) { | |
130 this.value_ = value; | |
131 } | |
132 | |
133 get value() { | |
134 return this.value_; | |
135 } | |
136 | |
137 set value(newValue) { | |
138 this.value_ = newValue; | |
139 } | |
140 } | |
141 | |
142 var variable = new Variable("first"); | |
143 | |
144 // Methods are on the prototype. | |
145 var proto = Object.getPrototypeOf(variable); | |
146 var descriptor = Object.getOwnPropertyDescriptor(proto, "value"); | |
147 assert.isObject(descriptor); | |
148 assert.isFunction(descriptor.get); | |
149 assert.isFunction(descriptor.set); | |
150 assert.isUndefined(descriptor.value); | |
151 | |
152 // Getter and setter should not be on the instance. | |
153 assert.isUndefined(Object.getOwnPropertyDescriptor(variable, "value")); | |
154 | |
155 assert.equal(variable.value, "first"); | |
156 assert.equal(variable.value_, "first"); | |
157 variable.value = "second"; | |
158 assert.equal(variable.value, "second"); | |
159 assert.equal(variable.value_, "second"); | |
160 }); | |
161 }); | |
162 </script> | |
163 </sky> | |
OLD | NEW |