summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/middleclass/spec/classes_spec.lua
blob: 7942f180c69445c7c2cee07c6912faddf7063348 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
local class = require 'middleclass'

describe('A Class', function()

  describe('Default stuff', function()

    local AClass

    before_each(function()
      AClass = class('AClass')
    end)

    describe('name', function()
      it('is correctly set', function()
        assert.equal(AClass.name, 'AClass')
      end)
    end)

    describe('tostring', function()
      it('returns "class *name*"', function()
        assert.equal(tostring(AClass), 'class AClass')
      end)
    end)

    describe('()', function()
      it('returns an object, like Class:new()', function()
        local obj = AClass()
        assert.equal(obj.class, AClass)
      end)
    end)

    describe('include', function()
      it('throws an error when used without the :', function()
        assert.error(function() AClass.include() end)
      end)
      it('throws an error when passed a non-table:', function()
        assert.error(function() AClass:include(1) end)
      end)
    end)

    describe('subclass', function()

      it('throws an error when used without the :', function()
        assert.error(function() AClass.subclass() end)
      end)

      it('throws an error when no name is given', function()
        assert.error( function() AClass:subclass() end)
      end)

      describe('when given a subclass name', function()

        local SubClass

        before_each(function()
          function AClass.static:subclassed(other) self.static.child = other end
          SubClass = AClass:subclass('SubClass')
        end)

        it('it returns a class with the correct name', function()
          assert.equal(SubClass.name, 'SubClass')
        end)

        it('it returns a class with the correct superclass', function()
          assert.equal(SubClass.super, AClass)
        end)

        it('it invokes the subclassed hook method', function()
          assert.equal(SubClass, AClass.child)
        end)

        it('it includes the subclass in the list of subclasses', function()
          assert.is_true(AClass.subclasses[SubClass])
        end)

      end)

    end)

  end)



  describe('attributes', function()

    local A, B

    before_each(function()
      A = class('A')
      A.static.foo = 'foo'

      B = class('B', A)
    end)

    it('are available after being initialized', function()
      assert.equal(A.foo, 'foo')
    end)

    it('are available for subclasses', function()
      assert.equal(B.foo, 'foo')
    end)

    it('are overridable by subclasses, without affecting the superclasses', function()
      B.static.foo = 'chunky bacon'
      assert.equal(B.foo, 'chunky bacon')
      assert.equal(A.foo, 'foo')
    end)

  end)

  describe('methods', function()

    local A, B

    before_each(function()
      A = class('A')
      function A.static:foo() return 'foo' end

      B = class('B', A)
    end)

    it('are available after being initialized', function()
      assert.equal(A:foo(), 'foo')
    end)

    it('are available for subclasses', function()
      assert.equal(B:foo(), 'foo')
    end)

    it('are overridable by subclasses, without affecting the superclasses', function()
      function B.static:foo() return 'chunky bacon' end
      assert.equal(B:foo(), 'chunky bacon')
      assert.equal(A:foo(), 'foo')
    end)

  end)

end)