File size: 2,408 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package.path = '../common/libs/?.lua;../common/vendor/?.lua;' .. package.path

local lu = require('luaunit')

require('Polo')

--

TestPolo = {}

function TestPolo:testClassFields()
  local TestPoloClass = Polo {
    field1 = "this is a field",
    field2 = "this is another field",
  }

  lu.assertEquals(TestPoloClass.field1, "this is a field")
  lu.assertEquals(TestPoloClass.field2, "this is another field")
end

function TestPolo:testDefaultNewMethod()
  local TestPoloClass = Polo {}

  lu.assertNotIsNil(TestPoloClass.new)
end

function TestPolo:testOverriddenNewMethod()
  local TestPoloClass = Polo {
    new = function(field1, field2)
      return {
        field1 = field1,
        field2 = field2,
      }
    end
  }

  local testPolo = TestPoloClass.new("this is a field", "this is another field")

  lu.assertEquals(testPolo.field1, "this is a field")
  lu.assertIsNil(TestPoloClass.field1)
  lu.assertNotEquals(TestPoloClass.field1, "this is a field")
  lu.assertEquals(testPolo.field2, "this is another field")
  lu.assertIsNil(TestPoloClass.field2)
  lu.assertNotEquals(TestPoloClass.field2, "this is another field")
end

function TestPolo:testOptionalInitMethod()
  local TestPoloClass = Polo {
    init = function(self)
      self.field1 = "this is a field"
      self.field2 = "this is another field"
    end
  }

  local testPolo = TestPoloClass.new()

  lu.assertEquals(testPolo.field1, "this is a field")
  lu.assertEquals(testPolo.field2, "this is another field")
end

function TestPolo:testInitMethodDefinedLater()
  local TestPoloClass = Polo {}

  function TestPoloClass:init()
    self.field1 = "this is a field"
    self.field2 = "this is another field"
  end

  local testPolo = TestPoloClass.new()

  lu.assertEquals(testPolo.field1, "this is a field")
  lu.assertEquals(testPolo.field2, "this is another field")
end

function TestPolo:testInstanceMethod()
  local TestPoloClass = Polo {
    instanceMethodDefinedUpFront = function(self)
      return "inside instanceMethodDefinedUpFront"
    end
  }

  function TestPoloClass:instanceMethodDefinedLater()
    return "inside instanceMethodDefinedLater"
  end

  local testPolo = TestPoloClass.new()
  lu.assertEquals(testPolo:instanceMethodDefinedUpFront(), "inside instanceMethodDefinedUpFront")
  lu.assertEquals(testPolo:instanceMethodDefinedLater(), "inside instanceMethodDefinedLater")
end

--

os.exit(lu.LuaUnit.run())