Skip to content
Browse files

Adding pick plugin

  • Loading branch information...
1 parent 8013d0e commit d90827c1b10a9a5e4e44419dcd480a9c03fba52f @bvandenbos bvandenbos committed Jan 10, 2012
Showing with 62 additions and 0 deletions.
  1. +1 −0 Gemfile
  2. +26 −0 lib/robut/plugin/pick.rb
  3. +35 −0 test/unit/plugin/pick_test.rb
View
1 Gemfile
@@ -8,6 +8,7 @@ group :test do
gem 'simplecov'
gem 'webmock'
gem 'time-warp'
+ gem 'mocha'
end
group :plugin do
View
26 lib/robut/plugin/pick.rb
@@ -0,0 +1,26 @@
+require 'calc'
+
+# Let fate decide!
+class Robut::Plugin::Pick
+ include Robut::Plugin
+
+ # Returns a description of how to use this plugin
+ def usage
+ "#{at_nick} pick <a>, <b>, <c>, ... - randomly selects one of the options"
+ end
+
+ # Perform the calculation specified in +message+, and send the
+ # result back.
+ def handle(time, sender_nick, message)
+ if sent_to_me?(message) && words(message).first == 'pick'
+ options = words(message, 'pick').join(' ').split(',').map { |s| s.strip }
+ rsp = options[random(options.length)]
+ reply("And the winner is... #{rsp}") if rsp
+ end
+ end
+
+ def random(c)
+ rand(c)
+ end
+
+end
View
35 test/unit/plugin/pick_test.rb
@@ -0,0 +1,35 @@
+require 'test_helper'
+require 'robut/plugin/pick'
+require 'mocha'
+
+class Robut::Plugin::PickTest < Test::Unit::TestCase
+
+ def setup
+ @connection = Robut::ConnectionMock.new
+ @presence = Robut::PresenceMock.new(@connection)
+ @plugin = Robut::Plugin::Pick.new(@presence)
+ end
+
+ def test_replies_with_correct_response
+ @plugin.stubs(:random).returns(0)
+ @plugin.handle(Time.now, "@john", "@robut pick a, b, c")
+ assert_equal ["And the winner is... a"], @plugin.reply_to.replies
+ end
+
+ def test_does_nothing_when_no_options_are_given
+ @plugin.handle(Time.now, "@john", "@robut pick")
+ assert @plugin.reply_to.replies.empty?
+ end
+
+ def test_replies_only_option_if_given_one_options
+ @plugin.handle(Time.now, "@john", "@robut pick a")
+ assert_equal ["And the winner is... a"], @plugin.reply_to.replies
+ end
+
+ def test_handles_spaces
+ @plugin.stubs(:random).returns(1)
+ @plugin.handle(Time.now, "@john", "@robut pick this is the first option, this is the second option, this is the third option")
+ assert_equal ["And the winner is... this is the second option"], @plugin.reply_to.replies
+ end
+
+end

0 comments on commit d90827c

Please sign in to comment.
Something went wrong with that request. Please try again.