Permalink
Browse files

First multi-state test working

  • Loading branch information...
chendo committed Nov 24, 2011
1 parent 6f27197 commit 1b68e08e726056f5bcdd1f41c72e283ef132f442
Showing with 85 additions and 10 deletions.
  1. +2 −2 Guardfile
  2. +40 −0 lib/cora.rb
  3. +15 −4 lib/cora/plugin.rb
  4. +28 −4 spec/cora_spec.rb
View
@@ -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
View
@@ -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
View
@@ -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
@@ -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
View
@@ -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

0 comments on commit 1b68e08

Please sign in to comment.