metamethods_spec.lua 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. local class = require 'middleclass'
  2. local function is_lua_5_2_compatible()
  3. return type(rawlen) == 'function'
  4. end
  5. local function is_lua_5_3_compatible()
  6. return type(string.unpack) == 'function'
  7. end
  8. if is_lua_5_2_compatible() then
  9. require 'spec/metamethods_lua_5_2'
  10. end
  11. if is_lua_5_3_compatible() then
  12. require 'spec.metamethods_lua_5_3'
  13. end
  14. describe('Metamethods', function()
  15. describe('Custom Metamethods', function()
  16. local Vector, v, w
  17. before_each(function()
  18. Vector= class('Vector')
  19. function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
  20. function Vector.__tostring(a) return a.class.name .. '[' .. a.x .. ',' .. a.y .. ',' .. a.z .. ']' end
  21. function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
  22. function Vector.__lt(a,b) return a() < b() end
  23. function Vector.__le(a,b) return a() <= b() end
  24. function Vector.__add(a,b) return a.class:new(a.x+b.x, a.y+b.y ,a.z+b.z) end
  25. function Vector.__sub(a,b) return a.class:new(a.x-b.x, a.y-b.y, a.z-b.z) end
  26. function Vector.__div(a,s) return a.class:new(a.x/s, a.y/s, a.z/s) end
  27. function Vector.__unm(a) return a.class:new(-a.x, -a.y, -a.z) end
  28. function Vector.__concat(a,b) return a.x*b.x+a.y*b.y+a.z*b.z end
  29. function Vector.__call(a) return math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z) end
  30. function Vector.__pow(a,b)
  31. return a.class:new(a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x)
  32. end
  33. function Vector.__mul(a,b)
  34. if type(b)=="number" then return a.class:new(a.x*b, a.y*b, a.z*b) end
  35. if type(a)=="number" then return b.class:new(a*b.x, a*b.y, a*b.z) end
  36. end
  37. Vector.__metatable = "metatable of a vector"
  38. Vector.__mode = "k"
  39. v = Vector:new(1,2,3)
  40. w = Vector:new(2,4,6)
  41. end)
  42. it('implements __tostring', function()
  43. assert.equal(tostring(v), "Vector[1,2,3]")
  44. end)
  45. it('implements __eq', function()
  46. assert.equal(v, v)
  47. end)
  48. it('implements __lt', function()
  49. assert.is_true(v < w)
  50. end)
  51. it('implements __le', function()
  52. assert.is_true(v <= w)
  53. end)
  54. it('implements __add', function()
  55. assert.equal(v+w, Vector(3,6,9))
  56. end)
  57. it('implements __sub', function()
  58. assert.equal(w-v, Vector(1,2,3))
  59. end)
  60. it('implements __div', function()
  61. assert.equal(w/2, Vector(1,2,3))
  62. end)
  63. it('implements __concat', function()
  64. assert.equal(v..w, 28)
  65. end)
  66. it('implements __call', function()
  67. assert.equal(v(), math.sqrt(14))
  68. end)
  69. it('implements __pow', function()
  70. assert.equal(v^w, Vector(0,0,0))
  71. end)
  72. it('implements __mul', function()
  73. assert.equal(4*v, Vector(4,8,12))
  74. end)
  75. it('implements __metatable', function()
  76. assert.equal("metatable of a vector", getmetatable(v))
  77. end)
  78. it('implements __mode', function()
  79. v[{}] = true
  80. collectgarbage()
  81. for k in pairs(v) do assert.not_table(k) end
  82. end)
  83. --[[
  84. it('implements __index', function()
  85. assert.equal(b[1], 3)
  86. end)
  87. --]]
  88. describe('Inherited Metamethods', function()
  89. local Vector2, v2, w2
  90. before_each(function()
  91. Vector2= class('Vector2', Vector)
  92. function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
  93. v2 = Vector2:new(1,2,3)
  94. w2 = Vector2:new(2,4,6)
  95. end)
  96. it('implements __tostring', function()
  97. assert.equal(tostring(v2), "Vector2[1,2,3]")
  98. end)
  99. it('implements __eq', function()
  100. assert.equal(v2, v2)
  101. end)
  102. it('implements __lt', function()
  103. assert.is_true(v2 < w2)
  104. end)
  105. it('implements __le', function()
  106. assert.is_true(v2 <= w2)
  107. end)
  108. it('implements __add', function()
  109. assert.equal(v2+w2, Vector2(3,6,9))
  110. end)
  111. it('implements __sub', function()
  112. assert.equal(w2-v2, Vector2(1,2,3))
  113. end)
  114. it('implements __div', function()
  115. assert.equal(w2/2, Vector2(1,2,3))
  116. end)
  117. it('implements __concat', function()
  118. assert.equal(v2..w2, 28)
  119. end)
  120. it('implements __call', function()
  121. assert.equal(v2(), math.sqrt(14))
  122. end)
  123. it('implements __pow', function()
  124. assert.equal(v2^w2, Vector2(0,0,0))
  125. end)
  126. it('implements __mul', function()
  127. assert.equal(4*v2, Vector2(4,8,12))
  128. end)
  129. it('implements __metatable', function()
  130. assert.equal("metatable of a vector", getmetatable(v2))
  131. end)
  132. it('implements __mode', function()
  133. v2[{}] = true
  134. collectgarbage()
  135. for k in pairs(v2) do assert.not_table(k) end
  136. end)
  137. it('allows inheriting further', function()
  138. local Vector3 = class('Vector3', Vector2)
  139. local v3 = Vector3(1,2,3)
  140. local w3 = Vector3(3,4,5)
  141. assert.equal(v3+w3, Vector3(4,6,8))
  142. end)
  143. describe('Updates', function()
  144. it('overrides __add', function()
  145. Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end
  146. assert.equal(v2+w2, Vector2(1.5,3,4.5))
  147. end)
  148. it('updates __add', function()
  149. Vector.__add = Vector.__sub
  150. assert.equal(v2+w2, Vector2(-1,-2,-3))
  151. end)
  152. it('does not update __add after overriding', function()
  153. Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end
  154. Vector.__add = Vector.__sub
  155. assert.equal(v2+w2, Vector2(-0.5,-1,-1.5))
  156. end)
  157. it('reverts __add override', function()
  158. Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end
  159. Vector2.__add = nil
  160. assert.equal(v2+w2, Vector2(3,6,9))
  161. end)
  162. end)
  163. end)
  164. end)
  165. describe('Custom __index and __newindex', function()
  166. describe('Tables', function()
  167. local Proxy, fallback, p
  168. before_each(function()
  169. Proxy = class('Proxy')
  170. fallback = {foo = 'bar', common = 'fallback'}
  171. Proxy.__index = fallback
  172. Proxy.__newindex = fallback
  173. Proxy.common = 'class'
  174. p = Proxy()
  175. end)
  176. it('uses __index', function()
  177. assert.equal(p.foo, 'bar')
  178. end)
  179. it('does not use __index when field exists in class', function()
  180. assert.equal(p.common, 'class')
  181. end)
  182. it('uses __newindex', function()
  183. p.key = 'value'
  184. assert.equal(fallback.key, 'value')
  185. end)
  186. it('uses __newindex when field exists in class', function()
  187. p.common = 'value'
  188. assert.equal(p.common, 'class')
  189. assert.equal(Proxy.common, 'class')
  190. assert.equal(fallback.common, 'value')
  191. end)
  192. end)
  193. describe('Functions', function()
  194. local Namespace, Rectangle, r
  195. before_each(function()
  196. Namespace = class('Namespace')
  197. function Namespace:__index(name)
  198. local getter = self.class[name.."Getter"]
  199. if getter then return getter(self) end
  200. end
  201. function Namespace:__newindex(name, value)
  202. local setter = self.class[name.."Setter"]
  203. if setter then setter(self, value) else rawset(self, name, value) end
  204. end
  205. Rectangle = class('Rectangle', Namespace)
  206. function Rectangle:initialize(x, y, scale)
  207. self._scale, self.x, self.y = 1, x, y
  208. self.scale = scale
  209. end
  210. function Rectangle:scaleGetter() return self._scale end
  211. function Rectangle:scaleSetter(v)
  212. self.x = self.x*v/self._scale
  213. self.y = self.y*v/self._scale
  214. self._scale = v
  215. end
  216. function Rectangle:areaGetter() return self.x * self.y end
  217. r = Rectangle(3, 4, 2)
  218. end)
  219. it('uses setter', function()
  220. assert.equal(r.x, 6)
  221. assert.equal(r.y, 8)
  222. r.scale = 3
  223. assert.equal(r.x, 9)
  224. assert.equal(r.y, 12)
  225. end)
  226. it('uses getters', function()
  227. assert.equal(r.scale, 2)
  228. assert.equal(r.area, 48)
  229. end)
  230. it('updates inherited __index', function()
  231. function Namespace.__index() return 42 end
  232. assert.equal(r.area, 42)
  233. function Rectangle.__index() return 24 end
  234. assert.equal(r.area, 24)
  235. function Namespace.__index() return 96 end
  236. assert.equal(r.area, 24)
  237. Rectangle.__index = nil
  238. assert.equal(r.area, 96)
  239. end)
  240. end)
  241. end)
  242. describe('Default Metamethods', function()
  243. local Peter, peter
  244. before_each(function()
  245. Peter = class('Peter')
  246. peter = Peter()
  247. end)
  248. describe('A Class', function()
  249. it('has a call metamethod properly set', function()
  250. assert.is_true(peter:isInstanceOf(Peter))
  251. end)
  252. it('has a tostring metamethod properly set', function()
  253. assert.equal(tostring(Peter), 'class Peter')
  254. end)
  255. end)
  256. describe('An instance', function()
  257. it('has a tostring metamethod, returning a different result from Object.__tostring', function()
  258. assert.equal(tostring(peter), 'instance of class Peter')
  259. end)
  260. end)
  261. end)
  262. end)