2010-04-03 10:39:32 -04:00
|
|
|
# Test classes with a four-level inheritance chain.
|
2010-02-27 18:57:45 -05:00
|
|
|
class Base
|
|
|
|
func: (string) ->
|
2010-03-29 21:43:12 -04:00
|
|
|
"zero/$string"
|
|
|
|
|
|
|
|
@static: (string) ->
|
|
|
|
"static/$string"
|
2010-02-27 18:57:45 -05:00
|
|
|
|
|
|
|
class FirstChild extends Base
|
|
|
|
func: (string) ->
|
|
|
|
super('one/') + string
|
|
|
|
|
|
|
|
class SecondChild extends FirstChild
|
|
|
|
func: (string) ->
|
|
|
|
super('two/') + string
|
|
|
|
|
|
|
|
class ThirdChild extends SecondChild
|
|
|
|
constructor: ->
|
|
|
|
@array: [1, 2, 3]
|
2010-02-27 19:03:23 -05:00
|
|
|
|
|
|
|
# Gratuitous comment for testing.
|
2010-02-27 18:57:45 -05:00
|
|
|
func: (string) ->
|
|
|
|
super('three/') + string
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
result: (new ThirdChild).func 'four'
|
2010-02-27 18:57:45 -05:00
|
|
|
|
|
|
|
ok result is 'zero/one/two/three/four'
|
2010-03-29 21:43:12 -04:00
|
|
|
ok Base.static('word') is 'static/word'
|
2010-02-27 18:57:45 -05:00
|
|
|
|
|
|
|
|
|
|
|
class TopClass
|
|
|
|
constructor: (arg) ->
|
|
|
|
@prop: 'top-' + arg
|
|
|
|
|
|
|
|
class SuperClass extends TopClass
|
|
|
|
constructor: (arg) ->
|
|
|
|
super 'super-' + arg
|
|
|
|
|
|
|
|
class SubClass extends SuperClass
|
|
|
|
constructor: ->
|
|
|
|
super 'sub'
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
ok (new SubClass).prop is 'top-super-sub'
|
2010-02-28 00:13:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
class OneClass
|
|
|
|
constructor: (name) -> @name: name
|
|
|
|
|
|
|
|
class TwoClass extends OneClass
|
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
ok (new TwoClass('three')).name is 'three'
|
|
|
|
|
|
|
|
|
|
|
|
# And now the same tests, but written in the manual style:
|
|
|
|
Base: ->
|
|
|
|
Base::func: (string) ->
|
|
|
|
'zero/' + string
|
2010-05-31 22:32:43 -04:00
|
|
|
Base::['func-func']: (string) ->
|
|
|
|
"dynamic-$string"
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
FirstChild: ->
|
|
|
|
FirstChild extends Base
|
|
|
|
FirstChild::func: (string) ->
|
|
|
|
super('one/') + string
|
|
|
|
|
|
|
|
SecondChild: ->
|
|
|
|
SecondChild extends FirstChild
|
|
|
|
SecondChild::func: (string) ->
|
|
|
|
super('two/') + string
|
|
|
|
|
|
|
|
ThirdChild: ->
|
|
|
|
@array: [1, 2, 3]
|
|
|
|
this
|
|
|
|
ThirdChild extends SecondChild
|
|
|
|
ThirdChild::func: (string) ->
|
|
|
|
super('three/') + string
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
result: (new ThirdChild).func 'four'
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
ok result is 'zero/one/two/three/four'
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
ok (new ThirdChild)['func-func']('thing') is 'dynamic-thing'
|
2010-05-31 22:32:43 -04:00
|
|
|
|
2010-04-03 10:39:32 -04:00
|
|
|
|
|
|
|
TopClass: (arg) ->
|
|
|
|
@prop: 'top-' + arg
|
|
|
|
this
|
|
|
|
|
|
|
|
SuperClass: (arg) ->
|
|
|
|
super 'super-' + arg
|
|
|
|
this
|
|
|
|
|
|
|
|
SubClass: ->
|
|
|
|
super 'sub'
|
|
|
|
this
|
|
|
|
|
|
|
|
SuperClass extends TopClass
|
|
|
|
SubClass extends SuperClass
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
ok (new SubClass).prop is 'top-super-sub'
|
2010-05-31 15:13:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
# '@' referring to the current instance, and not being coerced into a call.
|
|
|
|
class ClassName
|
2010-06-12 19:05:13 -04:00
|
|
|
amI: ->
|
2010-05-31 15:13:48 -04:00
|
|
|
@ instanceof ClassName
|
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
obj: new ClassName
|
2010-06-12 19:05:13 -04:00
|
|
|
ok obj.amI()
|
2010-05-31 15:13:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
# super() calls in constructors of classes that are defined as object properties.
|
|
|
|
class Hive
|
|
|
|
constructor: (name) -> @name: name
|
|
|
|
|
|
|
|
class Hive.Bee extends Hive
|
|
|
|
constructor: (name) -> super name
|
|
|
|
|
|
|
|
maya: new Hive.Bee 'Maya'
|
2010-06-15 00:54:02 -04:00
|
|
|
ok maya.name is 'Maya'
|
|
|
|
|
|
|
|
|
|
|
|
# Class with JS-keyword properties.
|
|
|
|
class Class
|
|
|
|
class: 'class'
|
2010-06-15 01:03:14 -04:00
|
|
|
name: -> @class
|
2010-06-15 00:54:02 -04:00
|
|
|
|
2010-06-29 21:03:50 -04:00
|
|
|
instance: new Class
|
2010-06-15 01:03:14 -04:00
|
|
|
ok instance.class is 'class'
|
2010-06-15 01:28:30 -04:00
|
|
|
ok instance.name() is 'class'
|
|
|
|
|
|
|
|
|
|
|
|
# Classes with methods that are pre-bound to the instance.
|
|
|
|
class Dog
|
|
|
|
|
|
|
|
constructor: (name) ->
|
|
|
|
@name: name
|
|
|
|
|
|
|
|
bark: =>
|
|
|
|
"$@name woofs!"
|
|
|
|
|
|
|
|
spark: new Dog('Spark')
|
|
|
|
fido: new Dog('Fido')
|
|
|
|
fido.bark: spark.bark
|
|
|
|
|
|
|
|
ok fido.bark() is 'Spark woofs!'
|
2010-06-30 21:54:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Testing a bound function in a bound function.
|
|
|
|
class Mini
|
|
|
|
num: 10
|
|
|
|
generate: =>
|
|
|
|
for i in [1..3]
|
|
|
|
=>
|
|
|
|
@num
|
|
|
|
|
|
|
|
m: new Mini
|
|
|
|
ok (func() for func in m.generate()).join(' ') is '10 10 10'
|