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