Fix problem with a [nil] result being taken as having a winner; add testcases
[rubyvote] / test / range_test.rb
1 #!/usr/bin/ruby -Ilib
2
3 require 'test/unit'
4 require 'rubyvote/election'
5 require 'rubyvote/range'
6
7 class TestRangeVote < Test::Unit::TestCase
8
9   def test_range_empty
10     vote_array = []
11     assert_nil RangeVote.new(vote_array).result.winners[0]
12     assert_equal(false, RangeVote.new(vote_array).result.winner?)
13   end
14   
15   def test_range
16     vote_array = []
17     42.times {vote_array << {'A' => 10, 'B' => 5, 'C' => 2, 'D' => 1}}
18     26.times {vote_array << {'A' => 1, 'B' => 10, 'C' => 5, 'D' => 2}}
19     15.times {vote_array << {'A' => 1, 'B' => 2, 'C' => 10, 'D' => 5}}
20     17.times {vote_array << {'A' => 1, 'B' => 2, 'C' => 5, 'D' => 10}}
21
22     assert_equal('B', RangeVote.new(vote_array).result.winners[0] )
23   end
24
25   def test_tie
26     vote_array = []
27     10.times {vote_array << {'A' => 5, 'B' => 2}}
28     10.times {vote_array << {'A' => 2, 'B' => 5}}
29
30     assert_equal(['A','B'], RangeVote.new(vote_array).result.winners )
31   end
32
33   def test_no_win
34     vote_array = []
35
36     assert_equal(nil, RangeVote.new(vote_array).result.winners[0] )
37   end
38 end

Benjamin Mako Hill || Want to submit a patch?