From: John Dong Date: Tue, 14 Aug 2007 23:49:20 +0000 (+0000) Subject: * Entire election aborts with InvalidVoteError when an invalid vote object is passed in. X-Git-Url: https://projects.mako.cc/source/rubyvote/commitdiff_plain/d28df2a4166aa83fd9a0eb1f5a0599b8fc1c9798?ds=inline * Entire election aborts with InvalidVoteError when an invalid vote object is passed in. * Add testcase for checking InvalidVoteError is properly raised git-svn-id: svn://rubyforge.org/var/svn/rubyvote/trunk@31 1440c7f4-e209-0410-9a04-881b5eb134a8 --- diff --git a/lib/rubyvote/election.rb b/lib/rubyvote/election.rb index ec30ab9..b9bb557 100644 --- a/lib/rubyvote/election.rb +++ b/lib/rubyvote/election.rb @@ -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 diff --git a/test/election_test.rb b/test/election_test.rb index 5b5664e..7675e58 100644 --- a/test/election_test.rb +++ b/test/election_test.rb @@ -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("")}