summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/middleclass/spec/instances_spec.lua
blob: d9ac52c713592f68b13ef2c61add2b2872607b07 (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
local class = require 'middleclass'

describe('An instance', function()

  describe('attributes', function()

    local Person

    before_each(function()
      Person = class('Person')
      function Person:initialize(name)
        self.name = name
      end
    end)

    it('are available in the instance after being initialized', function()
      local bob = Person:new('bob')
      assert.equal(bob.name, 'bob')
    end)

    it('are available in the instance after being initialized by a superclass', function()
      local AgedPerson = class('AgedPerson', Person)
      function AgedPerson:initialize(name, age)
        Person.initialize(self, name)
        self.age = age
      end

      local pete = AgedPerson:new('pete', 31)
      assert.equal(pete.name, 'pete')
      assert.equal(pete.age, 31)
    end)

  end)

  describe('methods', function()

    local A, B, a, b

    before_each(function()
      A = class('A')
      function A:overridden() return 'foo' end
      function A:regular() return 'regular' end

      B = class('B', A)
      function B:overridden() return 'bar' end

      a = A:new()
      b = B:new()
    end)

    it('are available for any instance', function()
      assert.equal(a:overridden(), 'foo')
    end)

    it('are inheritable', function()
      assert.equal(b:regular(), 'regular')
    end)

    it('are overridable', function()
      assert.equal(b:overridden(), 'bar')
    end)

  end)

end)