diff --git a/spec/metamethods_spec.lua b/spec/metamethods_spec.lua index 0d33d6a..93becaa 100644 --- a/spec/metamethods_spec.lua +++ b/spec/metamethods_spec.lua @@ -1,6 +1,10 @@ local class = require 'middleclass' local Object = class.Object +local function is_lua_5_2_compatible() + return type(rawlen) == 'function' +end + describe('Metamethods', function() describe('Custom Metamethods', function() @@ -26,7 +30,7 @@ describe('Metamethods', function() 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() + return coroutine.wrap(function() for k,v in pairs(t) do coroutine.yield(k,v) end @@ -34,7 +38,7 @@ describe('Metamethods', function() end function Vector.__ipairs(a) local t = {a.x,a.y,a.z} - return coroutine.wrap(function() + return coroutine.wrap(function() for k,v in ipairs(t) do coroutine.yield(k,v) end @@ -56,28 +60,45 @@ describe('Metamethods', function() __concat = { a..b, 28 }, __call = { a(), math.sqrt(14) }, __pow = { a^b, Vector(0,0,0) }, - __mul = { 4*a, Vector(4,8,12) }, - __len = { #a, 3}, + __mul = { 4*a, Vector(4,8,12) } --__index = { b[1], 3 } }) do - it(metamethod .. ' works as expected', function() - assert.equal(values[1], values[2]) + describe(metamethod, function() + it('works as expected', function() + assert.equal(values[1], values[2]) + end) end) end - it('__pairs works as expected',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('__ipairs works as expected',function() - local output = {} - for _,i in ipairs(a) do - output[#output+1] = i - end - assert.are.same(output,{1,2,3}) - end) + + if is_lua_5_2_compatible() then + + describe('__len', function() + it('works as expected', function() + assert.equal(#a, 3) + end) + end) + + describe('__pairs', function() + it('works as expected',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) + end) + + describe('__ipairs', function() + it('works as expected',function() + local output = {} + for _,i in ipairs(a) do + output[#output+1] = i + end + assert.are.same(output,{1,2,3}) + end) + end) + + end describe('Inherited Metamethods', function() local Vector2= class('Vector2', Vector) @@ -98,26 +119,43 @@ describe('Metamethods', function() __call = { c(), math.sqrt(14) }, __pow = { c^d, Vector(0,0,0) }, __mul = { 4*c, Vector(4,8,12) }, - __len = { #c, 3}, }) do - it(metamethod .. ' works as expected', function() - assert.equal(values[1], values[2]) + describe(metamethod, function() + it('works as expected', function() + assert.equal(values[1], values[2]) + end) end) end - it('__pairs works as expected',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('__ipairs works as expected',function() - local output = {} - for _,i in ipairs(c) do - output[#output+1] = i - end - assert.are.same(output,{1,2,3}) - end) + + if is_lua_5_2_compatible() then + + describe('__len', function() + it('works as expected', function() + assert.equal(#c, 3) + end) + end) + + describe('__pairs', function() + it('works as expected',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) + end) + + describe('__ipairs', function() + it('works as expected', function() + local output = {} + for _,i in ipairs(c) do + output[#output+1] = i + end + assert.are.same(output,{1,2,3}) + end) + end) + + end end)