Skip to content
Browse files

Added Choice Question

  • Loading branch information...
1 parent 8a0024e commit b7d20884134ab19ab12907561f7f2490d7fc819b @burtlo committed
Showing with 180 additions and 1 deletion.
  1. +38 −0 lib/choice.rb
  2. +2 −1 lib/quiz.rb
  3. +140 −0 spec/choice_spec.rb
View
38 lib/choice.rb
@@ -0,0 +1,38 @@
+# encoding: UTF-8
+
+class Robut::Plugin::Quiz::Choice
+ include Robut::Plugin::Quiz::Question
+
+ def handle_response(sender_nick,response)
+
+ if a_valid_choice response
+ store_response sender_nick, response
+ true
+ else
+ false
+ end
+
+ end
+
+ def a_valid_choice(response)
+ @parameters.find {|choice| choice == response }
+ end
+
+ def store_response(sender_nick,response)
+ captured_results[sender_nick] = response
+ end
+
+ def results
+
+ response = @parameters.map do |choice|
+ found = captured_results.values.find_all {|result| result == choice }
+
+ "#{found.length} '#{choice}'" unless found.empty?
+
+ end.compact.join(", ")
+
+ response.empty? ? "No votes have been made" : response
+
+ end
+
+end
View
3 lib/quiz.rb
@@ -148,4 +148,5 @@ def process_response_for_active_question(sender_nick, response)
require_relative 'question'
require_relative 'polar'
-require_relative 'range'
+require_relative 'range'
+require_relative 'choice'
View
140 spec/choice_spec.rb
@@ -0,0 +1,140 @@
+require_relative 'spec_helper'
+
+describe Robut::Plugin::Quiz::Choice do
+
+ subject do
+ Robut::Plugin::Quiz::Choice.new 'person',"'Should I continue the presentation?' 'yes', 'no', 'meh'"
+ end
+
+ let(:time) { Time.now }
+
+ describe "#handle_response" do
+
+ context "when the response is a 'yes'" do
+
+ it "should store the yes response for the user" do
+
+ subject.should_receive(:store_response).with('person','yes')
+ subject.handle_response('person','yes')
+
+ end
+
+ end
+
+ context "when the response is a 'no'" do
+
+ it "should store the no response for the user" do
+
+ subject.should_receive(:store_response).with('person','no')
+ subject.handle_response('person','no')
+
+ end
+ end
+
+ context "when the response is a 'meh'" do
+
+ it "should store the meh response for the user" do
+
+ subject.should_receive(:store_response).with('person','meh')
+ subject.handle_response('person','meh')
+
+ end
+ end
+
+ context "when the response is not valid" do
+
+ it "should not store the response for the user" do
+
+ subject.handle_response('person','no way').should be_false
+
+ end
+
+ end
+
+ end
+
+
+ describe "#declare_results" do
+
+ context "when there have been no votes" do
+
+ it "should reply that there are no votes" do
+
+ subject.results.should eq "No votes have been made"
+
+ end
+
+ end
+
+ context "when there has been a 'yes' vote" do
+
+ it "should reply that there is 1 'yes' vote" do
+
+ subject.handle_response('person','yes')
+ subject.results.should eq "1 'yes'"
+
+ end
+
+ end
+
+ context "when there has been a 'no' vote" do
+
+ it "should reply that there is 1 'no' vote" do
+
+ subject.handle_response('person','no')
+ subject.results.should eq "1 'no'"
+
+ end
+
+ end
+
+ context "when there has been a 'meh' vote" do
+
+ it "should reply that there is 1 'no' vote" do
+
+ subject.handle_response('person','meh')
+ subject.results.should eq "1 'meh'"
+
+ end
+
+ end
+
+ context "when there have been several different votes" do
+
+ it "should reply that there is 1 'no' vote" do
+
+ subject.handle_response('person_a','yes')
+ subject.handle_response('person_b','no')
+ subject.handle_response('person_c','meh')
+ subject.results.should eq "1 'yes', 1 'no', 1 'meh'"
+
+ end
+
+ end
+
+ context "when there has been multiple votes by the same user" do
+
+ it "should count as one vote" do
+
+ subject.handle_response('person','no')
+ subject.handle_response('person','no')
+ subject.handle_response('person','no')
+ subject.results.should eq "1 'no'"
+
+ end
+
+ it "should only count the last vote" do
+
+ subject.handle_response('person','yes')
+ subject.handle_response('person','yes')
+ subject.handle_response('person','no')
+ subject.results.should eq "1 'no'"
+
+ end
+
+ end
+
+ end
+
+
+end

0 comments on commit b7d2088

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