classes_spec.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. local class = require 'middleclass'
  2. describe('A Class', function()
  3. describe('Default stuff', function()
  4. local AClass
  5. before_each(function()
  6. AClass = class('AClass')
  7. end)
  8. describe('name', function()
  9. it('is correctly set', function()
  10. assert.equal(AClass.name, 'AClass')
  11. end)
  12. end)
  13. describe('tostring', function()
  14. it('returns "class *name*"', function()
  15. assert.equal(tostring(AClass), 'class AClass')
  16. end)
  17. end)
  18. describe('()', function()
  19. it('returns an object, like Class:new()', function()
  20. local obj = AClass()
  21. assert.equal(obj.class, AClass)
  22. end)
  23. end)
  24. describe('include', function()
  25. it('throws an error when used without the :', function()
  26. assert.error(function() AClass.include() end)
  27. end)
  28. it('throws an error when passed a non-table:', function()
  29. assert.error(function() AClass:include(1) end)
  30. end)
  31. end)
  32. describe('subclass', function()
  33. it('throws an error when used without the :', function()
  34. assert.error(function() AClass.subclass() end)
  35. end)
  36. it('throws an error when no name is given', function()
  37. assert.error( function() AClass:subclass() end)
  38. end)
  39. describe('when given a subclass name', function()
  40. local SubClass
  41. before_each(function()
  42. function AClass.static:subclassed(other) self.static.child = other end
  43. SubClass = AClass:subclass('SubClass')
  44. end)
  45. it('it returns a class with the correct name', function()
  46. assert.equal(SubClass.name, 'SubClass')
  47. end)
  48. it('it returns a class with the correct superclass', function()
  49. assert.equal(SubClass.super, AClass)
  50. end)
  51. it('it invokes the subclassed hook method', function()
  52. assert.equal(SubClass, AClass.child)
  53. end)
  54. it('it includes the subclass in the list of subclasses', function()
  55. assert.is_true(AClass.subclasses[SubClass])
  56. end)
  57. end)
  58. end)
  59. end)
  60. describe('attributes', function()
  61. local A, B
  62. before_each(function()
  63. A = class('A')
  64. A.static.foo = 'foo'
  65. B = class('B', A)
  66. end)
  67. it('are available after being initialized', function()
  68. assert.equal(A.foo, 'foo')
  69. end)
  70. it('are available for subclasses', function()
  71. assert.equal(B.foo, 'foo')
  72. end)
  73. it('are overridable by subclasses, without affecting the superclasses', function()
  74. B.static.foo = 'chunky bacon'
  75. assert.equal(B.foo, 'chunky bacon')
  76. assert.equal(A.foo, 'foo')
  77. end)
  78. end)
  79. describe('methods', function()
  80. local A, B
  81. before_each(function()
  82. A = class('A')
  83. function A.static:foo() return 'foo' end
  84. B = class('B', A)
  85. end)
  86. it('are available after being initialized', function()
  87. assert.equal(A:foo(), 'foo')
  88. end)
  89. it('are available for subclasses', function()
  90. assert.equal(B:foo(), 'foo')
  91. end)
  92. it('are overridable by subclasses, without affecting the superclasses', function()
  93. function B.static:foo() return 'chunky bacon' end
  94. assert.equal(B:foo(), 'chunky bacon')
  95. assert.equal(A:foo(), 'foo')
  96. end)
  97. end)
  98. end)