mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
add more super class specs
This commit is contained in:
parent
7aa63b2b61
commit
92932584b4
@ -121,11 +121,11 @@ do
|
|||||||
end,
|
end,
|
||||||
render = function(self, ...)
|
render = function(self, ...)
|
||||||
self:lint_check_unused()
|
self:lint_check_unused()
|
||||||
return _class_0.__parent.render(self, ...)
|
return _class_0.__parent.__base.render(self, ...)
|
||||||
end,
|
end,
|
||||||
block = function(self, ...)
|
block = function(self, ...)
|
||||||
do
|
do
|
||||||
local _with_0 = _class_0.__parent.block(self, ...)
|
local _with_0 = _class_0.__parent.__base.block(self, ...)
|
||||||
_with_0.block = self.block
|
_with_0.block = self.block
|
||||||
_with_0.render = self.render
|
_with_0.render = self.render
|
||||||
_with_0.get_root_block = self.get_root_block
|
_with_0.get_root_block = self.get_root_block
|
||||||
|
@ -40,61 +40,6 @@ describe "class", ->
|
|||||||
instance = Thing "color"
|
instance = Thing "color"
|
||||||
assert.same instance\get_property!, "green"
|
assert.same instance\get_property!, "green"
|
||||||
|
|
||||||
it "should call super constructor", ->
|
|
||||||
class Base
|
|
||||||
new: (@property) =>
|
|
||||||
|
|
||||||
class Thing extends Base
|
|
||||||
new: (@name) =>
|
|
||||||
super "name"
|
|
||||||
|
|
||||||
instance = Thing "the_thing"
|
|
||||||
|
|
||||||
assert.same instance.property, "name"
|
|
||||||
assert.same instance.name, "the_thing"
|
|
||||||
|
|
||||||
it "should call super method", ->
|
|
||||||
class Base
|
|
||||||
_count: 111
|
|
||||||
counter: => @_count
|
|
||||||
|
|
||||||
class Thing extends Base
|
|
||||||
counter: => "%08d"\format super!
|
|
||||||
|
|
||||||
instance = Thing!
|
|
||||||
assert.same instance\counter!, "00000111"
|
|
||||||
|
|
||||||
it "should call other method from super", ->
|
|
||||||
class Base
|
|
||||||
_count: 111
|
|
||||||
counter: =>
|
|
||||||
@_count
|
|
||||||
|
|
||||||
class Thing extends Base
|
|
||||||
other_method: => super\counter!
|
|
||||||
|
|
||||||
instance = Thing!
|
|
||||||
assert.same instance\other_method!, 111
|
|
||||||
|
|
||||||
it "should get super class", ->
|
|
||||||
class Base
|
|
||||||
class Thing extends Base
|
|
||||||
get_super: => super
|
|
||||||
|
|
||||||
instance = Thing!
|
|
||||||
assert.is_true instance\get_super! == Base
|
|
||||||
|
|
||||||
it "should get a bound method from super", ->
|
|
||||||
class Base
|
|
||||||
count: 1
|
|
||||||
get_count: => @count
|
|
||||||
|
|
||||||
class Thing extends Base
|
|
||||||
get_count: => "this is wrong"
|
|
||||||
get_method: => super\get_count
|
|
||||||
|
|
||||||
instance = Thing!
|
|
||||||
assert.same instance\get_method!!, 1
|
|
||||||
|
|
||||||
it "should have class properties", ->
|
it "should have class properties", ->
|
||||||
class Base
|
class Base
|
||||||
@ -151,86 +96,195 @@ describe "class", ->
|
|||||||
|
|
||||||
assert.same "hello", Thing.prop
|
assert.same "hello", Thing.prop
|
||||||
|
|
||||||
it "class properties take precedence in super class over base", ->
|
describe "super", ->
|
||||||
class Thing
|
it "should call super constructor", ->
|
||||||
@prop: "hello"
|
class Base
|
||||||
prop: "world"
|
new: (@property) =>
|
||||||
|
|
||||||
class OtherThing extends Thing
|
class Thing extends Base
|
||||||
|
new: (@name) =>
|
||||||
|
super "name"
|
||||||
|
|
||||||
assert.same "hello", OtherThing.prop
|
instance = Thing "the_thing"
|
||||||
|
|
||||||
it "gets value from base in super class", ->
|
assert.same instance.property, "name"
|
||||||
class Thing
|
assert.same instance.name, "the_thing"
|
||||||
prop: "world"
|
|
||||||
|
|
||||||
class OtherThing extends Thing
|
it "should call super method", ->
|
||||||
assert.same "world", OtherThing.prop
|
class Base
|
||||||
|
_count: 111
|
||||||
|
counter: => @_count
|
||||||
|
|
||||||
it "should let parent be replaced on class", ->
|
class Thing extends Base
|
||||||
class A
|
counter: => "%08d"\format super!
|
||||||
@prop: "yeah"
|
|
||||||
cool: => 1234
|
|
||||||
plain: => "a"
|
|
||||||
|
|
||||||
class B
|
instance = Thing!
|
||||||
@prop: "okay"
|
assert.same instance\counter!, "00000111"
|
||||||
cool: => 9999
|
|
||||||
plain: => "b"
|
|
||||||
|
|
||||||
class Thing extends A
|
it "should call other method from super", ->
|
||||||
cool: =>
|
class Base
|
||||||
super! + 1
|
_count: 111
|
||||||
|
counter: =>
|
||||||
|
@_count
|
||||||
|
|
||||||
get_super: =>
|
class Thing extends Base
|
||||||
super
|
other_method: => super\counter!
|
||||||
|
|
||||||
instance = Thing!
|
instance = Thing!
|
||||||
|
assert.same instance\other_method!, 111
|
||||||
|
|
||||||
assert.same "a", instance\plain!
|
it "should get super class", ->
|
||||||
assert.same 1235, instance\cool!
|
class Base
|
||||||
assert A == instance\get_super!, "expected super to be B"
|
class Thing extends Base
|
||||||
|
get_super: => super
|
||||||
|
|
||||||
Thing.__parent = B
|
instance = Thing!
|
||||||
setmetatable Thing.__base, B.__base
|
assert.is_true instance\get_super! == Base
|
||||||
|
|
||||||
assert.same "b", instance\plain!
|
it "should get a bound method from super", ->
|
||||||
assert.same 10000, instance\cool!
|
class Base
|
||||||
assert B == instance\get_super!, "expected super to be B"
|
count: 1
|
||||||
|
get_count: => @count
|
||||||
|
|
||||||
it "should resolve many levels of super", ->
|
class Thing extends Base
|
||||||
class One
|
get_count: => "this is wrong"
|
||||||
a: =>
|
get_method: => super\get_count
|
||||||
1
|
|
||||||
|
|
||||||
class Two extends One
|
instance = Thing!
|
||||||
a: =>
|
assert.same instance\get_method!!, 1
|
||||||
super! + 2
|
|
||||||
|
|
||||||
class Three extends Two
|
it "class properties take precedence in super class over base", ->
|
||||||
a: =>
|
class Thing
|
||||||
super! + 3
|
@prop: "hello"
|
||||||
|
prop: "world"
|
||||||
|
|
||||||
i = Three!
|
class OtherThing extends Thing
|
||||||
|
|
||||||
assert.same 6, i\a!
|
assert.same "hello", OtherThing.prop
|
||||||
|
|
||||||
|
it "gets value from base in super class", ->
|
||||||
|
class Thing
|
||||||
|
prop: "world"
|
||||||
|
|
||||||
|
class OtherThing extends Thing
|
||||||
|
assert.same "world", OtherThing.prop
|
||||||
|
|
||||||
|
it "should let parent be replaced on class", ->
|
||||||
|
class A
|
||||||
|
@prop: "yeah"
|
||||||
|
cool: => 1234
|
||||||
|
plain: => "a"
|
||||||
|
|
||||||
|
class B
|
||||||
|
@prop: "okay"
|
||||||
|
cool: => 9999
|
||||||
|
plain: => "b"
|
||||||
|
|
||||||
|
class Thing extends A
|
||||||
|
cool: =>
|
||||||
|
super! + 1
|
||||||
|
|
||||||
|
get_super: =>
|
||||||
|
super
|
||||||
|
|
||||||
|
instance = Thing!
|
||||||
|
|
||||||
|
assert.same "a", instance\plain!
|
||||||
|
assert.same 1235, instance\cool!
|
||||||
|
assert A == instance\get_super!, "expected super to be B"
|
||||||
|
|
||||||
|
Thing.__parent = B
|
||||||
|
setmetatable Thing.__base, B.__base
|
||||||
|
|
||||||
|
assert.same "b", instance\plain!
|
||||||
|
assert.same 10000, instance\cool!
|
||||||
|
assert B == instance\get_super!, "expected super to be B"
|
||||||
|
|
||||||
|
it "should resolve many levels of super", ->
|
||||||
|
class One
|
||||||
|
a: =>
|
||||||
|
1
|
||||||
|
|
||||||
|
class Two extends One
|
||||||
|
a: =>
|
||||||
|
super! + 2
|
||||||
|
|
||||||
|
class Three extends Two
|
||||||
|
a: =>
|
||||||
|
super! + 3
|
||||||
|
|
||||||
|
i = Three!
|
||||||
|
|
||||||
|
assert.same 6, i\a!
|
||||||
|
|
||||||
|
|
||||||
it "should resolve many levels of super with a gap", ->
|
it "should resolve many levels of super with a gap", ->
|
||||||
class One
|
class One
|
||||||
a: =>
|
a: =>
|
||||||
1
|
1
|
||||||
|
|
||||||
class Two extends One
|
class Two extends One
|
||||||
|
|
||||||
class Three extends Two
|
class Three extends Two
|
||||||
a: =>
|
a: =>
|
||||||
super! + 3
|
super! + 3
|
||||||
|
|
||||||
class Four extends Three
|
class Four extends Three
|
||||||
a: =>
|
a: =>
|
||||||
super! + 4
|
super! + 4
|
||||||
|
|
||||||
|
i = Four!
|
||||||
|
|
||||||
|
assert.same 8, i\a!
|
||||||
|
|
||||||
|
|
||||||
|
it "should call correct class/instance super methods", ->
|
||||||
|
class Base
|
||||||
|
doit: =>
|
||||||
|
"instance"
|
||||||
|
|
||||||
|
@doit: =>
|
||||||
|
"class"
|
||||||
|
|
||||||
|
class One extends Base
|
||||||
|
doit: => super!
|
||||||
|
@doit: => super!
|
||||||
|
|
||||||
|
assert.same "instance", One!\doit!
|
||||||
|
assert.same "class", One\doit!
|
||||||
|
|
||||||
|
|
||||||
|
it "should resolve many levels of super on class methods", ->
|
||||||
|
class One
|
||||||
|
@a: =>
|
||||||
|
1
|
||||||
|
|
||||||
|
class Two extends One
|
||||||
|
|
||||||
|
class Three extends Two
|
||||||
|
@a: =>
|
||||||
|
super! + 3
|
||||||
|
|
||||||
|
class Four extends Three
|
||||||
|
@a: =>
|
||||||
|
super! + 4
|
||||||
|
|
||||||
|
assert.same 8, Four\a!
|
||||||
|
|
||||||
|
it "super should still work when method wrapped", ->
|
||||||
|
add_some = (opts) ->
|
||||||
|
=> opts.amount + opts[1] @
|
||||||
|
|
||||||
|
class Base
|
||||||
|
value: => 1
|
||||||
|
|
||||||
|
class Sub extends Base
|
||||||
|
value: add_some {
|
||||||
|
amount: 12
|
||||||
|
=>
|
||||||
|
super! + 100
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.same 1 + 100 + 12, Sub!\value!
|
||||||
|
|
||||||
i = Four!
|
|
||||||
|
|
||||||
assert.same 8, i\a!
|
|
||||||
|
@ -202,7 +202,7 @@ class Wowha extends Thing
|
|||||||
super\hello
|
super\hello
|
||||||
|
|
||||||
|
|
||||||
@butt: cool {
|
@zone: cool {
|
||||||
->
|
->
|
||||||
super!
|
super!
|
||||||
super.hello
|
super.hello
|
||||||
|
@ -893,9 +893,9 @@ do
|
|||||||
return _fn_0(self, ...)
|
return _fn_0(self, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.butt = cool({
|
self.zone = cool({
|
||||||
function()
|
function()
|
||||||
_class_0.__parent.butt(self)
|
_class_0.__parent.zone(self)
|
||||||
_ = _class_0.__parent.hello
|
_ = _class_0.__parent.hello
|
||||||
_class_0.__parent.hello(self)
|
_class_0.__parent.hello(self)
|
||||||
local _base_1 = _class_0.__parent
|
local _base_1 = _class_0.__parent
|
||||||
|
Loading…
Reference in New Issue
Block a user