diff options
Diffstat (limited to 'spec/inputs/test')
-rw-r--r-- | spec/inputs/test/class_spec.yue | 297 | ||||
-rw-r--r-- | spec/inputs/test/loops_spec.yue | 9 |
2 files changed, 306 insertions, 0 deletions
diff --git a/spec/inputs/test/class_spec.yue b/spec/inputs/test/class_spec.yue new file mode 100644 index 0000000..394c59b --- /dev/null +++ b/spec/inputs/test/class_spec.yue | |||
@@ -0,0 +1,297 @@ | |||
1 | |||
2 | describe "class", -> | ||
3 | it "should make a class with constructor", -> | ||
4 | class Thing | ||
5 | new: => | ||
6 | @color = "blue" | ||
7 | |||
8 | instance = Thing! | ||
9 | |||
10 | assert.same instance, { color: "blue" } | ||
11 | |||
12 | it "should have instance methods", -> | ||
13 | class Thing | ||
14 | get_color: => @color | ||
15 | |||
16 | new: => | ||
17 | @color = "blue" | ||
18 | |||
19 | instance = Thing! | ||
20 | assert.same instance\get_color!, "blue" | ||
21 | |||
22 | it "should have base properies from class", -> | ||
23 | class Thing | ||
24 | color: "blue" | ||
25 | get_color: => @color | ||
26 | |||
27 | instance = Thing! | ||
28 | assert.same instance\get_color!, "blue" | ||
29 | assert.same Thing.color, "blue" | ||
30 | |||
31 | it "should inherit another class", -> | ||
32 | class Base | ||
33 | get_property: => @[@property] | ||
34 | |||
35 | new: (@property) => | ||
36 | |||
37 | class Thing extends Base | ||
38 | color: "green" | ||
39 | |||
40 | instance = Thing "color" | ||
41 | assert.same instance\get_property!, "green" | ||
42 | |||
43 | |||
44 | it "should have class properties", -> | ||
45 | class Base | ||
46 | class Thing extends Base | ||
47 | |||
48 | instance = Thing! | ||
49 | |||
50 | assert.same Base.__name, "Base" | ||
51 | assert.same Thing.__name, "Thing" | ||
52 | assert.is_true Thing.__parent == Base | ||
53 | |||
54 | assert.is_true instance.__class == Thing | ||
55 | |||
56 | it "should have name when assigned", -> | ||
57 | Thing = class | ||
58 | assert.same Thing.__name, "Thing" | ||
59 | |||
60 | it "should not expose class properties on instance", -> | ||
61 | class Thing | ||
62 | @height: 10 | ||
63 | |||
64 | Thing.color = "blue" | ||
65 | |||
66 | instance = Thing! | ||
67 | assert.same instance.color, nil | ||
68 | assert.same instance.height, nil | ||
69 | |||
70 | it "should expose new things added to __base", -> | ||
71 | class Thing | ||
72 | |||
73 | instance = Thing! | ||
74 | Thing.__base.color = "green" | ||
75 | |||
76 | assert.same instance.color, "green" | ||
77 | |||
78 | it "should call with correct receiver", -> | ||
79 | local instance | ||
80 | |||
81 | class Thing | ||
82 | is_class: => assert.is_true @ == Thing | ||
83 | is_instance: => assert.is_true @ == instance | ||
84 | |||
85 | go: => | ||
86 | @@is_class! | ||
87 | @is_instance! | ||
88 | |||
89 | instance = Thing! | ||
90 | instance\go! | ||
91 | |||
92 | it "should have class properies take precedence over base properties", -> | ||
93 | class Thing | ||
94 | @prop: "hello" | ||
95 | prop: "world" | ||
96 | |||
97 | assert.same "hello", Thing.prop | ||
98 | |||
99 | describe "super", -> | ||
100 | it "should call super constructor", -> | ||
101 | class Base | ||
102 | new: (@property) => | ||
103 | |||
104 | class Thing extends Base | ||
105 | new: (@name) => | ||
106 | super "name" | ||
107 | |||
108 | instance = Thing "the_thing" | ||
109 | |||
110 | assert.same instance.property, "name" | ||
111 | assert.same instance.name, "the_thing" | ||
112 | |||
113 | it "should call super method", -> | ||
114 | class Base | ||
115 | _count: 111 | ||
116 | counter: => @_count | ||
117 | |||
118 | class Thing extends Base | ||
119 | counter: => "%08d"\format super! | ||
120 | |||
121 | instance = Thing! | ||
122 | assert.same instance\counter!, "00000111" | ||
123 | |||
124 | it "should call other method from super", -> | ||
125 | class Base | ||
126 | _count: 111 | ||
127 | counter: => | ||
128 | @_count | ||
129 | |||
130 | class Thing extends Base | ||
131 | other_method: => super\counter! | ||
132 | |||
133 | instance = Thing! | ||
134 | assert.same instance\other_method!, 111 | ||
135 | |||
136 | it "should get super class", -> | ||
137 | class Base | ||
138 | class Thing extends Base | ||
139 | get_super: => super | ||
140 | |||
141 | instance = Thing! | ||
142 | assert.is_true instance\get_super! == Base | ||
143 | |||
144 | it "should get a bound method from super", -> | ||
145 | class Base | ||
146 | count: 1 | ||
147 | get_count: => @count | ||
148 | |||
149 | class Thing extends Base | ||
150 | get_count: => "this is wrong" | ||
151 | get_method: => super\get_count | ||
152 | |||
153 | instance = Thing! | ||
154 | assert.same instance\get_method!!, 1 | ||
155 | |||
156 | it "class properties take precedence in super class over base", -> | ||
157 | class Thing | ||
158 | @prop: "hello" | ||
159 | prop: "world" | ||
160 | |||
161 | class OtherThing extends Thing | ||
162 | |||
163 | assert.same "hello", OtherThing.prop | ||
164 | |||
165 | it "gets value from base in super class", -> | ||
166 | class Thing | ||
167 | prop: "world" | ||
168 | |||
169 | class OtherThing extends Thing | ||
170 | assert.same "world", OtherThing.prop | ||
171 | |||
172 | it "should let parent be replaced on class", -> | ||
173 | class A | ||
174 | @prop: "yeah" | ||
175 | cool: => 1234 | ||
176 | plain: => "a" | ||
177 | |||
178 | class B | ||
179 | @prop: "okay" | ||
180 | cool: => 9999 | ||
181 | plain: => "b" | ||
182 | |||
183 | class Thing extends A | ||
184 | cool: => | ||
185 | super! + 1 | ||
186 | |||
187 | get_super: => | ||
188 | super | ||
189 | |||
190 | instance = Thing! | ||
191 | |||
192 | assert.same "a", instance\plain! | ||
193 | assert.same 1235, instance\cool! | ||
194 | assert A == instance\get_super!, "expected super to be B" | ||
195 | |||
196 | Thing.__parent = B | ||
197 | setmetatable Thing.__base, B.__base | ||
198 | |||
199 | assert.same "b", instance\plain! | ||
200 | assert.same 10000, instance\cool! | ||
201 | assert B == instance\get_super!, "expected super to be B" | ||
202 | |||
203 | it "should resolve many levels of super", -> | ||
204 | class One | ||
205 | a: => | ||
206 | 1 | ||
207 | |||
208 | class Two extends One | ||
209 | a: => | ||
210 | super! + 2 | ||
211 | |||
212 | class Three extends Two | ||
213 | a: => | ||
214 | super! + 3 | ||
215 | |||
216 | i = Three! | ||
217 | |||
218 | assert.same 6, i\a! | ||
219 | |||
220 | |||
221 | it "should resolve many levels of super with a gap", -> | ||
222 | class One | ||
223 | a: => | ||
224 | 1 | ||
225 | |||
226 | class Two extends One | ||
227 | |||
228 | class Three extends Two | ||
229 | a: => | ||
230 | super! + 3 | ||
231 | |||
232 | class Four extends Three | ||
233 | a: => | ||
234 | super! + 4 | ||
235 | |||
236 | i = Four! | ||
237 | |||
238 | assert.same 8, i\a! | ||
239 | |||
240 | |||
241 | it "should call correct class/instance super methods", -> | ||
242 | class Base | ||
243 | doit: => | ||
244 | "instance" | ||
245 | |||
246 | @doit: => | ||
247 | "class" | ||
248 | |||
249 | class One extends Base | ||
250 | doit: => super! | ||
251 | @doit: => super! | ||
252 | |||
253 | assert.same "instance", One!\doit! | ||
254 | assert.same "class", One\doit! | ||
255 | |||
256 | |||
257 | it "should resolve many levels of super on class methods", -> | ||
258 | class One | ||
259 | @a: => | ||
260 | 1 | ||
261 | |||
262 | class Two extends One | ||
263 | |||
264 | class Three extends Two | ||
265 | @a: => | ||
266 | super! + 3 | ||
267 | |||
268 | class Four extends Three | ||
269 | @a: => | ||
270 | super! + 4 | ||
271 | |||
272 | assert.same 8, Four\a! | ||
273 | |||
274 | it "super should still work when method wrapped", -> | ||
275 | add_some = (opts) -> | ||
276 | => opts.amount + opts[1] @ | ||
277 | |||
278 | class Base | ||
279 | value: => 1 | ||
280 | |||
281 | class Sub extends Base | ||
282 | value: add_some { | ||
283 | amount: 12 | ||
284 | => | ||
285 | super! + 100 | ||
286 | } | ||
287 | |||
288 | class OtherSub extends Base | ||
289 | value: if true | ||
290 | => 5 + super! | ||
291 | else | ||
292 | => 2 + super! | ||
293 | |||
294 | assert.same 1 + 100 + 12, Sub!\value! | ||
295 | assert.same 6, OtherSub!\value! | ||
296 | |||
297 | |||
diff --git a/spec/inputs/test/loops_spec.yue b/spec/inputs/test/loops_spec.yue new file mode 100644 index 0000000..68b5f97 --- /dev/null +++ b/spec/inputs/test/loops_spec.yue | |||
@@ -0,0 +1,9 @@ | |||
1 | |||
2 | describe "loops", -> | ||
3 | it "should continue", -> | ||
4 | input = {1,2,3,4,5,6} | ||
5 | output = for x in *input | ||
6 | continue if x % 2 == 1 | ||
7 | x | ||
8 | |||
9 | assert.same output, { 2,4,6 } | ||