|
| 1 | + |
| 2 | +describe "class", -> |
| 3 | + it "should make a class with constructor", -> |
| 4 | + class Thing |
| 5 | + new: => |
| 6 | + @color = "blue" |
| 7 | + |
| 8 | + instance = Thing! |
| 9 | + |
| 10 | + assert.same instance, { color: "blue" } |
| 11 | + |
| 12 | + it "should have instance methods", -> |
| 13 | + class Thing |
| 14 | + get_color: => @color |
| 15 | + |
| 16 | + new: => |
| 17 | + @color = "blue" |
| 18 | + |
| 19 | + instance = Thing! |
| 20 | + assert.same instance\get_color!, "blue" |
| 21 | + |
| 22 | + |
| 23 | + it "should have class property", -> |
| 24 | + class Thing |
| 25 | + color: "blue" |
| 26 | + get_color: => @color |
| 27 | + |
| 28 | + instance = Thing! |
| 29 | + assert.same instance\get_color!, "blue" |
| 30 | + assert.same Thing.color, "blue" |
| 31 | + |
| 32 | + it "should inherit another class", -> |
| 33 | + class Base |
| 34 | + get_property: => @[@property] |
| 35 | + |
| 36 | + new: (@property) => |
| 37 | + |
| 38 | + class Thing extends Base |
| 39 | + color: "green" |
| 40 | + |
| 41 | + instance = Thing "color" |
| 42 | + assert.same instance\get_property!, "green" |
| 43 | + |
| 44 | + it "should call super constructor", -> |
| 45 | + class Base |
| 46 | + new: (@property) => |
| 47 | + |
| 48 | + class Thing extends Base |
| 49 | + new: (@name) => |
| 50 | + super "name" |
| 51 | + |
| 52 | + instance = Thing "the_thing" |
| 53 | + |
| 54 | + assert.same instance.property, "name" |
| 55 | + assert.same instance.name, "the_thing" |
| 56 | + |
| 57 | + |
| 58 | + it "should call super method", -> |
| 59 | + class Base |
| 60 | + _count: 111 |
| 61 | + counter: => @_count |
| 62 | + |
| 63 | + class Thing extends Base |
| 64 | + counter: => "%08d"\format super! |
| 65 | + |
| 66 | + instance = Thing! |
| 67 | + assert.same instance\counter!, "00000111" |
| 68 | + |
| 69 | + it "should get super class", -> |
| 70 | + class Base |
| 71 | + class Thing extends Base |
| 72 | + get_super: => super |
| 73 | + |
| 74 | + instance = Thing! |
| 75 | + assert.is_true instance\get_super! == Base |
| 76 | + |
| 77 | + it "should get a bound method from super", -> |
| 78 | + class Base |
| 79 | + count: 1 |
| 80 | + get_count: => @count |
| 81 | + |
| 82 | + class Thing extends Base |
| 83 | + get_count: => "this is wrong" |
| 84 | + get_method: => super\get_count |
| 85 | + |
| 86 | + instance = Thing! |
| 87 | + assert.same instance\get_method!!, 1 |
| 88 | + |
| 89 | + it "should have class properties", -> |
| 90 | + class Base |
| 91 | + class Thing extends Base |
| 92 | + |
| 93 | + instance = Thing! |
| 94 | + |
| 95 | + assert.same Base.__name, "Base" |
| 96 | + assert.same Thing.__name, "Thing" |
| 97 | + assert.is_true Thing.__parent == Base |
| 98 | + |
0 commit comments