2013-09-15 21:14:00 +00:00
|
|
|
local class = require 'middleclass'
|
2013-09-15 21:18:08 +00:00
|
|
|
local Object = class.Object
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-12-05 00:15:28 +00:00
|
|
|
local function is_lua_5_2_compatible()
|
|
|
|
return type(rawlen) == 'function'
|
|
|
|
end
|
|
|
|
|
2015-10-12 11:39:35 +00:00
|
|
|
local function is_lua_5_3_compatible()
|
|
|
|
return type(string.unpack) == 'function'
|
|
|
|
end
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('Metamethods', function()
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('Custom Metamethods', function()
|
2015-10-12 11:49:50 +00:00
|
|
|
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.__tostring(a) return a.class.name .. '[' .. a.x .. ',' .. a.y .. ',' .. a.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.__le(a,b) return a() <= b() end
|
2015-10-31 15:50:11 +00:00
|
|
|
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 a.class:new(a.x-b.x, a.y-b.y, a.z-b.z) 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 a.class:new(-a.x, -a.y, -a.z) end
|
2015-10-12 11:49:50 +00:00
|
|
|
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.__pow(a,b)
|
2015-10-31 16:16:10 +00:00
|
|
|
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)
|
2015-10-12 11:49:50 +00:00
|
|
|
end
|
|
|
|
function Vector.__mul(a,b)
|
2015-10-31 15:50:11 +00:00
|
|
|
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 b.class:new(a*b.x, a*b.y, a*b.z) end
|
2015-10-12 11:49:50 +00:00
|
|
|
end
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 15:53:19 +00:00
|
|
|
if is_lua_5_2_compatible() then
|
2015-10-31 15:50:11 +00:00
|
|
|
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
|
2015-10-12 11:49:50 +00:00
|
|
|
end
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 15:53:19 +00:00
|
|
|
if is_lua_5_3_compatible() then
|
2015-10-31 16:16:10 +00:00
|
|
|
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
|
|
|
|
]])
|
2015-10-12 11:49:50 +00:00
|
|
|
end
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2015-10-12 11:49:50 +00:00
|
|
|
a = Vector:new(1,2,3)
|
|
|
|
b = Vector:new(2,4,6)
|
|
|
|
end)
|
2011-08-12 08:33:58 +00:00
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __tostring', function()
|
|
|
|
assert.equal(tostring(a), "Vector[1,2,3]")
|
|
|
|
end)
|
2015-10-12 11:39:35 +00:00
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __eq', function()
|
|
|
|
assert.equal(a, a)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __lt', function()
|
|
|
|
assert.is_true(a < b)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __le', function()
|
|
|
|
assert.is_true(a <= b)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __add', function()
|
|
|
|
assert.equal(a+b, Vector(3,6,9))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __sub', function()
|
|
|
|
assert.equal(b-a, Vector(1,2,3))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __div', function()
|
|
|
|
assert.equal(b/2, Vector(1,2,3))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __concat', function()
|
|
|
|
assert.equal(a..b, 28)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __call', function()
|
|
|
|
assert.equal(a(), math.sqrt(14))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __pow', function()
|
|
|
|
assert.equal(a^b, Vector(0,0,0))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __mul', function()
|
|
|
|
assert.equal(4*a, Vector(4,8,12))
|
|
|
|
end)
|
|
|
|
|
|
|
|
--[[
|
|
|
|
it('implements __index', function()
|
|
|
|
assert.equal(b[1], 3)
|
|
|
|
end)
|
|
|
|
--]]
|
2013-12-05 00:15:28 +00:00
|
|
|
|
|
|
|
if is_lua_5_2_compatible() then
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __len', function()
|
|
|
|
assert.equal(#a, 3)
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
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})
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __ipairs',function()
|
|
|
|
local output = {}
|
|
|
|
for _,i in ipairs(a) do
|
|
|
|
output[#output+1] = i
|
|
|
|
end
|
|
|
|
assert.are.same(output,{1,2,3})
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
end
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2015-10-12 11:39:35 +00:00
|
|
|
if is_lua_5_3_compatible() then
|
2015-10-31 16:16:10 +00:00
|
|
|
eval([[
|
|
|
|
it('implements __gc', function()
|
|
|
|
a = nil
|
|
|
|
collectgarbage()
|
|
|
|
assert.are.same({b.x, b.y, b.z}, {1,2,3})
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __band', function()
|
|
|
|
assert.equal(a & 1, Vector(1,1,1))
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __bor', function()
|
|
|
|
assert.equal(a | 0, Vector(1,2,3))
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __bxor', function()
|
|
|
|
assert.equal(a | 0, Vector(0,0,0))
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __shl', function()
|
|
|
|
assert.equal(a << 1, Vector(0,0,0))
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __shr', function()
|
|
|
|
assert.equal(a >> 1, Vector(0,0,0))
|
|
|
|
end)
|
2015-10-31 15:50:11 +00:00
|
|
|
|
2015-10-31 16:16:10 +00:00
|
|
|
it('implements __bnot', function()
|
|
|
|
assert.equal(~a, Vector(0,0,0))
|
|
|
|
end)
|
|
|
|
]])
|
2015-10-12 11:39:35 +00:00
|
|
|
end
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('Inherited Metamethods', function()
|
2015-10-12 21:45:32 +00:00
|
|
|
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 __tostring', function()
|
|
|
|
assert.equal(tostring(c), "Vector2[1,2,3]")
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __eq', function()
|
|
|
|
assert.equal(c, c)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __lt', function()
|
|
|
|
assert.is_true(c < d)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __le', function()
|
|
|
|
assert.is_true(c <= d)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __add', function()
|
2015-10-31 16:16:10 +00:00
|
|
|
assert.equal(c+d, Vector2(3,6,9))
|
2015-10-12 21:45:32 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __sub', function()
|
2015-10-31 16:16:10 +00:00
|
|
|
assert.equal(d-c, Vector2(1,2,3))
|
2015-10-12 21:45:32 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __div', function()
|
2015-10-31 16:16:10 +00:00
|
|
|
assert.equal(d/2, Vector2(1,2,3))
|
2015-10-12 21:45:32 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __concat', function()
|
|
|
|
assert.equal(c..d, 28)
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __call', function()
|
|
|
|
assert.equal(c(), math.sqrt(14))
|
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __pow', function()
|
2015-10-31 16:16:10 +00:00
|
|
|
assert.equal(c^d, Vector2(0,0,0))
|
2015-10-12 21:45:32 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it('implements __mul', function()
|
2015-10-31 16:16:10 +00:00
|
|
|
assert.equal(4*c, Vector2(4,8,12))
|
2015-10-12 21:45:32 +00:00
|
|
|
end)
|
2013-12-05 00:15:28 +00:00
|
|
|
|
|
|
|
if is_lua_5_2_compatible() then
|
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __len', function()
|
|
|
|
assert.equal(#c, 3)
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
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})
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
it('implements __ipairs',function()
|
|
|
|
local output = {}
|
|
|
|
for _,i in ipairs(c) do
|
|
|
|
output[#output+1] = i
|
|
|
|
end
|
|
|
|
assert.are.same(output,{1,2,3})
|
2013-12-05 00:15:28 +00:00
|
|
|
end)
|
|
|
|
end
|
2013-12-04 10:09:54 +00:00
|
|
|
|
2015-10-12 21:45:32 +00:00
|
|
|
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
|
2011-08-12 08:33:58 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('Default Metamethods', function()
|
2011-08-12 08:33:58 +00:00
|
|
|
|
|
|
|
local Peter, peter
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
before_each(function()
|
2011-08-12 08:33:58 +00:00
|
|
|
Peter = class('Peter')
|
|
|
|
peter = Peter()
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('A Class', function()
|
|
|
|
it('has a call metamethod properly set', function()
|
|
|
|
assert.is_true(peter:isInstanceOf(Peter))
|
2011-08-12 08:33:58 +00:00
|
|
|
end)
|
2013-09-17 18:55:50 +00:00
|
|
|
it('has a tostring metamethod properly set', function()
|
|
|
|
assert.equal(tostring(Peter), 'class Peter')
|
2011-08-12 08:33:58 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2013-09-17 18:55:50 +00:00
|
|
|
describe('An instance', function()
|
|
|
|
it('has a tostring metamethod, returning a different result from Object.__tostring', function()
|
|
|
|
assert.not_equal(Peter.__tostring, Object.__tostring)
|
|
|
|
assert.equal(tostring(peter), 'instance of class Peter')
|
2011-08-12 08:33:58 +00:00
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|