Skip to content

Commit

Permalink
More tests, removed necessary arguments from many of the BabyBot pre …
Browse files Browse the repository at this point in the history
…and post classes
  • Loading branch information
jamiltron committed May 2, 2012
1 parent fe284d8 commit 753b246
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ rdoc
spec/reports
test/tmp
test/version_tmp
tmp
tmp
6 changes: 2 additions & 4 deletions example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ def pre_ready(event=nil)
event.to_s
end

def post_ready(event=nil)
puts "Leading ready, again event is whatever was supplied before pre_ready."
puts "event: #{event}"
event
def post_ready
puts "Leaving ready, notice no parameter is supplied."
end

def pre_run(event=nil)
Expand Down
25 changes: 22 additions & 3 deletions lib/baby_bots/baby_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class NoSuchStateException < Exception
class NoSuchTransitionException < Exception
end

# # Error to handle incorrect arity on the post_state methods.
# class PostMethodArgumentError < ArgumentError
# end

# A tiny finite-state automata class.
class BabyBot
attr_accessor :curr, :states, :start
Expand Down Expand Up @@ -89,7 +93,7 @@ def process(event=nil)

# check if we need to preprocess the event
if respond_to?("pre_#{@curr.state}")
cooked_event = send("pre_#{@curr.state}", event)
cooked_event = my_send("pre_#{@curr.state}", event)
end

# calculate the next state
Expand All @@ -115,9 +119,9 @@ def process(event=nil)
# check if we need to postprocess the event, this will act
# as the "return" from any state transition (even self-looping transitions)
if respond_to?("post_#{@curr.state}")
ret_val = send("post_#{@curr.state}", event)
ret_val = my_send("post_#{@curr.state}", event)
elsif respond_to?("post_cooked_#{curr.state}")
ret_val = send("post_#{@curr.state}", cooked_event)
ret_val = my_send("post_#{@curr.state}", cooked_event)
end

# actually transition, and make sure such a transition exists
Expand All @@ -134,6 +138,21 @@ def process(event=nil)
def restart
@curr = @start
end

private

# A wrapper around send, using the arity restrictions assumed by BabyBots.
def my_send(method_name, event=nil)
m_arity = method(method_name).arity
if m_arity == 0
ret_val = send(method_name)
elsif m_arity == 1 or m_arity == -1
ret_val = send(method_name, event)
else
raise ArgumentError
end
return ret_val
end
end

end
2 changes: 1 addition & 1 deletion lib/baby_bots/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module BabyBots
# Current development version
VERSION = "0.0.4"
VERSION = "0.0.5"
end
26 changes: 26 additions & 0 deletions spec/baby_bot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def post_run(event=nil)
end
end

class BB2 < BB
def post_loading
true
end
end

class BB3 < BB
def post_loading(event, stuff, mo_stuff)
true
end
end


describe BabyBots::BabyBot do
it "should be able to be initiated with no additional initialization" do
Expand Down Expand Up @@ -124,4 +136,18 @@ def post_run(event=nil)
ret1.should == ret2
ret2.should == true
end

it "should run the example, and not have issue with a zero-arity method" do
test = BB2.new
test.process(1)
test.process(1)
test.state.should == :run
end

it "should run the example, and raise an ArgumentError on arity error" do
test = BB3.new
lambda{ test.process(1)}.should raise_error ArgumentError
end


end

0 comments on commit 753b246

Please sign in to comment.