splits 5.2 and 5.3 metamethods into separate files. Fixes 5.3 errors

This commit is contained in:
kikito 2015-11-01 17:09:15 +01:00
parent cd8ce449e1
commit a5a334c775
3 changed files with 201 additions and 123 deletions

View File

@ -0,0 +1,87 @@
local class = require 'middleclass'
local it = require('busted').it
local describe = require('busted').describe
local before_each = require('busted').before_each
local assert = require('busted').assert
describe('Lua 5.2 Metamethods', function()
local Vector, a, b
before_each(function()
Vector= class('Vector')
function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
function Vector.__len(a) return 3 end
function Vector.__pairs(a)
local t = {x=a.x,y=a.y,z=a.z}
return coroutine.wrap(function()
for k,v in pairs(t) do
coroutine.yield(k,v)
end
end)
end
function Vector.__ipairs(a)
local t = {a.x,a.y,a.z}
return coroutine.wrap(function()
for k,v in ipairs(t) do
coroutine.yield(k,v)
end
end)
end
a = Vector:new(1,2,3)
b = Vector:new(2,4,6)
end)
it('implements __len', function()
assert.equal(#a, 3)
end)
it('implements __pairs',function()
local output = {}
for k,v in pairs(a) do
output[k] = v
end
assert.are.same(output,{x=1,y=2,z=3})
end)
it('implements __ipairs',function()
local output = {}
for _,i in ipairs(a) do
output[#output+1] = i
end
assert.are.same(output,{1,2,3})
end)
describe('Inherited Metamethods', function()
local Vector2, c, d
before_each(function()
Vector2= class('Vector2', Vector)
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
c = Vector2:new(1,2,3)
d = Vector2:new(2,4,6)
end)
it('implements __len', function()
assert.equal(#c, 3)
end)
it('implements __pairs',function()
local output = {}
for k,v in pairs(c) do
output[k] = v
end
assert.are.same(output,{x=1,y=2,z=3})
end)
it('implements __ipairs',function()
local output = {}
for _,i in ipairs(c) do
output[#output+1] = i
end
assert.are.same(output,{1,2,3})
end)
end)
end)

View File

@ -0,0 +1,106 @@
local class = require 'middleclass'
local it = require('busted').it
local describe = require('busted').describe
local before_each = require('busted').before_each
local assert = require('busted').assert
describe('Lua 5.3 Metamethods', function()
local Vector, a, b
before_each(function()
Vector= class('Vector')
function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end
function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.z end
function Vector.__pairs(a)
local t = {x=a.x,y=a.y,z=a.z}
return coroutine.wrap(function()
for k,v in pairs(t) do
coroutine.yield(k,v)
end
end)
end
function Vector.__len(a) return 3 end
function Vector.__gc(a) b.x, b.y, b.z = a.x, a.y, a.z end
function Vector.__band(a,n) return a.class:new(a.x & n, a.y & n, a.z & n) end
function Vector.__bor(a,n) return a.class:new(a.x | n, a.y | n, a.z | n) end
function Vector.__bxor(a,n) return a.class:new(a.x ~ n, a.y ~ n, a.z ~ n) end
function Vector.__shl(a,n) return a.class:new(a.x << n, a.y << n, a.z << n) end
function Vector.__shr(a,n) return a.class:new(a.x >> n, a.y >> n, a.z >> n) end
function Vector.__bnot(a) return a.class:new(~a.x, ~a.y, ~a.z) end
a = Vector:new(1,2,3)
b = Vector:new(2,4,6)
end)
it('implements __gc', function()
a = nil
collectgarbage()
assert.are.same({b.x, b.y, b.z}, {1,2,3})
end)
it('implements __band', function()
assert.equal(a & 1, Vector(1,0,1))
end)
it('implements __bor', function()
assert.equal(a | 0, Vector(1,2,3))
end)
it('implements __bxor', function()
assert.equal(a | 1, Vector(1,3,3))
end)
it('implements __shl', function()
assert.equal(a << 1, Vector(2,4,6))
end)
it('implements __shr', function()
assert.equal(a >> 1, Vector(0,1,1))
end)
it('implements __bnot', function()
assert.equal(~a, Vector(-2,-3,-4))
end)
describe('Inherited Metamethods', function()
local Vector2, c, d
before_each(function()
Vector2= class('Vector2', Vector)
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
c = Vector2:new(1,2,3)
d = Vector2:new(2,4,6)
end)
it('implements __gc', function()
c = nil
collectgarbage()
assert.are.same({b.x, b.y, b.z}, {1,2,3})
end)
it('implements __band', function()
assert.equal(c & 1, Vector2(1,0,1))
end)
it('implements __bor', function()
assert.equal(c | 0, Vector2(1,2,3))
end)
it('implements __bxor', function()
assert.equal(c | 1, Vector2(1,3,3))
end)
it('implements __shl', function()
assert.equal(c << 1, Vector2(2,4,6))
end)
it('implements __shr', function()
assert.equal(c >> 1, Vector2(0,1,1))
end)
it('implements __bnot', function()
assert.equal(~c, Vector2(-2,-3,-4))
end)
end)
end)

View File

@ -9,8 +9,15 @@ local function is_lua_5_3_compatible()
return type(string.unpack) == 'function'
end
describe('Metamethods', function()
if is_lua_5_2_compatible() then
require 'spec/metamethods_lua_5_2'
end
if is_lua_5_3_compatible() then
require 'spec.metamethods_lua_5_3'
end
describe('Metamethods', function()
describe('Custom Metamethods', function()
local Vector, a, b
before_each(function()
@ -34,40 +41,6 @@ describe('Metamethods', function()
if type(a)=="number" then return b.class:new(a*b.x, a*b.y, a*b.z) end
end
if is_lua_5_2_compatible() then
function Vector.__len(a) return 3 end
function Vector.__pairs(a)
local t = {x=a.x,y=a.y,z=a.z}
return coroutine.wrap(function()
for k,v in pairs(t) do
coroutine.yield(k,v)
end
end)
end
function Vector.__ipairs(a)
local t = {a.x,a.y,a.z}
return coroutine.wrap(function()
for k,v in ipairs(t) do
coroutine.yield(k,v)
end
end)
end
end
if is_lua_5_3_compatible() then
eval([[
function Vector.__gc(a)
b.x, b.y, b.z = a.x, a.y, a.z
end
function Vector.__band(a,n) return a.class:new(a.x & n, a.y & n, a.z & n) end
function Vector.__bor(a,n) return a.class:new(a.x | n, a.y | n, a.z | n) end
function Vector.__bxor(a,n) return a.class:new(a.x ~ n, a.y ~ n, a.z ~ n) end
function Vector.__shl(a,n) return a.class:new(a.x << n, a.y << n, a.z << n) end
function Vector.__shr(a,n) return a.class:new(a.x >> n, a.y >> n, a.z >> n) end
function Vector.__bnot(a) return a.class:new(~a.x, ~a.y, ~a.z) end
]])
end
a = Vector:new(1,2,3)
b = Vector:new(2,4,6)
end)
@ -122,65 +95,8 @@ describe('Metamethods', function()
end)
--]]
if is_lua_5_2_compatible() then
it('implements __len', function()
assert.equal(#a, 3)
end)
it('implements __pairs',function()
local output = {}
for k,v in pairs(a) do
output[k] = v
end
assert.are.same(output,{x=1,y=2,z=3})
end)
it('implements __ipairs',function()
local output = {}
for _,i in ipairs(a) do
output[#output+1] = i
end
assert.are.same(output,{1,2,3})
end)
end
if is_lua_5_3_compatible() then
eval([[
it('implements __gc', function()
a = nil
collectgarbage()
assert.are.same({b.x, b.y, b.z}, {1,2,3})
end)
it('implements __band', function()
assert.equal(a & 1, Vector(1,1,1))
end)
it('implements __bor', function()
assert.equal(a | 0, Vector(1,2,3))
end)
it('implements __bxor', function()
assert.equal(a | 0, Vector(0,0,0))
end)
it('implements __shl', function()
assert.equal(a << 1, Vector(0,0,0))
end)
it('implements __shr', function()
assert.equal(a >> 1, Vector(0,0,0))
end)
it('implements __bnot', function()
assert.equal(~a, Vector(0,0,0))
end)
]])
end
describe('Inherited Metamethods', function()
local Vector2, c, d
before_each(function()
Vector2= class('Vector2', Vector)
function Vector2:initialize(x,y,z) Vector.initialize(self,x,y,z) end
@ -232,37 +148,6 @@ describe('Metamethods', function()
it('implements __mul', function()
assert.equal(4*c, Vector2(4,8,12))
end)
if is_lua_5_2_compatible() then
it('implements __len', function()
assert.equal(#c, 3)
end)
it('implements __pairs',function()
local output = {}
for k,v in pairs(c) do
output[k] = v
end
assert.are.same(output,{x=1,y=2,z=3})
end)
it('implements __ipairs',function()
local output = {}
for _,i in ipairs(c) do
output[#output+1] = i
end
assert.are.same(output,{1,2,3})
end)
end
if is_lua_5_3_compatible() then
it('implements __gc', function()
c = nil
collectgarbage()
assert.are.same({b.x, b.y, b.z}, {1,2,3})
end)
end
end)
end)