default_methods_spec.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. local class = require 'middleclass'
  2. describe('Default methods', function()
  3. local Object
  4. before_each(function()
  5. Object = class('Object')
  6. end)
  7. describe('name', function()
  8. it('is correctly set', function()
  9. assert.equal(Object.name, 'Object')
  10. end)
  11. end)
  12. describe('tostring', function()
  13. it('returns "class Object"', function()
  14. assert.equal(tostring(Object), 'class Object')
  15. end)
  16. end)
  17. describe('()', function()
  18. it('returns an object, like Object:new()', function()
  19. local obj = Object()
  20. assert.is_true(obj:isInstanceOf(Object))
  21. end)
  22. end)
  23. describe('subclass', function()
  24. it('throws an error when used without the :', function()
  25. assert.error(function() Object.subclass() end)
  26. end)
  27. it('throws an error when no name is given', function()
  28. assert.error( function() Object:subclass() end)
  29. end)
  30. describe('when given a class name', function()
  31. local SubClass
  32. before_each(function()
  33. SubClass = Object:subclass('SubClass')
  34. end)
  35. it('it returns a class with the correct name', function()
  36. assert.equal(SubClass.name, 'SubClass')
  37. end)
  38. it('it returns a class with the correct superclass', function()
  39. assert.equal(SubClass.super, Object)
  40. end)
  41. it('it includes the subclass in the list of subclasses', function()
  42. assert.is_true(Object.subclasses[SubClass])
  43. end)
  44. end)
  45. end)
  46. describe('instance creation', function()
  47. local SubClass
  48. before_each(function()
  49. SubClass = class('SubClass')
  50. function SubClass:initialize() self.mark=true end
  51. end)
  52. describe('allocate', function()
  53. it('allocates instances properly', function()
  54. local instance = SubClass:allocate()
  55. assert.equal(instance.class, SubClass)
  56. assert.equal(tostring(instance), "instance of " .. tostring(SubClass))
  57. end)
  58. it('throws an error when used without the :', function()
  59. assert.error(Object.allocate)
  60. end)
  61. it('does not call the initializer', function()
  62. local allocated = SubClass:allocate()
  63. assert.is_nil(allocated.mark)
  64. end)
  65. it('can be overriden', function()
  66. local previousAllocate = SubClass.static.allocate
  67. function SubClass.static:allocate()
  68. local instance = previousAllocate(SubClass)
  69. instance.mark = true
  70. return instance
  71. end
  72. local allocated = SubClass:allocate()
  73. assert.is_true(allocated.mark)
  74. end)
  75. end)
  76. describe('new', function()
  77. it('initializes instances properly', function()
  78. local instance = SubClass:new()
  79. assert.equal(instance.class, SubClass)
  80. end)
  81. it('throws an error when used without the :', function()
  82. assert.error(SubClass.new)
  83. end)
  84. it('calls the initializer', function()
  85. local initialized = SubClass:new()
  86. assert.is_true(initialized.mark)
  87. end)
  88. end)
  89. describe('isInstanceOf', function()
  90. describe('primitives', function()
  91. local o = Object:new()
  92. local primitives = {nil, 1, 'hello', {}, function() end, Object:new()}
  93. describe('used as classes', function()
  94. for _,primitive in pairs(primitives) do
  95. local theType = type(primitive)
  96. it('object:isInstanceOf(, '.. theType ..') returns false', function()
  97. assert.is_falsy(o:isInstanceOf(primitive))
  98. end)
  99. end
  100. end)
  101. describe('used as instances', function()
  102. for _,primitive in pairs(primitives) do
  103. local theType = type(primitive)
  104. it('Object.isInstanceOf('.. theType ..', Object) returns false without error', function()
  105. assert.is_falsy(Object.isInstanceOf(primitive, Object))
  106. end)
  107. end
  108. end)
  109. end)
  110. describe('An instance', function()
  111. local Class1 = class('Class1')
  112. local Class2 = class('Class2', Class1)
  113. local Class3 = class('Class3', Class2)
  114. local UnrelatedClass = class('Unrelated')
  115. local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
  116. it('isInstanceOf its class', function()
  117. assert.is_true(o1:isInstanceOf(Class1))
  118. assert.is_true(o2:isInstanceOf(Class2))
  119. assert.is_true(o3:isInstanceOf(Class3))
  120. end)
  121. it('is instanceOf its class\' superclasses', function()
  122. assert.is_true(o2:isInstanceOf(Class1))
  123. assert.is_true(o3:isInstanceOf(Class1))
  124. assert.is_true(o3:isInstanceOf(Class2))
  125. end)
  126. it('is not instanceOf its class\' subclasses', function()
  127. assert.is_false(o1:isInstanceOf(Class2))
  128. assert.is_false(o1:isInstanceOf(Class3))
  129. assert.is_false(o2:isInstanceOf(Class3))
  130. end)
  131. it('is not instanceOf an unrelated class', function()
  132. assert.is_false(o1:isInstanceOf(UnrelatedClass))
  133. assert.is_false(o2:isInstanceOf(UnrelatedClass))
  134. assert.is_false(o3:isInstanceOf(UnrelatedClass))
  135. end)
  136. end)
  137. end)
  138. end)
  139. describe('isSubclassOf', function()
  140. it('returns false for instances', function()
  141. assert.is_false(Object:isSubclassOf(Object:new()))
  142. end)
  143. describe('on primitives', function()
  144. local primitives = {nil, 1, 'hello', {}, function() end}
  145. for _,primitive in pairs(primitives) do
  146. local theType = type(primitive)
  147. it('returns false for ' .. theType, function()
  148. assert.is_false(Object:isSubclassOf(primitive))
  149. end)
  150. end
  151. end)
  152. describe('Any class (except Object)', function()
  153. local Class1 = class('Class1')
  154. local Class2 = class('Class2', Class1)
  155. local Class3 = class('Class3', Class2)
  156. local UnrelatedClass = class('Unrelated')
  157. it('is subclassOf its direct superclass', function()
  158. assert.is_true(Class2:isSubclassOf(Class1))
  159. assert.is_true(Class3:isSubclassOf(Class2))
  160. end)
  161. it('is subclassOf its ancestors', function()
  162. assert.is_true(Class3:isSubclassOf(Class1))
  163. end)
  164. it('is a subclassOf its class\' subclasses', function()
  165. assert.is_true(Class2:isSubclassOf(Class1))
  166. assert.is_true(Class3:isSubclassOf(Class1))
  167. assert.is_true(Class3:isSubclassOf(Class2))
  168. end)
  169. it('is not a subclassOf an unrelated class', function()
  170. assert.is_false(Class1:isSubclassOf(UnrelatedClass))
  171. assert.is_false(Class2:isSubclassOf(UnrelatedClass))
  172. assert.is_false(Class3:isSubclassOf(UnrelatedClass))
  173. end)
  174. end)
  175. end)
  176. end)