1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/test/test_classes.coffee

229 lines
4.1 KiB
CoffeeScript
Raw Normal View History

# Test classes with a four-level inheritance chain.
class Base
func: (string) ->
"zero/#{string}"
@static: (string) ->
"static/#{string}"
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]
# Gratuitous comment for testing.
func: (string) ->
super('three/') + string
2010-07-25 01:23:37 -04:00
result = (new ThirdChild).func 'four'
ok result is 'zero/one/two/three/four'
ok Base.static('word') is 'static/word'
class TopClass
constructor: (arg) ->
2010-07-25 01:23:37 -04:00
@prop = 'top-' + arg
class SuperClass extends TopClass
constructor: (arg) ->
super 'super-' + arg
class SubClass extends SuperClass
constructor: ->
super 'sub'
ok (new SubClass).prop is 'top-super-sub'
class OneClass
2010-07-25 01:23:37 -04:00
constructor: (name) -> @name = name
class TwoClass extends OneClass
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) ->
'zero/' + string
2010-07-25 01:23:37 -04:00
Base::['func-func'] = (string) ->
"dynamic-#{string}"
2010-07-25 01:23:37 -04:00
FirstChild = ->
FirstChild extends Base
2010-07-25 01:23:37 -04:00
FirstChild::func = (string) ->
super('one/') + string
2010-07-25 01:23:37 -04:00
SecondChild = ->
SecondChild extends FirstChild
2010-07-25 01:23:37 -04:00
SecondChild::func = (string) ->
super('two/') + string
2010-07-25 01:23:37 -04:00
ThirdChild = ->
@array = [1, 2, 3]
this
ThirdChild extends SecondChild
2010-07-25 01:23:37 -04:00
ThirdChild::func = (string) ->
super('three/') + string
2010-07-25 01:23:37 -04:00
result = (new ThirdChild).func 'four'
ok result is 'zero/one/two/three/four'
ok (new ThirdChild)['func-func']('thing') is 'dynamic-thing'
2010-07-25 01:23:37 -04:00
TopClass = (arg) ->
@prop = 'top-' + arg
this
2010-07-25 01:23:37 -04:00
SuperClass = (arg) ->
super 'super-' + arg
this
2010-07-25 01:23:37 -04:00
SubClass = ->
super 'sub'
this
SuperClass extends TopClass
SubClass extends SuperClass
ok (new SubClass).prop is 'top-super-sub'
# '@' referring to the current instance, and not being coerced into a call.
class ClassName
amI: ->
@ instanceof ClassName
2010-07-25 01:23:37 -04:00
obj = new ClassName
ok obj.amI()
# 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
class Hive.Bee extends Hive
2010-07-18 14:46:21 -04:00
constructor: (name) -> super
2010-07-25 01:23:37 -04:00
maya = new Hive.Bee 'Maya'
ok maya.name is 'Maya'
# Class with JS-keyword properties.
class Class
class: 'class'
name: -> @class
2010-07-25 01:23:37 -04:00
instance = new Class
ok instance.class is 'class'
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
bark: =>
"#{@name} woofs!"
2010-07-25 01:23:37 -04:00
spark = new Dog('Spark')
fido = new Dog('Fido')
fido.bark = spark.bark
ok fido.bark() is 'Spark woofs!'
# 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
ok (func() for func in m.generate()).join(' ') is '10 10 10'
# 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]
out: ->
"#{@one}-#{@two}-#{@three}"
2010-07-25 01:23:37 -04:00
list = [3, 2, 1]
conn = new Connection list...
ok conn instanceof Connection
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'
# Test `extended` callback.
class Base
@extended: (subclass) ->
for key, value of @
2010-07-25 01:23:37 -04:00
subclass[key] = value
class Element extends Base
@fromHTML: (html) ->
2010-07-25 01:23:37 -04:00
node = "..."
new @(node)
constructor: (node) ->
2010-07-25 01:23:37 -04:00
@node = node
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
# 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'
# Test anonymous classes.
obj =
klass: class
method: -> 'value'
instance = new obj.klass
ok instance.method() is 'value'