]> projects.mako.cc - rubyvote/commitdiff
* 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 ec30ab9c357f5cd35f28e2b45123de76c1eebf74..b9bb5574c7ea03b4d9d5d6e1b544fdca640e3ee3 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 5b5664e7eaabf55c9dc9e0ed63da5c7e211be7aa..7675e58a01c8eb1fe4a254d57f6204062dc27619 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?