added more tests. fixed some minor issues on MindState

This commit is contained in:
kikito 2010-08-06 20:05:50 +02:00
parent 8cc514f19d
commit 0cdd3bacbb
2 changed files with 48 additions and 11 deletions

View File

@ -58,8 +58,12 @@ local _ignoredMethods = {
local _prevSubclass = StatefulObject.subclass -- previous way of creating subclasses (used to redefine subclass itself)
------------------------------------
-- STATE CLASS
------------------------------------
-- The State class; is the father of all State objects
local State = class('State', Object)
State = class('State', Object)
function State.subclass(theClass, name, theStatefulClass)
local theSubClass = Object.subclass(theClass, name)
@ -221,13 +225,13 @@ end
function StatefulObject:isInState(stateName, testStateStack)
local stack = _getStack(self)
if(testStateStack==true) then
if testStateStack == true then
for _,state in ipairs(stack) do
if(state.name == stateName) then return true end
if state.name == stateName then return true end
end
else --testStateStack==false
local state = stack[#stack]
if(state~=nil and state.name == stateName) then return true end
if state~=nil and state.name == stateName then return true end
end
return false
@ -282,12 +286,12 @@ function StatefulObject.subclass(theClass, name)
local classDict = theSubClass.__classDict
classDict.__index = function(instance, methodName)
-- If the method isn't on the 'ignoredMethods' list, look through the stack to see if it is defined
if(_ignoredMethods[methodName]~=1) then
if _ignoredMethods[methodName] ~= 1 then
local stack = _private[instance].stateStack
local method
for i = #stack,1,-1 do -- reversal loop
method = stack[i][methodName]
if(method~=nil) then return method end
if method ~= nil then return method end
end
end
--if ignored or not found, look on the class method
@ -314,7 +318,7 @@ function StatefulObject.includes(theClass, module, ...)
if type(module.states)=="table" then
for stateName,moduleState in pairs(module.states) do
local state = theClass.states[stateName]
if(state==nil) then state = theClass:addState(stateName) end
if state == nil then state = theClass:addState(stateName) end
state:includes(moduleState, ...)
end
end

View File

@ -145,14 +145,47 @@ context( 'StatefulObject', function()
context('When testing whether it is on one state', function()
local igor = Goblin:new()
test('This is pending', function() end)
test('it should return true if the state is on the top of the stack', function()
igor:gotoState('Iddle')
assert_true(igor:isInState('Iddle'))
end)
test('it should return true if the state is on the stack and "testStateStack" is true', function()
igor:pushState('Attacking')
assert_true(igor:isInState('Attacking'))
assert_true(igor:isInState('Iddle', true))
end)
test('it should return false otherwise', function()
assert_false(igor:isInState(nil))
assert_false(igor:isInState('Foo', true))
end)
end)
context('When getting the current state name', function()
local peppy = Goblin:new()
test('it should return nil when on nil-state', function()
assert_nil(peppy:getCurrentStateName())
end)
test('it should return the top-of-the-stack statename otherwise', function()
peppy:pushState('Iddle')
assert_equal(peppy:getCurrentStateName(), 'Iddle')
peppy:pushState('Attacking')
assert_equal(peppy:getCurrentStateName(), 'Attacking')
end)
end)
end) -- context 'An Instance'
context('A mixin on a stateful object', function()
-- pending
end)
context('A State', function()
-- pending
end)
end)