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-09-16 08:37:56 +00:00
|
|
|
context('Object.isInstanceOf', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
|
|
|
|
context('nils, integers, strings, tables, and functions', function()
|
|
|
|
local o = Object:new()
|
|
|
|
local primitives = {nil, 1, 'hello', {}, function() end}
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
for _,primitive in pairs(primitives) do
|
|
|
|
local theType = type(primitive)
|
|
|
|
context('A ' .. theType, function()
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-16 08:37:56 +00:00
|
|
|
local f1 = function() return Object.isInstanceOf(primitive, Object) end
|
|
|
|
local f2 = function() return Object.isInstanceOf(primitive, o) end
|
|
|
|
local f3 = function() return Object.isInstanceOf(primitive, primitive) end
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
context('does not throw errors', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
test('instanceOf(Object, '.. theType ..')', function()
|
|
|
|
assert_not_error(f1)
|
|
|
|
end)
|
|
|
|
test('instanceOf(' .. theType .. ', Object:new())', function()
|
|
|
|
assert_not_error(f2)
|
|
|
|
end)
|
|
|
|
test('instanceOf(' .. theType .. ',' .. theType ..')', function()
|
|
|
|
assert_not_error(f3)
|
|
|
|
end)
|
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
test('makes instanceOf return false', function()
|
2011-08-10 19:45:15 +00:00
|
|
|
assert_false(f1())
|
|
|
|
assert_false(f2())
|
|
|
|
assert_false(f3())
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
end
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
context('An instance', function()
|
|
|
|
local Class1 = class('Class1')
|
|
|
|
local Class2 = class('Class2', Class1)
|
|
|
|
local Class3 = class('Class3', Class2)
|
|
|
|
local UnrelatedClass = class('Unrelated')
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
local o1, o2, o3 = Class1:new(), Class2:new(), Class3:new()
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-16 08:37:56 +00:00
|
|
|
test('isInstanceOf(Object)', function()
|
|
|
|
assert_true(o1:isInstanceOf(Object))
|
|
|
|
assert_true(o2:isInstanceOf(Object))
|
|
|
|
assert_true(o3:isInstanceOf(Object))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2013-09-16 08:37:56 +00:00
|
|
|
test('isInstanceOf its class', function()
|
|
|
|
assert_true(o1:isInstanceOf(Class1))
|
|
|
|
assert_true(o2:isInstanceOf(Class2))
|
|
|
|
assert_true(o3:isInstanceOf(Class3))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
test('is instanceOf its class\' superclasses', function()
|
2013-09-16 08:37:56 +00:00
|
|
|
assert_true(o2:isInstanceOf(Class1))
|
|
|
|
assert_true(o3:isInstanceOf(Class1))
|
|
|
|
assert_true(o3:isInstanceOf(Class2))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
test('is not instanceOf its class\' subclasses', function()
|
2013-09-16 08:37:56 +00:00
|
|
|
assert_false(o1:isInstanceOf(Class2))
|
|
|
|
assert_false(o1:isInstanceOf(Class3))
|
|
|
|
assert_false(o2:isInstanceOf(Class3))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
2013-09-15 21:14:00 +00:00
|
|
|
|
2011-08-12 08:33:58 +00:00
|
|
|
test('is not instanceOf an unrelated class', function()
|
2013-09-16 08:37:56 +00:00
|
|
|
assert_false(o1:isInstanceOf(UnrelatedClass))
|
|
|
|
assert_false(o2:isInstanceOf(UnrelatedClass))
|
|
|
|
assert_false(o3:isInstanceOf(UnrelatedClass))
|
2011-08-10 19:45:15 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
end)
|