adds failing lua 5.3 metamethods tests

This commit is contained in:
kikito 2015-10-31 16:50:11 +01:00
parent d6b5f5c22d
commit 4c1f15c055

View File

@ -20,38 +20,50 @@ describe('Metamethods', function()
function Vector.__eq(a,b) return a.x==b.x and a.y==b.y and a.z==b.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.__lt(a,b) return a() < b() end function Vector.__lt(a,b) return a() < b() end
function Vector.__le(a,b) return a() <= b() end function Vector.__le(a,b) return a() <= b() end
function Vector.__add(a,b) return Vector:new(a.x+b.x, a.y+b.y ,a.z+b.z) end function Vector.__add(a,b) return a.class:new(a.x+b.x, a.y+b.y ,a.z+b.z) end
function Vector.__sub(a,b) return Vector:new(a.x-b.x, a.y-b.y, a.z-b.z) end function Vector.__sub(a,b) return a.class:new(a.x-b.x, a.y-b.y, a.z-b.z) end
function Vector.__div(a,s) return Vector:new(a.x/s, a.y/s, a.z/s) end function Vector.__div(a,s) return a.class:new(a.x/s, a.y/s, a.z/s) end
function Vector.__unm(a) return Vector:new(-a.x, -a.y, -a.z) end function Vector.__unm(a) return a.class:new(-a.x, -a.y, -a.z) end
function Vector.__concat(a,b) return a.x*b.x+a.y*b.y+a.z*b.z end function Vector.__concat(a,b) return a.x*b.x+a.y*b.y+a.z*b.z end
function Vector.__call(a) return math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z) end function Vector.__call(a) return math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z) end
function Vector.__pow(a,b) function Vector.__pow(a,b)
return Vector: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) return Vector: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)
end end
function Vector.__mul(a,b) function Vector.__mul(a,b)
if type(b)=="number" then return Vector:new(a.x*b, a.y*b, a.z*b) end if type(b)=="number" then return a.class:new(a.x*b, a.y*b, a.z*b) end
if type(a)=="number" then return Vector:new(a*b.x, a*b.y, a*b.z) end if type(a)=="number" then return b.class:new(a*b.x, a*b.y, a*b.z) end
end end
function Vector.__len(a) return 3 end
function Vector.__pairs(a) if is_lua_5_2_compatible then
local t = {x=a.x,y=a.y,z=a.z} function Vector.__len(a) return 3 end
return coroutine.wrap(function() function Vector.__pairs(a)
for k,v in pairs(t) do local t = {x=a.x,y=a.y,z=a.z}
coroutine.yield(k,v) return coroutine.wrap(function()
end for k,v in pairs(t) do
end) 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 end
function Vector.__ipairs(a)
local t = {a.x,a.y,a.z} if is_lua_5_3_compatible then
return coroutine.wrap(function() function Vector.__gc(a)
for k,v in ipairs(t) do b.x, b.y, b.z = a.x, a.y, a.z
coroutine.yield(k,v) end
end function Vector.__band(a,n) return a.class:new(a.x & n, a.y & n, a.z & n) end
end) function Vector.__bor(a,n) return a.class:new(a.x | n, a.y | n, a.z | n) end
end function Vector.__bxor(a,n) return a.class:new(a.x ~ n, a.y ~ n, a.z ~ n) end
function Vector.__gc(a) function Vector.__shl(a,n) return a.class:new(a.x << n, a.y << n, a.z << n) end
b.x, b.y, b.z = a.x, a.y, a.z 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 end
a = Vector:new(1,2,3) a = Vector:new(1,2,3)
@ -109,7 +121,6 @@ describe('Metamethods', function()
--]] --]]
if is_lua_5_2_compatible() then if is_lua_5_2_compatible() then
it('implements __len', function() it('implements __len', function()
assert.equal(#a, 3) assert.equal(#a, 3)
end) end)
@ -132,11 +143,37 @@ describe('Metamethods', function()
end end
if is_lua_5_3_compatible() then if is_lua_5_3_compatible() then
it('implements __gc', function() it('implements __gc', function()
a = nil a = nil
collectgarbage() collectgarbage()
assert.are.same({b.x, b.y, b.z}, {1,2,3}) assert.are.same({b.x, b.y, b.z}, {1,2,3})
end) 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 end
describe('Inherited Metamethods', function() describe('Inherited Metamethods', function()