instances_spec.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. local class = require 'middleclass'
  2. describe('An instance', function()
  3. describe('attributes', function()
  4. local Person
  5. before_each(function()
  6. Person = class('Person')
  7. function Person:initialize(name)
  8. self.name = name
  9. end
  10. end)
  11. it('are available in the instance after being initialized', function()
  12. local bob = Person:new('bob')
  13. assert.equal(bob.name, 'bob')
  14. end)
  15. it('are available in the instance after being initialized by a superclass', function()
  16. local AgedPerson = class('AgedPerson', Person)
  17. function AgedPerson:initialize(name, age)
  18. Person.initialize(self, name)
  19. self.age = age
  20. end
  21. local pete = AgedPerson:new('pete', 31)
  22. assert.equal(pete.name, 'pete')
  23. assert.equal(pete.age, 31)
  24. end)
  25. end)
  26. describe('methods', function()
  27. local A, B, a, b
  28. before_each(function()
  29. A = class('A')
  30. function A:overridden() return 'foo' end
  31. function A:regular() return 'regular' end
  32. B = class('B', A)
  33. function B:overridden() return 'bar' end
  34. a = A:new()
  35. b = B:new()
  36. end)
  37. it('are available for any instance', function()
  38. assert.equal(a:overridden(), 'foo')
  39. end)
  40. it('are inheritable', function()
  41. assert.equal(b:regular(), 'regular')
  42. end)
  43. it('are overridable', function()
  44. assert.equal(b:overridden(), 'bar')
  45. end)
  46. end)
  47. end)