Skip to content

Commit

Permalink
First multi-state test working
Browse files Browse the repository at this point in the history
  • Loading branch information
chendo committed Nov 24, 2011
1 parent 6f27197 commit 1b68e08
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2 do
guard 'rspec', :version => 2, :cli => "--color" do
watch(%r{^spec/.+_spec\.rb})
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/.+\.rb}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
end
40 changes: 40 additions & 0 deletions lib/cora.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,44 @@ def plugins
@plugins ||= []
end

def process(text)
log "Processing '#{text}'"
plugins.each do |plugin|
log "Processing plugin #{plugin}"
plugin.default_listeners.each do |regex, entry|

if text =~ regex
log "Matches #{regex}"

if entry[:within_state]
log "Applicable states: #{entry[:within_state].join(', ')}"
log "Current state: #{plugin.current_state}"

if entry[:within_state].include?(plugin.current_state)
log "Matches, executing block"
plugin.instance_exec(&entry[:block])

log "Bailing from process loop"
return
end
end
end

end
end

log "No matches for '#{text}'"
no_matches
end

def respond(text)
end

def no_matches

end

def log(text)
$stderr.puts(text) if defined?(LOG)
end
end
19 changes: 15 additions & 4 deletions lib/cora/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
class Cora::Plugin

attr_accessor :manager
attr_reader :current_state

class << self

def listen_for(regex, &block)
default_listeners[regex] = block
def listen_for(regex, options = {}, &block)
default_listeners[regex] = {
block: block,
within_state: ([options[:within_state]].flatten)
}
end

def default_listeners
Expand All @@ -18,10 +24,15 @@ def default_listeners

def say(text)
log "Say: #{text}"
manager.respond(text)
end

def set_state(state)
@current_state = state
end

def ask(question)
log "Ask: #{question}"
def log(*args)
manager.log(*args)
end

end
32 changes: 28 additions & 4 deletions spec/cora_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,40 @@ class TestPlugin < Cora::Plugin
say "test!"
end

listen_for /foo/ do
say "foo"
set_state :waiting_for_bar
end

listen_for /bar/, within_state: :waiting_for_bar do
say "bar get"
end

end

let(:plugin) do
TestPlugin.new.tap { |plugin| plugin.manager = subject }
end

before do
subject.plugins << TestPlugin
subject.plugins << plugin
end

it "responds to a simple test hook" do
subject.process("this is a test")
context "single state" do
it "responds to a simple test hook" do

subject.should_receive(:respond).with("test!")
subject.should_receive(:respond).with("test!")
subject.process("this is a test")

end
end

context "multiple state" do
it "doesn't respond to listeners that don't have the required state" do
subject.should_receive(:no_matches)

subject.process("bar")
end
end

end
Expand Down

0 comments on commit 1b68e08

Please sign in to comment.