Skip to content

Commit

Permalink
Merge pull request #1037 from hawkthorne/fsm_mixin
Browse files Browse the repository at this point in the history
FSM mixin
  • Loading branch information
kyleconroy committed Mar 20, 2013
2 parents 6e7ff62 + 23d1652 commit c6c6f7a
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 478 deletions.
69 changes: 44 additions & 25 deletions src/datastructures/lsm/spec/fsm_spec.lua → spec/fsm_spec.lua
Original file line number Diff line number Diff line change
@@ -1,83 +1,80 @@
require("busted")

local machine = require("statemachine")

local stoplight = {
{ name = 'warn', from = 'green', to = 'yellow' },
{ name = 'panic', from = 'yellow', to = 'red' },
{ name = 'calm', from = 'red', to = 'yellow' },
{ name = 'clear', from = 'yellow', to = 'green' }
}
local machine = require 'hawk/statemachine'
local middle = require 'hawk/middleclass'

describe("Lua state machine framework", function()
describe("A stop light", function()
local fsm
local stoplight = {
{ name = 'warn', from = 'green', to = 'yellow' },
{ name = 'panic', from = 'yellow', to = 'red' },
{ name = 'calm', from = 'red', to = 'yellow' },
{ name = 'clear', from = 'yellow', to = 'green' }
}

before_each(function()
fsm = machine.create({ initial = 'green', events = stoplight })
end)

it("should start as green", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.are.equal(fsm.current, 'green')
assert.is_true(fsm:is('green'))
end)

it("should not let you get to the wrong state", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.is_false(fsm:panic())
assert.is_false(fsm:calm())
assert.is_false(fsm:clear())
end)

it("should let you go to yellow", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.is_true(fsm:warn())
assert.are.equal(fsm.current, 'yellow')
assert.is_true(fsm:is('yellow'))
end)

it("should tell you what it can do", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.is_true(fsm:can('warn'))
assert.is_false(fsm:can('panic'))
assert.is_false(fsm:can('calm'))
assert.is_false(fsm:can('clear'))
end)

it("should tell you what it can't do", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.is_false(fsm:cannot('warn'))
assert.is_true(fsm:cannot('panic'))
assert.is_true(fsm:cannot('calm'))
assert.is_true(fsm:cannot('clear'))
end)

it("should support checking states", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
assert.is_true(fsm:is('green'))
assert.is_false(fsm:is('red'))
assert.is_false(fsm:is('yellow'))
end)

it("should cancel the warn event", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
it("should cancel the warn event on leave", function()
fsm.onleavegreen = function(self, name, from, to)
return false
end

local result = fsm:warn()

assert.is_false(result)
assert.are.equals(fsm.current, 'green')
assert.is_true(fsm:is('green'))
end)

it("should cancel the warn event", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
it("should cancel the warn event on before", function()
fsm.onbeforewarn = function(self, name, from, to)
return false
end

local result = fsm:warn()

assert.is_false(result)
assert.are.equals(fsm.current, 'green')
assert.is_true(fsm:is('green'))
end)

it("should accept other arguments", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
fsm.onstatechange = function(self, name, from, to, foo)
self.foo = foo
end
Expand All @@ -88,7 +85,6 @@ describe("Lua state machine framework", function()
end)

it("should fire the onstatechange handler", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
fsm.onstatechange = function(self, name, from, to)
self.name = name
self.from = from
Expand All @@ -103,8 +99,31 @@ describe("Lua state machine framework", function()
end)


it("should support mixins", function()
local Stoplight = middle.class('Stoplight')

Stoplight:include(machine.mixin({ initial = 'green', events = stoplight }))

function Stoplight:onwarn(name, from, to)
self.name = name
self.from = from
self.to = to
end

local light = Stoplight()
local light2 = Stoplight()

light:warn()

assert.is_true(light:is('yellow'))
assert.are.equals(light.name, 'warn')
assert.are.equals(light.from, 'green')
assert.are.equals(light.to, 'yellow')

assert.is_true(light2:is('green'))
end)

it("should fire the onwarn handler", function()
local fsm = machine.create({ initial = 'green', events = stoplight })
fsm.onwarn = function(self, name, from, to)
self.name = name
self.from = from
Expand Down
2 changes: 1 addition & 1 deletion src/app.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
local app = require 'hawk/application'
return app.new('config.json')
return app('config.json')
2 changes: 1 addition & 1 deletion src/controls.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local store = require 'hawk/store'

local db = store.load('controls-1')
local db = store('controls-1')
local controls = {}

local buttonmap = db:get('buttonmap', {
Expand Down
19 changes: 0 additions & 19 deletions src/datastructures/lsm/LICENSE

This file was deleted.

Loading

0 comments on commit c6c6f7a

Please sign in to comment.