Skip to content

Commit 9293258

Browse files
committed
add more super class specs
1 parent 7aa63b2 commit 9293258

File tree

4 files changed

+173
-119
lines changed

4 files changed

+173
-119
lines changed

moonscript/cmd/lint.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ do
121121
end,
122122
render = function(self, ...)
123123
self:lint_check_unused()
124-
return _class_0.__parent.render(self, ...)
124+
return _class_0.__parent.__base.render(self, ...)
125125
end,
126126
block = function(self, ...)
127127
do
128-
local _with_0 = _class_0.__parent.block(self, ...)
128+
local _with_0 = _class_0.__parent.__base.block(self, ...)
129129
_with_0.block = self.block
130130
_with_0.render = self.render
131131
_with_0.get_root_block = self.get_root_block

spec/class_spec.moon

Lines changed: 168 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -40,61 +40,6 @@ describe "class", ->
4040
instance = Thing "color"
4141
assert.same instance\get_property!, "green"
4242

43-
it "should call super constructor", ->
44-
class Base
45-
new: (@property) =>
46-
47-
class Thing extends Base
48-
new: (@name) =>
49-
super "name"
50-
51-
instance = Thing "the_thing"
52-
53-
assert.same instance.property, "name"
54-
assert.same instance.name, "the_thing"
55-
56-
it "should call super method", ->
57-
class Base
58-
_count: 111
59-
counter: => @_count
60-
61-
class Thing extends Base
62-
counter: => "%08d"\format super!
63-
64-
instance = Thing!
65-
assert.same instance\counter!, "00000111"
66-
67-
it "should call other method from super", ->
68-
class Base
69-
_count: 111
70-
counter: =>
71-
@_count
72-
73-
class Thing extends Base
74-
other_method: => super\counter!
75-
76-
instance = Thing!
77-
assert.same instance\other_method!, 111
78-
79-
it "should get super class", ->
80-
class Base
81-
class Thing extends Base
82-
get_super: => super
83-
84-
instance = Thing!
85-
assert.is_true instance\get_super! == Base
86-
87-
it "should get a bound method from super", ->
88-
class Base
89-
count: 1
90-
get_count: => @count
91-
92-
class Thing extends Base
93-
get_count: => "this is wrong"
94-
get_method: => super\get_count
95-
96-
instance = Thing!
97-
assert.same instance\get_method!!, 1
9843

9944
it "should have class properties", ->
10045
class Base
@@ -151,86 +96,195 @@ describe "class", ->
15196

15297
assert.same "hello", Thing.prop
15398

154-
it "class properties take precedence in super class over base", ->
155-
class Thing
156-
@prop: "hello"
157-
prop: "world"
99+
describe "super", ->
100+
it "should call super constructor", ->
101+
class Base
102+
new: (@property) =>
158103

159-
class OtherThing extends Thing
104+
class Thing extends Base
105+
new: (@name) =>
106+
super "name"
160107

161-
assert.same "hello", OtherThing.prop
108+
instance = Thing "the_thing"
162109

163-
it "gets value from base in super class", ->
164-
class Thing
165-
prop: "world"
110+
assert.same instance.property, "name"
111+
assert.same instance.name, "the_thing"
166112

167-
class OtherThing extends Thing
168-
assert.same "world", OtherThing.prop
113+
it "should call super method", ->
114+
class Base
115+
_count: 111
116+
counter: => @_count
169117

170-
it "should let parent be replaced on class", ->
171-
class A
172-
@prop: "yeah"
173-
cool: => 1234
174-
plain: => "a"
118+
class Thing extends Base
119+
counter: => "%08d"\format super!
175120

176-
class B
177-
@prop: "okay"
178-
cool: => 9999
179-
plain: => "b"
121+
instance = Thing!
122+
assert.same instance\counter!, "00000111"
180123

181-
class Thing extends A
182-
cool: =>
183-
super! + 1
124+
it "should call other method from super", ->
125+
class Base
126+
_count: 111
127+
counter: =>
128+
@_count
184129

185-
get_super: =>
186-
super
130+
class Thing extends Base
131+
other_method: => super\counter!
187132

188-
instance = Thing!
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"
189245

190-
assert.same "a", instance\plain!
191-
assert.same 1235, instance\cool!
192-
assert A == instance\get_super!, "expected super to be B"
246+
@doit: =>
247+
"class"
193248

194-
Thing.__parent = B
195-
setmetatable Thing.__base, B.__base
249+
class One extends Base
250+
doit: => super!
251+
@doit: => super!
196252

197-
assert.same "b", instance\plain!
198-
assert.same 10000, instance\cool!
199-
assert B == instance\get_super!, "expected super to be B"
253+
assert.same "instance", One!\doit!
254+
assert.same "class", One\doit!
200255

201-
it "should resolve many levels of super", ->
202-
class One
203-
a: =>
204-
1
205256

206-
class Two extends One
207-
a: =>
208-
super! + 2
257+
it "should resolve many levels of super on class methods", ->
258+
class One
259+
@a: =>
260+
1
209261

210-
class Three extends Two
211-
a: =>
212-
super! + 3
262+
class Two extends One
213263

214-
i = Three!
264+
class Three extends Two
265+
@a: =>
266+
super! + 3
215267

216-
assert.same 6, i\a!
268+
class Four extends Three
269+
@a: =>
270+
super! + 4
217271

272+
assert.same 8, Four\a!
218273

219-
it "should resolve many levels of super with a gap", ->
220-
class One
221-
a: =>
222-
1
274+
it "super should still work when method wrapped", ->
275+
add_some = (opts) ->
276+
=> opts.amount + opts[1] @
223277

224-
class Two extends One
278+
class Base
279+
value: => 1
225280

226-
class Three extends Two
227-
a: =>
228-
super! + 3
281+
class Sub extends Base
282+
value: add_some {
283+
amount: 12
284+
=>
285+
super! + 100
286+
}
229287

230-
class Four extends Three
231-
a: =>
232-
super! + 4
288+
assert.same 1 + 100 + 12, Sub!\value!
233289

234-
i = Four!
235290

236-
assert.same 8, i\a!

spec/inputs/class.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class Wowha extends Thing
202202
super\hello
203203

204204

205-
@butt: cool {
205+
@zone: cool {
206206
->
207207
super!
208208
super.hello

spec/outputs/class.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,9 +893,9 @@ do
893893
return _fn_0(self, ...)
894894
end
895895
end
896-
self.butt = cool({
896+
self.zone = cool({
897897
function()
898-
_class_0.__parent.butt(self)
898+
_class_0.__parent.zone(self)
899899
_ = _class_0.__parent.hello
900900
_class_0.__parent.hello(self)
901901
local _base_1 = _class_0.__parent

0 commit comments

Comments
 (0)