* Entire election aborts with InvalidVoteError when an invalid vote object is passed in.
authorJohn Dong <jdong@mit.edu>
Tue, 14 Aug 2007 23:49:20 +0000 (23:49 +0000)
committerJohn Dong <jdong@mit.edu>
Tue, 14 Aug 2007 23:49:20 +0000 (23:49 +0000)
 * Add testcase for checking InvalidVoteError is properly raised

git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@31 1440c7f4-e209-0410-9a04-881b5eb134a8

lib/rubyvote/election.rb
test/election_test.rb

index ec30ab9..b9bb557 100644 (file)
@@ -42,7 +42,11 @@ class ElectionVote
     if votes
       if votes.instance_of?( Array )
         votes.each do |vote|
-          self.tally_vote(vote) if self.verify_vote(vote)
+          if self.verify_vote(vote)
+            self.tally_vote(vote)
+          else
+            raise InvalidVoteError.new ("Invalid vote object", vote)
+          end
         end
       else
         raise ElectionError, "Votes must be in the form of an array.", caller
@@ -163,3 +167,10 @@ end
 class ElectionError < ArgumentError
 end
 
+class InvalidVoteError < ElectionError
+  attr_accessor :voteobj
+  def initialize(msg=nil, voteobj=nil)
+    super(msg)
+    @voteobj=voteobj
+  end
+end
index 5b5664e..7675e58 100644 (file)
@@ -16,7 +16,11 @@ class TestElectionVote < Test::Unit::TestCase
     assert_equal( 1, PluralityVote.new(vote_array).result.winners[0] )
   end
 
-
+  def test_invalid_voteobj
+    vote_array = [1,2,nil,1]
+    assert_raise(InvalidVoteError) { PluralityVote.new(vote_array).result.winners[0] }
+  end
+  
   def test_approval
     vote_array = Array.new
     10.times {vote_array << "AB".split("")}

Benjamin Mako Hill || Want to submit a patch?