diff --git a/.travis.yml b/.travis.yml index 91f7f93..53998d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,7 +20,7 @@ install: - luarocks install luacov-coveralls script: - - luacheck --std max+busted *.lua spec + - luacheck --no-unused-args --std max+busted *.lua spec - busted --verbose --coverage after_success: diff --git a/middleclass.lua b/middleclass.lua index 1c793e7..90fda19 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -138,8 +138,8 @@ local DefaultMixin = { local subclass = _createClass(name, self) - for name, f in pairs(self.__instanceDict) do - _propagateInstanceMethod(subclass, name, f) + for methodName, f in pairs(self.__instanceDict) do + _propagateInstanceMethod(subclass, methodName, f) end subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end diff --git a/spec/class_spec.lua b/spec/class_spec.lua index 5924437..144cb9f 100644 --- a/spec/class_spec.lua +++ b/spec/class_spec.lua @@ -1,5 +1,4 @@ local class = require 'middleclass' -local Object = class.Object describe('class()', function() diff --git a/spec/default_methods_spec.lua b/spec/default_methods_spec.lua index d7c8e86..81602f0 100644 --- a/spec/default_methods_spec.lua +++ b/spec/default_methods_spec.lua @@ -204,7 +204,7 @@ describe('Default methods', function() describe('A ' .. theType, function() local f1 = function() return Object.isSubclassOf(Object, primitive) end - local f2 = function() return Object.isSubclassOf(primitive, o) end + local f2 = function() return Object.isSubclassOf(primitive, Object:new()) end local f3 = function() return Object.isSubclassOf(primitive, primitive) end describe('does not throw errors', function() diff --git a/spec/metamethods_lua_5_2.lua b/spec/metamethods_lua_5_2.lua index 7b119b1..2ea6c9b 100644 --- a/spec/metamethods_lua_5_2.lua +++ b/spec/metamethods_lua_5_2.lua @@ -6,7 +6,7 @@ local before_each = require('busted').before_each local assert = require('busted').assert describe('Lua 5.2 Metamethods', function() - local Vector, a, b + local Vector, v before_each(function() Vector= class('Vector') function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end @@ -16,69 +16,67 @@ describe('Lua 5.2 Metamethods', function() 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) + for k,val in pairs(t) do + coroutine.yield(k,val) 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) + for k,val in ipairs(t) do + coroutine.yield(k,val) end end) end - a = Vector:new(1,2,3) - b = Vector:new(2,4,6) + v = Vector:new(1,2,3) end) it('implements __len', function() - assert.equal(#a, 3) + assert.equal(#v, 3) end) it('implements __pairs',function() local output = {} - for k,v in pairs(a) do - output[k] = v + for k,val in pairs(v) do + output[k] = val end assert.are.same(output,{x=1,y=2,z=3}) end) it('implements __ipairs',function() local output = {} - for _,i in ipairs(a) do + for _,i in ipairs(v) do output[#output+1] = i end assert.are.same(output,{1,2,3}) end) describe('Inherited Metamethods', function() - local Vector2, c, d + local Vector2, v2 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) + v2 = Vector2:new(1,2,3) end) it('implements __len', function() - assert.equal(#c, 3) + assert.equal(#v2, 3) end) it('implements __pairs',function() local output = {} - for k,v in pairs(c) do - output[k] = v + for k,val in pairs(v2) do + output[k] = val end assert.are.same(output,{x=1,y=2,z=3}) end) it('implements __ipairs',function() local output = {} - for _,i in ipairs(c) do + for _,i in ipairs(v2) do output[#output+1] = i end assert.are.same(output,{1,2,3}) diff --git a/spec/metamethods_lua_5_3.lua b/spec/metamethods_lua_5_3.lua index 8d926a3..e74f6d7 100644 --- a/spec/metamethods_lua_5_3.lua +++ b/spec/metamethods_lua_5_3.lua @@ -6,7 +6,7 @@ local before_each = require('busted').before_each local assert = require('busted').assert describe('Lua 5.3 Metamethods', function() - local Vector, a, b, last_gc + local Vector, v, last_gc before_each(function() Vector= class('Vector') function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end @@ -14,8 +14,8 @@ describe('Lua 5.3 Metamethods', function() 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) + for k,val in pairs(t) do + coroutine.yield(k,val) end end) end @@ -29,80 +29,78 @@ describe('Lua 5.3 Metamethods', function() 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) + v = Vector:new(1,2,3) end) it('implements __gc', function() collectgarbage() - a = nil + v = nil collectgarbage() assert.are.same(last_gc, {"Vector",1,2,3}) end) it('implements __band', function() - assert.equal(a & 1, Vector(1,0,1)) + assert.equal(v & 1, Vector(1,0,1)) end) it('implements __bor', function() - assert.equal(a | 0, Vector(1,2,3)) + assert.equal(v | 0, Vector(1,2,3)) end) it('implements __bxor', function() - assert.equal(a | 1, Vector(1,3,3)) + assert.equal(v | 1, Vector(1,3,3)) end) it('implements __shl', function() - assert.equal(a << 1, Vector(2,4,6)) + assert.equal(v << 1, Vector(2,4,6)) end) it('implements __shr', function() - assert.equal(a >> 1, Vector(0,1,1)) + assert.equal(v >> 1, Vector(0,1,1)) end) it('implements __bnot', function() - assert.equal(~a, Vector(-2,-3,-4)) + assert.equal(~v, Vector(-2,-3,-4)) end) describe('Inherited Metamethods', function() - local Vector2, c, d + local Vector2, v2 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) + v2 = Vector2:new(1,2,3) end) it('implements __gc', function() collectgarbage() - c = nil + v2 = nil collectgarbage() assert.are.same(last_gc, {"Vector2",1,2,3}) end) it('implements __band', function() - assert.equal(c & 1, Vector2(1,0,1)) + assert.equal(v2 & 1, Vector2(1,0,1)) end) it('implements __bor', function() - assert.equal(c | 0, Vector2(1,2,3)) + assert.equal(v2 | 0, Vector2(1,2,3)) end) it('implements __bxor', function() - assert.equal(c | 1, Vector2(1,3,3)) + assert.equal(v2 | 1, Vector2(1,3,3)) end) it('implements __shl', function() - assert.equal(c << 1, Vector2(2,4,6)) + assert.equal(v2 << 1, Vector2(2,4,6)) end) it('implements __shr', function() - assert.equal(c >> 1, Vector2(0,1,1)) + assert.equal(v2 >> 1, Vector2(0,1,1)) end) it('implements __bnot', function() - assert.equal(~c, Vector2(-2,-3,-4)) + assert.equal(~v2, Vector2(-2,-3,-4)) end) end) end) diff --git a/spec/metamethods_spec.lua b/spec/metamethods_spec.lua index 6e8277e..73bf883 100644 --- a/spec/metamethods_spec.lua +++ b/spec/metamethods_spec.lua @@ -1,5 +1,4 @@ local class = require 'middleclass' -local Object = class.Object local function is_lua_5_2_compatible() return type(rawlen) == 'function' @@ -19,7 +18,7 @@ end describe('Metamethods', function() describe('Custom Metamethods', function() - local Vector, a, b + local Vector, v, w before_each(function() Vector= class('Vector') function Vector.initialize(a,x,y,z) a.x, a.y, a.z = x,y,z end @@ -43,62 +42,62 @@ describe('Metamethods', function() Vector.__metatable = "metatable of a vector" Vector.__mode = "k" - a = Vector:new(1,2,3) - b = Vector:new(2,4,6) + v = Vector:new(1,2,3) + w = Vector:new(2,4,6) end) it('implements __tostring', function() - assert.equal(tostring(a), "Vector[1,2,3]") + assert.equal(tostring(v), "Vector[1,2,3]") end) it('implements __eq', function() - assert.equal(a, a) + assert.equal(v, v) end) it('implements __lt', function() - assert.is_true(a < b) + assert.is_true(v < w) end) it('implements __le', function() - assert.is_true(a <= b) + assert.is_true(v <= w) end) it('implements __add', function() - assert.equal(a+b, Vector(3,6,9)) + assert.equal(v+w, Vector(3,6,9)) end) it('implements __sub', function() - assert.equal(b-a, Vector(1,2,3)) + assert.equal(w-v, Vector(1,2,3)) end) it('implements __div', function() - assert.equal(b/2, Vector(1,2,3)) + assert.equal(w/2, Vector(1,2,3)) end) it('implements __concat', function() - assert.equal(a..b, 28) + assert.equal(v..w, 28) end) it('implements __call', function() - assert.equal(a(), math.sqrt(14)) + assert.equal(v(), math.sqrt(14)) end) it('implements __pow', function() - assert.equal(a^b, Vector(0,0,0)) + assert.equal(v^w, Vector(0,0,0)) end) it('implements __mul', function() - assert.equal(4*a, Vector(4,8,12)) + assert.equal(4*v, Vector(4,8,12)) end) it('implements __metatable', function() - assert.equal("metatable of a vector", getmetatable(a)) + assert.equal("metatable of a vector", getmetatable(v)) end) it('implements __mode', function() - a[{}] = true + v[{}] = true collectgarbage() - for k in pairs(a) do assert.not_table(k) end + for k in pairs(v) do assert.not_table(k) end end) --[[ @@ -108,97 +107,97 @@ describe('Metamethods', function() --]] describe('Inherited Metamethods', function() - local Vector2, c, d + local Vector2, v2, w2 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) + v2 = Vector2:new(1,2,3) + w2 = Vector2:new(2,4,6) end) it('implements __tostring', function() - assert.equal(tostring(c), "Vector2[1,2,3]") + assert.equal(tostring(v2), "Vector2[1,2,3]") end) it('implements __eq', function() - assert.equal(c, c) + assert.equal(v2, v2) end) it('implements __lt', function() - assert.is_true(c < d) + assert.is_true(v2 < w2) end) it('implements __le', function() - assert.is_true(c <= d) + assert.is_true(v2 <= w2) end) it('implements __add', function() - assert.equal(c+d, Vector2(3,6,9)) + assert.equal(v2+w2, Vector2(3,6,9)) end) it('implements __sub', function() - assert.equal(d-c, Vector2(1,2,3)) + assert.equal(w2-v2, Vector2(1,2,3)) end) it('implements __div', function() - assert.equal(d/2, Vector2(1,2,3)) + assert.equal(w2/2, Vector2(1,2,3)) end) it('implements __concat', function() - assert.equal(c..d, 28) + assert.equal(v2..w2, 28) end) it('implements __call', function() - assert.equal(c(), math.sqrt(14)) + assert.equal(v2(), math.sqrt(14)) end) it('implements __pow', function() - assert.equal(c^d, Vector2(0,0,0)) + assert.equal(v2^w2, Vector2(0,0,0)) end) it('implements __mul', function() - assert.equal(4*c, Vector2(4,8,12)) + assert.equal(4*v2, Vector2(4,8,12)) end) it('implements __metatable', function() - assert.equal("metatable of a vector", getmetatable(c)) + assert.equal("metatable of a vector", getmetatable(v2)) end) it('implements __mode', function() - c[{}] = true + v2[{}] = true collectgarbage() - for k in pairs(c) do assert.not_table(k) end + for k in pairs(v2) do assert.not_table(k) end end) it('allows inheriting further', function() local Vector3 = class('Vector3', Vector2) - local e = Vector3(1,2,3) - local f = Vector3(3,4,5) - assert.equal(e+f, Vector3(4,6,8)) + local v3 = Vector3(1,2,3) + local w3 = Vector3(3,4,5) + assert.equal(v3+w3, Vector3(4,6,8)) end) describe('Updates', function() it('overrides __add', function() Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end - assert.equal(c+d, Vector2(1.5,3,4.5)) + assert.equal(v2+w2, Vector2(1.5,3,4.5)) end) it('updates __add', function() Vector.__add = Vector.__sub - assert.equal(c+d, Vector2(-1,-2,-3)) + assert.equal(v2+w2, Vector2(-1,-2,-3)) end) it('does not update __add after overriding', function() Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end Vector.__add = Vector.__sub - assert.equal(c+d, Vector2(-0.5,-1,-1.5)) + assert.equal(v2+w2, Vector2(-0.5,-1,-1.5)) end) it('reverts __add override', function() Vector2.__add = function(a, b) return Vector.__add(a, b)/2 end Vector2.__add = nil - assert.equal(c+d, Vector2(3,6,9)) + assert.equal(v2+w2, Vector2(3,6,9)) end) end) end)