metamethods_lua_5_3.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. local class = require 'middleclass'
  2. local it = require('busted').it
  3. local describe = require('busted').describe
  4. local before_each = require('busted').before_each
  5. local assert = require('busted').assert
  6. describe('Lua 5.3 Metamethods', function()
  7. local Vector, v, last_gc
  8. before_each(function()
  9. Vector= class('Vector')
  10. function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
  11. function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
  12. function Vector.__pairs(a)
  13. local t = {x=a.x,y=a.y,z=a.z}
  14. return coroutine.wrap(function()
  15. for k,val in pairs(t) do
  16. coroutine.yield(k,val)
  17. end
  18. end)
  19. end
  20. function Vector.__len(a) return 3 end
  21. function Vector.__gc(a) last_gc = {a.class.name, a.x, a.y, a.z} end
  22. function Vector.__band(a,n) return a.class:new(a.x & n, a.y & n, a.z & n) end
  23. function Vector.__bor(a,n) return a.class:new(a.x | n, a.y | n, a.z | n) end
  24. function Vector.__bxor(a,n) return a.class:new(a.x ~ n, a.y ~ n, a.z ~ n) end
  25. function Vector.__shl(a,n) return a.class:new(a.x << n, a.y << n, a.z << n) end
  26. function Vector.__shr(a,n) return a.class:new(a.x >> n, a.y >> n, a.z >> n) end
  27. function Vector.__bnot(a) return a.class:new(~a.x, ~a.y, ~a.z) end
  28. v = Vector:new(1,2,3)
  29. end)
  30. it('implements __gc', function()
  31. collectgarbage()
  32. v = nil
  33. collectgarbage()
  34. assert.are.same(last_gc, {"Vector",1,2,3})
  35. end)
  36. it('implements __band', function()
  37. assert.equal(v & 1, Vector(1,0,1))
  38. end)
  39. it('implements __bor', function()
  40. assert.equal(v | 0, Vector(1,2,3))
  41. end)
  42. it('implements __bxor', function()
  43. assert.equal(v | 1, Vector(1,3,3))
  44. end)
  45. it('implements __shl', function()
  46. assert.equal(v << 1, Vector(2,4,6))
  47. end)
  48. it('implements __shr', function()
  49. assert.equal(v >> 1, Vector(0,1,1))
  50. end)
  51. it('implements __bnot', function()
  52. assert.equal(~v, Vector(-2,-3,-4))
  53. end)
  54. describe('Inherited Metamethods', function()
  55. local Vector2, v2
  56. before_each(function()
  57. Vector2= class('Vector2', Vector)
  58. function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
  59. v2 = Vector2:new(1,2,3)
  60. end)
  61. it('implements __gc', function()
  62. collectgarbage()
  63. v2 = nil
  64. collectgarbage()
  65. assert.are.same(last_gc, {"Vector2",1,2,3})
  66. end)
  67. it('implements __band', function()
  68. assert.equal(v2 & 1, Vector2(1,0,1))
  69. end)
  70. it('implements __bor', function()
  71. assert.equal(v2 | 0, Vector2(1,2,3))
  72. end)
  73. it('implements __bxor', function()
  74. assert.equal(v2 | 1, Vector2(1,3,3))
  75. end)
  76. it('implements __shl', function()
  77. assert.equal(v2 << 1, Vector2(2,4,6))
  78. end)
  79. it('implements __shr', function()
  80. assert.equal(v2 >> 1, Vector2(0,1,1))
  81. end)
  82. it('implements __bnot', function()
  83. assert.equal(~v2, Vector2(-2,-3,-4))
  84. end)
  85. end)
  86. end)