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-08-07 08:02:16 -04:00
|
|
|
"zero/#{string}"
|
2010-03-29 21:43:12 -04:00
|
|
|
|
|
|
|
@static: (string) ->
|
2010-08-07 08:02:16 -04:00
|
|
|
"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: ->
|
2010-07-25 01:23:37 -04:00
|
|
|
@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-07-25 01:23:37 -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) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
@prop = 'top-' + arg
|
2010-02-27 18:57:45 -05:00
|
|
|
|
|
|
|
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
|
2010-07-25 01:23:37 -04:00
|
|
|
constructor: (name) -> @name = name
|
2010-02-28 00:13:17 -05:00
|
|
|
|
|
|
|
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:
|
2010-07-25 01:23:37 -04:00
|
|
|
Base = ->
|
|
|
|
Base::func = (string) ->
|
2010-04-03 10:39:32 -04:00
|
|
|
'zero/' + string
|
2010-07-25 01:23:37 -04:00
|
|
|
Base::['func-func'] = (string) ->
|
2010-08-07 08:02:16 -04:00
|
|
|
"dynamic-#{string}"
|
2010-04-03 10:39:32 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
FirstChild = ->
|
2010-04-03 10:39:32 -04:00
|
|
|
FirstChild extends Base
|
2010-07-25 01:23:37 -04:00
|
|
|
FirstChild::func = (string) ->
|
2010-04-03 10:39:32 -04:00
|
|
|
super('one/') + string
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
SecondChild = ->
|
2010-04-03 10:39:32 -04:00
|
|
|
SecondChild extends FirstChild
|
2010-07-25 01:23:37 -04:00
|
|
|
SecondChild::func = (string) ->
|
2010-04-03 10:39:32 -04:00
|
|
|
super('two/') + string
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
ThirdChild = ->
|
|
|
|
@array = [1, 2, 3]
|
2010-04-03 10:39:32 -04:00
|
|
|
this
|
|
|
|
ThirdChild extends SecondChild
|
2010-07-25 01:23:37 -04:00
|
|
|
ThirdChild::func = (string) ->
|
2010-04-03 10:39:32 -04:00
|
|
|
super('three/') + string
|
|
|
|
|
2010-07-25 01:23:37 -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
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
TopClass = (arg) ->
|
|
|
|
@prop = 'top-' + arg
|
2010-04-03 10:39:32 -04:00
|
|
|
this
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
SuperClass = (arg) ->
|
2010-04-03 10:39:32 -04:00
|
|
|
super 'super-' + arg
|
|
|
|
this
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
SubClass = ->
|
2010-04-03 10:39:32 -04:00
|
|
|
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-07-25 01:23:37 -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
|
2010-07-25 01:23:37 -04:00
|
|
|
constructor: (name) -> @name = name
|
2010-05-31 15:13:48 -04:00
|
|
|
|
|
|
|
class Hive.Bee extends Hive
|
2010-07-18 14:46:21 -04:00
|
|
|
constructor: (name) -> super
|
2010-05-31 15:13:48 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
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-07-25 01:23:37 -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) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
@name = name
|
2010-06-15 01:28:30 -04:00
|
|
|
|
|
|
|
bark: =>
|
2010-08-07 08:02:16 -04:00
|
|
|
"#{@name} woofs!"
|
2010-06-15 01:28:30 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
spark = new Dog('Spark')
|
|
|
|
fido = new Dog('Fido')
|
|
|
|
fido.bark = spark.bark
|
2010-06-15 01:28:30 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
m = new Mini
|
2010-06-30 21:54:16 -04:00
|
|
|
ok (func() for func in m.generate()).join(' ') is '10 10 10'
|
2010-07-12 22:42:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Testing a contructor called with varargs.
|
|
|
|
class Connection
|
|
|
|
constructor: (one, two, three) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
[@one, @two, @three] = [one, two, three]
|
2010-07-12 22:42:21 -04:00
|
|
|
|
|
|
|
out: ->
|
2010-08-07 08:02:16 -04:00
|
|
|
"#{@one}-#{@two}-#{@three}"
|
2010-07-12 22:42:21 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
list = [3, 2, 1]
|
|
|
|
conn = new Connection list...
|
2010-07-13 08:57:42 -04:00
|
|
|
ok conn instanceof Connection
|
2010-07-12 22:42:21 -04:00
|
|
|
ok conn.out() is '3-2-1'
|
2010-07-18 14:46:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test calling super and passing along all arguments.
|
|
|
|
class Parent
|
2010-07-25 01:23:37 -04:00
|
|
|
method: (args...) -> @args = args
|
2010-07-18 14:46:21 -04:00
|
|
|
|
|
|
|
class Child extends Parent
|
|
|
|
method: -> super
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
c = new Child
|
2010-07-18 14:46:21 -04:00
|
|
|
c.method 1, 2, 3, 4
|
|
|
|
ok c.args.join(' ') is '1 2 3 4'
|
|
|
|
|
2010-07-18 15:22:07 -04:00
|
|
|
|
|
|
|
# Test `extended` callback.
|
|
|
|
class Base
|
|
|
|
@extended: (subclass) ->
|
|
|
|
for key, value of @
|
2010-07-25 01:23:37 -04:00
|
|
|
subclass[key] = value
|
2010-07-18 15:22:07 -04:00
|
|
|
|
|
|
|
class Element extends Base
|
|
|
|
@fromHTML: (html) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
node = "..."
|
2010-07-18 15:22:07 -04:00
|
|
|
new @(node)
|
|
|
|
|
|
|
|
constructor: (node) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
@node = node
|
2010-07-18 15:22:07 -04:00
|
|
|
|
|
|
|
ok Element.extended is Base.extended
|
|
|
|
ok Element.__superClass__ is Base.prototype
|
|
|
|
|
|
|
|
class MyElement extends Element
|
|
|
|
|
|
|
|
ok MyElement.extended is Base.extended
|
|
|
|
ok MyElement.fromHTML is Element.fromHTML
|
|
|
|
ok MyElement.__superClass__ is Element.prototype
|
2010-07-27 07:03:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test classes wrapped in decorators.
|
|
|
|
func = (klass) ->
|
|
|
|
klass::prop = 'value'
|
|
|
|
klass
|
|
|
|
|
|
|
|
func class Test
|
|
|
|
prop2: 'value2'
|
|
|
|
|
|
|
|
ok (new Test).prop is 'value'
|
|
|
|
ok (new Test).prop2 is 'value2'
|
2010-07-27 22:05:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test anonymous classes.
|
|
|
|
obj =
|
|
|
|
klass: class
|
|
|
|
method: -> 'value'
|
|
|
|
|
|
|
|
instance = new obj.klass
|
|
|
|
ok instance.method() is 'value'
|