mixins_spec.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. local class = require 'middleclass'
  2. describe('A Mixin', function()
  3. local Mixin1, Mixin2, Class1, Class2
  4. before_each(function()
  5. Mixin1, Mixin2 = {},{}
  6. function Mixin1:included(theClass) theClass.includesMixin1 = true end
  7. function Mixin1:foo() return 'foo' end
  8. function Mixin1:bar() return 'bar' end
  9. Mixin1.static = {}
  10. Mixin1.static.bazzz = function() return 'bazzz' end
  11. function Mixin2:baz() return 'baz' end
  12. Class1 = class('Class1'):include(Mixin1, Mixin2)
  13. function Class1:foo() return 'foo1' end
  14. Class2 = class('Class2', Class1)
  15. function Class2:bar2() return 'bar2' end
  16. end)
  17. it('invokes the "included" method when included', function()
  18. assert.is_true(Class1.includesMixin1)
  19. end)
  20. it('has all its functions (except "included") copied to its target class', function()
  21. assert.equal(Class1:bar(), 'bar')
  22. assert.is_nil(Class1.included)
  23. end)
  24. it('makes its functions available to subclasses', function()
  25. assert.equal(Class2:baz(), 'baz')
  26. end)
  27. it('allows overriding of methods in the same class', function()
  28. assert.equal(Class2:foo(), 'foo1')
  29. end)
  30. it('allows overriding of methods on subclasses', function()
  31. assert.equal(Class2:bar2(), 'bar2')
  32. end)
  33. it('makes new static methods available in classes', function()
  34. assert.equal(Class1:bazzz(), 'bazzz')
  35. assert.equal(Class2:bazzz(), 'bazzz')
  36. end)
  37. end)