From 7723c043af3d63f3288048d47f0c31f8449c9582 Mon Sep 17 00:00:00 2001 From: Hundter Biede <6586509+hbiede@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:39:15 -0600 Subject: [PATCH] Fix test coverage --- gen_tokens.rb | 2 + pdfs/template/logo.png | Bin 7065 -> 0 bytes tests/vote_parser_test.rb | 664 +++++++++++++++++++------------------- vote_parser.rb | 14 +- 4 files changed, 346 insertions(+), 334 deletions(-) delete mode 100644 pdfs/template/logo.png diff --git a/gen_tokens.rb b/gen_tokens.rb index 908e211..efa1693 100644 --- a/gen_tokens.rb +++ b/gen_tokens.rb @@ -10,6 +10,7 @@ token_char_count = 7 # Whether or not to generate PDFs generate_pdfs = true +# :nocov: OptionParser.new do |opt| opt.on( '-cCOUNT', @@ -19,6 +20,7 @@ ) { |o| token_char_count = o } opt.on('-n', '--no-pdfs', 'Disable PDF generation') { generate_pdfs = false } end.parse! +# :nocov: # Regex to match the following alphabet: ^[a-km-zA-HJ-NPRT-Z2-46-9]{7,7}$ # noinspection SpellCheckingInspection diff --git a/pdfs/template/logo.png b/pdfs/template/logo.png deleted file mode 100644 index 17a82073152dc3a960f7a2866a7886f8dc353213..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7065 zcmb_>XD}Sl*SCbI*$9FldR?)IZiTQ~h)#4i>M9X!^|B&1TC^w$t7Mhvors=9Z)>d* zU7~lE)q}_X{q(*c-e;Z<&z*DUH|Lz+Id|sFow;-FiPqCm2T(Fo5)lyrG@h#%5E0#S z`p+gOzOiV_C9T~62~0^_iHN8=f$E>log1CQ?zw?B5m6u?5m8tK5z+aLDQumH$VZfj zX!8{j5%@C^5u-jjHn0 zsf2<1hLFrI7R3#A6wznYj`V!2k%Ci|CE;u zuA!|;wnEMV5aB!emkJ;vVp7piQ8MzM-pQhX&OA?!4EaOHejh?x(@c@nNc_+iW%X!C zi#DNlY4AMr>?+6M0z2Y2@TEghe$Un8dC$^s@1pOaXVBW)*O z`&_+@S>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2gpo^*3xElVc^^spdjSs9%^H!|15dOqXt>qP>ZV^p*=ywPPy`k(Q{jg;Gr z&+n|g($)KkORYX)&@+vcQl!kgf7dMgUATNOfSyOeS8_%L`UuWJw$mK%7L0JoS5vwE z>r|z|txhZXvEIRe`=eaAA8))es4gudm}*&0k2BXj3<|kLhAY;nccIx*xtAXiF1whT zb=#m-6M|HbYb|>kl|s6L#_wlT5VVbkgEF&@U?=wB7&5Ae!9fO z(3m*D{$iG>n2k*AIgdW;xJ!XS^G2Py1kUv@VmxWSH$z`F+MQHv!6ibiLZ zGOmp8vnY!*K)+?1@}+oImEY#FJYDn)a!sGu_l9vg;2+N#vy)k#9+0D>?7qkiG { "George Washington" => 1 } }, votes_count) + assert_equal({ 0 => { 'George Washington' => 1 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 0) - assert_equal({ 0 => { "George Washington" => 2 } }, votes_count) + assert_equal({ 0 => { 'George Washington' => 2 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 0) - assert_equal({ 0 => { "George Washington" => 3 } }, votes_count) + assert_equal({ 0 => { 'George Washington' => 3 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 1) - assert_equal({ 0 => { "George Washington" => 3 }, 1 => { "John Adams" => 1 } }, votes_count) + assert_equal({ 0 => { 'George Washington' => 3 }, 1 => { 'John Adams' => 1 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 2) - assert_equal({ 0 => { "George Washington" => 3 }, 1 => { "John Adams" => 1 }, 2 => { "Thomas Jefferson" => 1 } }, votes_count) + assert_equal({ 0 => { 'George Washington' => 3 }, 1 => { 'John Adams' => 1 }, 2 => { 'Thomas Jefferson' => 1 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 3) assert_equal({ - 0 => { "George Washington" => 3 }, - 1 => { "John Adams" => 1 }, - 2 => { "Thomas Jefferson" => 1 }, - 3 => { "Alexander Hamilton" => 1 } + 0 => { 'George Washington' => 3 }, + 1 => { 'John Adams' => 1 }, + 2 => { 'Thomas Jefferson' => 1 }, + 3 => { 'Alexander Hamilton' => 1 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 0) assert_equal({ - 0 => { "George Washington" => 4 }, - 1 => { "John Adams" => 1 }, - 2 => { "Thomas Jefferson" => 1 }, - 3 => { "Alexander Hamilton" => 1 } + 0 => { 'George Washington' => 4 }, + 1 => { 'John Adams' => 1 }, + 2 => { 'Thomas Jefferson' => 1 }, + 3 => { 'Alexander Hamilton' => 1 } }, votes_count) VoteParser.parse_single_vote(votes_count, vote, 2) assert_equal({ - 0 => { "George Washington" => 4 }, - 1 => { "John Adams" => 1 }, - 2 => { "Thomas Jefferson" => 2 }, - 3 => { "Alexander Hamilton" => 1 } + 0 => { 'George Washington' => 4 }, + 1 => { 'John Adams' => 1 }, + 2 => { 'Thomas Jefferson' => 2 }, + 3 => { 'Alexander Hamilton' => 1 } }, votes_count) end def test_validate_vote vote_count = {} used_tokens = {} - vote = ["George Washington", "John Adams", "Thomas Jefferson", "Alexander Hamilton"] - alt_vote = ["George Washington", "Aaron Burr", "Thomas Jefferson", "Someone Else"] - abstain_vote = ["George Washington", "", "", ""] - short_abstain_cote = ["George Washington"] - token_mapping = { "abc" => "A", "bcd" => "B", "cde" => "C", "def" => "D", "efg" => "E", "fgh" => "F" } - - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["abc"].concat(vote), token_mapping)) - assert_equal({ "abc" => true }, used_tokens) + vote = ['George Washington', 'John Adams', 'Thomas Jefferson', 'Alexander Hamilton'] + alt_vote = ['George Washington', 'Aaron Burr', 'Thomas Jefferson', 'Someone Else'] + abstain_vote = ['George Washington', '', '', ''] + short_abstain_cote = ['George Washington'] + token_mapping = { 'abc' => 'A', 'bcd' => 'B', 'cde' => 'C', 'def' => 'D', 'efg' => 'E', 'fgh' => 'F' } + + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['abc'].concat(vote), token_mapping)) + assert_equal({ 'abc' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 1 }, - 2 => { "John Adams" => 1 }, - 3 => { "Thomas Jefferson" => 1 }, - 4 => { "Alexander Hamilton" => 1 } + 1 => { 'George Washington' => 1 }, + 2 => { 'John Adams' => 1 }, + 3 => { 'Thomas Jefferson' => 1 }, + 4 => { 'Alexander Hamilton' => 1 } }, vote_count) - assert_equal("abc (A) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ["abc"].concat(vote), token_mapping)) - assert_equal({ "abc" => true }, used_tokens) + assert_equal("abc (A) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ['abc'].concat(vote), token_mapping)) + assert_equal({ 'abc' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 1 }, - 2 => { "John Adams" => 1 }, - 3 => { "Thomas Jefferson" => 1 }, - 4 => { "Alexander Hamilton" => 1 } + 1 => { 'George Washington' => 1 }, + 2 => { 'John Adams' => 1 }, + 3 => { 'Thomas Jefferson' => 1 }, + 4 => { 'Alexander Hamilton' => 1 } }, vote_count) - assert_equal("abc (A) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ["abc"].concat(vote), token_mapping)) - assert_equal({ "abc" => true }, used_tokens) + assert_equal("abc (A) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ['abc'].concat(vote), token_mapping)) + assert_equal({ 'abc' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 1 }, - 2 => { "John Adams" => 1 }, - 3 => { "Thomas Jefferson" => 1 }, - 4 => { "Alexander Hamilton" => 1 } + 1 => { 'George Washington' => 1 }, + 2 => { 'John Adams' => 1 }, + 3 => { 'Thomas Jefferson' => 1 }, + 4 => { 'Alexander Hamilton' => 1 } }, vote_count) - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["bcd"].concat(vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true }, used_tokens) + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['bcd'].concat(vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 2 }, - 2 => { "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 2 }, - 4 => { "Alexander Hamilton" => 2 } + 1 => { 'George Washington' => 2 }, + 2 => { 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 2 }, + 4 => { 'Alexander Hamilton' => 2 } }, vote_count) - assert_equal("bcd (B) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ["bcd"].concat(vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true }, used_tokens) + assert_equal("bcd (B) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ['bcd'].concat(vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 2 }, - 2 => { "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 2 }, - 4 => { "Alexander Hamilton" => 2 } + 1 => { 'George Washington' => 2 }, + 2 => { 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 2 }, + 4 => { 'Alexander Hamilton' => 2 } }, vote_count) - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["cde"].concat(alt_vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true }, used_tokens) + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['cde'].concat(alt_vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 3 }, - 2 => { "Aaron Burr" => 1, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 3 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 1 } + 1 => { 'George Washington' => 3 }, + 2 => { 'Aaron Burr' => 1, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 3 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 1 } }, vote_count) - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["def"].concat(alt_vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true, "def" => true }, used_tokens) + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['def'].concat(alt_vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true, 'def' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 4 }, - 2 => { "Aaron Burr" => 2, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 4 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 2 } + 1 => { 'George Washington' => 4 }, + 2 => { 'Aaron Burr' => 2, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 4 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 2 } }, vote_count) - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["efg"].concat(abstain_vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true, "def" => true, "efg" => true }, used_tokens) + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['efg'].concat(abstain_vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true, 'def' => true, 'efg' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 5 }, - 2 => { "Aaron Burr" => 2, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 4 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 2 } + 1 => { 'George Washington' => 5 }, + 2 => { 'Aaron Burr' => 2, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 4 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 2 } }, vote_count) - assert_equal("efg (E) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ["efg"].concat(abstain_vote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true, "def" => true, "efg" => true }, used_tokens) + assert_equal("efg (E) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ['efg'].concat(abstain_vote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true, 'def' => true, 'efg' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 5 }, - 2 => { "Aaron Burr" => 2, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 4 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 2 } + 1 => { 'George Washington' => 5 }, + 2 => { 'Aaron Burr' => 2, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 4 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 2 } }, vote_count) - assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ["fgh"].concat(short_abstain_cote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true, "def" => true, "efg" => true, "fgh" => true }, used_tokens) + assert_equal('', VoteParser.validate_vote(vote_count, used_tokens, ['fgh'].concat(short_abstain_cote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true, 'def' => true, 'efg' => true, 'fgh' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 6 }, - 2 => { "Aaron Burr" => 2, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 4 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 2 } + 1 => { 'George Washington' => 6 }, + 2 => { 'Aaron Burr' => 2, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 4 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 2 } }, vote_count) - assert_equal("fgh (F) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ["fgh"].concat(short_abstain_cote), token_mapping)) - assert_equal({ "abc" => true, "bcd" => true, "cde" => true, "def" => true, "efg" => true, "fgh" => true }, used_tokens) + assert_equal("fgh (F) voted multiple times. Using latest.\n", VoteParser.validate_vote(vote_count, used_tokens, ['fgh'].concat(short_abstain_cote), token_mapping)) + assert_equal({ 'abc' => true, 'bcd' => true, 'cde' => true, 'def' => true, 'efg' => true, 'fgh' => true }, used_tokens) assert_equal({ - 1 => { "George Washington" => 6 }, - 2 => { "Aaron Burr" => 2, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 4 }, - 4 => { "Alexander Hamilton" => 2, "Someone Else" => 2 } + 1 => { 'George Washington' => 6 }, + 2 => { 'Aaron Burr' => 2, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 4 }, + 4 => { 'Alexander Hamilton' => 2, 'Someone Else' => 2 } }, vote_count) end @@ -220,93 +220,110 @@ def test_generate_vote_totals # Messages tests assert_equal('', VoteParser.generate_vote_totals({}, {}, [['abc', '']], { 'abc' => 'A' }, true)) assert_equal( - "abc (A) voted multiple times. Using latest.\n", - VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', '']], { 'abc' => 'A' }, true) + "abc (A) voted multiple times. Using latest.\n", + VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', '']], { 'abc' => 'A' }, true) ) assert_equal( - "abc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\n", - VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', ''], ['abc', '']], { 'abc' => 'A' }, true) + "abc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\n", + VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', ''], ['abc', '']], { 'abc' => 'A' }, true) ) assert_equal( - "xyz is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\n", - VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', ''], ['abc', ''], ['xyz', '']], { 'abc' => 'A' }, true)) + "xyz is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\n", + VoteParser.generate_vote_totals({}, { 'abc' => true }, [['abc', ''], ['abc', ''], ['xyz', '']], { 'abc' => 'A' }, true)) assert_equal( - "xyz is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\nxyz2 is an invalid token. Vote not counted.\n", - VoteParser.generate_vote_totals( - {}, - { 'abc' => true }, - [['xyz2', ''], ['abc', ''], ['abc', ''], ['xyz', '']], - { 'abc' => 'A' }, - true - ) + "xyz is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\nabc (A) voted multiple times. Using latest.\nxyz2 is an invalid token. Vote not counted.\n", + VoteParser.generate_vote_totals( + {}, + { 'abc' => true }, + [['xyz2', ''], ['abc', ''], ['abc', ''], ['xyz', '']], + { 'abc' => 'A' }, + true + ) ) - #Vote Counts + # Vote Counts vote_counts = {} used_tokens = {} VoteParser.generate_vote_totals( - vote_counts, - used_tokens, - [%w[xyz2 AVote1 BVote1], %w[abc AVote2 BVote2], %w[abc AVote3 BVote3], %w[xyz AVote4 BVote4]], - { 'abc' => 'A' }, - true + vote_counts, + used_tokens, + [%w[xyz2 AVote1 BVote1], %w[abc AVote2 BVote2], %w[abc AVote3 BVote3], %w[xyz AVote4 BVote4]], + { 'abc' => 'A' }, + true ) - assert_equal({ 1 => { "AVote3" => 1 }, 2 => { "BVote3" => 1 } }, vote_counts) - assert_equal({ "abc" => true }, used_tokens) + assert_equal({ 1 => { 'AVote3' => 1 }, 2 => { 'BVote3' => 1 } }, vote_counts) + assert_equal({ 'abc' => true }, used_tokens) vote_counts = {} used_tokens = {} VoteParser.generate_vote_totals( - vote_counts, - used_tokens, - [%w[xyz2 AVote1 BVote1], %w[abc AVote2 BVote2], %w[abc AVote3 BVote3], %w[xyz AVote4 BVote4]], - { 'abc' => 'A', 'xyz' => 'X', 'xyz2' => 'X2' }, - true + vote_counts, + used_tokens, + [%w[xyz2 AVote1 BVote1], %w[abc AVote2 BVote2], %w[abc AVote3 BVote3], %w[xyz AVote4 BVote4]], + { 'abc' => 'A', 'xyz' => 'X', 'xyz2' => 'X2' }, + true ) assert_equal( - { - 1 => { 'AVote1' => 1, "AVote3" => 1, 'AVote4' => 1 }, - 2 => { 'BVote1' => 1, "BVote3" => 1, 'BVote4' => 1 } - }, - vote_counts) - assert_equal({ "abc" => true, 'xyz' => true, 'xyz2' => true }, used_tokens) + { + 1 => { 'AVote1' => 1, 'AVote3' => 1, 'AVote4' => 1 }, + 2 => { 'BVote1' => 1, 'BVote3' => 1, 'BVote4' => 1 } + }, + vote_counts) + assert_equal({ 'abc' => true, 'xyz' => true, 'xyz2' => true }, used_tokens) vote_counts = {} used_tokens = {} warning = VoteParser.generate_vote_totals( - vote_counts, - used_tokens, - [ - %w[xyz2 AVote4 BVote4], - %w[abc AVote2 BVote2], - %w[abc AVote3 BVote3], - %w[xyz AVote4 BVote4], - %w[hi AVote4 BVote3], - %w[hi2 AVote4 BVote2], - %w[hi3 AVote4], - ['hi4', '', 'BVote2'], - %w[fake hi] - ], - { - 'abc' => 'A', - 'bcd' => 'B', - 'hi' => 'H', - 'hi2' => 'H', - 'hi3' => 'H', - 'hi4' => 'H', - 'xyz' => 'X', - 'xyz2' => 'X2' - }, - true + vote_counts, + used_tokens, + [ + %w[xyz2 AVote4 BVote4], + %w[abc AVote2 BVote2], + %w[abc AVote3 BVote3], + %w[xyz AVote4 BVote4], + %w[hi AVote4 BVote3], + %w[hi2 AVote4 BVote2], + %w[hi3 AVote4], + ['hi4', '', 'BVote2'], + %w[fake hi] + ], + { + 'abc' => 'A', + 'bcd' => 'B', + 'hi' => 'H', + 'hi2' => 'H', + 'hi3' => 'H', + 'hi4' => 'H', + 'xyz' => 'X', + 'xyz2' => 'X2' + }, + true ) assert_equal("fake is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\n", warning) assert_equal( - { - 1 => { "AVote3" => 1, 'AVote4' => 5 }, - 2 => { 'BVote2' => 2, "BVote3" => 2, 'BVote4' => 2 } - }, - vote_counts) - assert_equal({ "abc" => true, 'hi' => true, 'hi2' => true, 'hi3' => true, 'hi4' => true, 'xyz' => true, 'xyz2' => true }, used_tokens) + { + 1 => { 'AVote3' => 1, 'AVote4' => 5 }, + 2 => { 'BVote2' => 2, 'BVote3' => 2, 'BVote4' => 2 } + }, + vote_counts) + assert_equal({ 'abc' => true, 'hi' => true, 'hi2' => true, 'hi3' => true, 'hi4' => true, 'xyz' => true, 'xyz2' => true }, used_tokens) + + vote_counts = {} + used_tokens = {} + VoteParser.generate_vote_totals( + vote_counts, + used_tokens, + [%w[xyz2 AVote1 BVote1], %w[abc AVote2 BVote2], %w[abc AVote3 BVote3], %w[xyz AVote4 BVote4]], + { 'abc' => 'A', 'xyz' => 'X', 'xyz2' => 'X2' }, + false + ) + assert_equal( + { + 1 => { 'AVote1' => 1, 'AVote2' => 1, 'AVote4' => 1 }, + 2 => { 'BVote1' => 1, 'BVote2' => 1, 'BVote4' => 1 } + }, + vote_counts) + assert_equal({ 'abc' => true, 'xyz' => true, 'xyz2' => true }, used_tokens) end def test_init @@ -365,47 +382,46 @@ def test_init def test_process_votes result = VoteParser.process_votes( - [ - %w[xyz2 AVote4 BVote4], - %w[abc AVote2 BVote2], - %w[abc AVote3 BVote3], - %w[xyz AVote4 BVote4], - %w[hi AVote4 BVote3], - %w[hi2 AVote4 BVote2], - %w[hi3 AVote4], - ['hi4', '', 'BVote2'], - %w[fake hi] - ], - { - 'abc' => 'A', - 'bcd' => 'B', - 'hi' => 'H', - 'hi2' => 'H', - 'hi3' => 'H', - 'hi4' => 'H', - 'xyz' => 'X', - 'xyz2' => 'X2' - }, - true + [ + %w[xyz2 AVote4 BVote4], + %w[abc AVote2 BVote2], + %w[abc AVote3 BVote3], + %w[xyz AVote4 BVote4], + %w[hi AVote4 BVote3], + %w[hi2 AVote4 BVote2], + %w[hi3 AVote4], + ['hi4', '', 'BVote2'], + %w[fake hi] + ], + { + 'abc' => 'A', + 'bcd' => 'B', + 'hi' => 'H', + 'hi2' => 'H', + 'hi3' => 'H', + 'hi4' => 'H', + 'xyz' => 'X', + 'xyz2' => 'X2' + }, + true ) assert_equal("fake is an invalid token. Vote not counted.\nabc (A) voted multiple times. Using latest.\n", result[:Warning]) assert_equal( - { - 1 => { "AVote3" => 1, 'AVote4' => 5 }, - 2 => { 'BVote2' => 2, "BVote3" => 2, 'BVote4' => 2 } - }, - result[:VoteCounts]) + { + 1 => { 'AVote3' => 1, 'AVote4' => 5 }, + 2 => { 'BVote2' => 2, 'BVote3' => 2, 'BVote4' => 2 } + }, + result[:VoteCounts]) assert_equal(7, result[:TotalVoterCount]) end end - -#noinspection RubyResolve +# noinspection RubyResolve class TestOutputPrinter < Test::Unit::TestCase def test_write_output orig_stdout = $stdout.clone - $stdout = File.new(File::NULL,"w") - + $stdout = File.new(File::NULL, 'w') + begin OutputPrinter.write_output('REPORT', 'WARNING', nil) rescue @@ -439,9 +455,9 @@ def test_write_election_report file_name = 'test_write_election_report.txt' OutputPrinter.write_election_report( - file_name, - 'This is a fake election report that needs to be seen', - 'This is a warning that also needs to be seen' + file_name, + 'This is a fake election report that needs to be seen', + 'This is a warning that also needs to be seen' ) file = File.open(file_name) @@ -450,9 +466,9 @@ def test_write_election_report assert_true(contents.match?(/\sThis is a warning that also needs to be seen\s/m)) OutputPrinter.write_election_report( - file_name, - 'This is a second fake election report that needs to be seen', - 'This is a second warning that also needs to be seen' + file_name, + 'This is a second fake election report that needs to be seen', + 'This is a second warning that also needs to be seen' ) file = File.open(file_name) @@ -466,20 +482,20 @@ def test_write_election_report def test_ballot_entry_string assert_equal( - "\t*Tom: 100 votes (51.81%)\n", - OutputPrinter.ballot_entry_string('Tom', 100, 51.8134715) + "\t*Tom: 100 votes (51.81%)\n", + OutputPrinter.ballot_entry_string('Tom', 100, 51.8134715) ) assert_equal( - "\t*Tom: 4 votes (66.67%)\n", - OutputPrinter.ballot_entry_string('Tom', 4, 66.6666) + "\t*Tom: 4 votes (66.67%)\n", + OutputPrinter.ballot_entry_string('Tom', 4, 66.6666) ) assert_equal( - "\t Allison: 1 vote (1.00%)\n", - OutputPrinter.ballot_entry_string('Allison', 1, 1) + "\t Allison: 1 vote (1.00%)\n", + OutputPrinter.ballot_entry_string('Allison', 1, 1) ) assert_equal( - "\t*Allison: 1 vote (100.00%)\n", - OutputPrinter.ballot_entry_string('Allison', 1, 100) + "\t*Allison: 1 vote (100.00%)\n", + OutputPrinter.ballot_entry_string('Allison', 1, 100) ) end @@ -493,197 +509,197 @@ def test_abstention_count_string def test_position_report_totals assert_equal( - "\t Abstained: 1 vote\n-------------------------------------------------\n\t Total: 193 votes\n\n", - OutputPrinter.position_report_totals(193, 192) + "\t Abstained: 1 vote\n-------------------------------------------------\n\t Total: 193 votes\n\n", + OutputPrinter.position_report_totals(193, 192) ) assert_equal( - "\t Abstained: 100 votes\n-------------------------------------------------\n\t Total: 193 votes\n\n", - OutputPrinter.position_report_totals(193, 93) + "\t Abstained: 100 votes\n-------------------------------------------------\n\t Total: 193 votes\n\n", + OutputPrinter.position_report_totals(193, 93) ) assert_equal( - "\t Abstained: 1 vote\n-------------------------------------------------\n\t Total: 1 vote\n\n", - OutputPrinter.position_report_totals(1, 0) + "\t Abstained: 1 vote\n-------------------------------------------------\n\t Total: 1 vote\n\n", + OutputPrinter.position_report_totals(1, 0) ) assert_equal( - "-------------------------------------------------\n\t Total: 1 vote\n\n", - OutputPrinter.position_report_totals(1, 1) + "-------------------------------------------------\n\t Total: 1 vote\n\n", + OutputPrinter.position_report_totals(1, 1) ) assert_equal( - "-------------------------------------------------\n\t Total: 1000 votes\n\n", - OutputPrinter.position_report_totals(1000, 1000) + "-------------------------------------------------\n\t Total: 1000 votes\n\n", + OutputPrinter.position_report_totals(1000, 1000) ) end def test_position_report_indiv result = OutputPrinter.position_report_individuals( - 6, - 5, - { "AVote3" => 1, 'AVote4' => 4 } + 6, + 5, + { 'AVote3' => 1, 'AVote4' => 4 } ) expected = "\t*AVote4: 4 votes (66.67%)\n" + - "\t AVote3: 1 vote (16.67%)\n" + - "\t Abstained: 1 vote\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes\n\n" + "\t AVote3: 1 vote (16.67%)\n" + + "\t Abstained: 1 vote\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes\n\n" assert_equal(expected, result) result = OutputPrinter.position_report_individuals( - 6, - 6, - { "AVote3" => 2, 'AVote4' => 4 } + 6, + 6, + { 'AVote3' => 2, 'AVote4' => 4 } ) expected = "\t*AVote4: 4 votes (66.67%)\n" + - "\t AVote3: 2 votes (33.33%)\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes\n\n" + "\t AVote3: 2 votes (33.33%)\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes\n\n" assert_equal(expected, result) result = OutputPrinter.position_report_individuals( - 1000, - 6, - { "AVote3" => 2, 'AVote4' => 4 } + 1000, + 6, + { 'AVote3' => 2, 'AVote4' => 4 } ) expected = "\t AVote4: 4 votes (0.40%)\n" + - "\t AVote3: 2 votes (0.20%)\n" + - "\t Abstained: 994 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 1000 votes\n\n" + "\t AVote3: 2 votes (0.20%)\n" + + "\t Abstained: 994 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 1000 votes\n\n" assert_equal(expected, result) result = OutputPrinter.position_report_individuals( - 1000, - 602, - { "AVote3" => 2, 'AVote4' => 600 } + 1000, + 602, + { 'AVote3' => 2, 'AVote4' => 600 } ) expected = "\t*AVote4: 600 votes (60.00%)\n" + - "\t AVote3: 2 votes (0.20%)\n" + - "\t Abstained: 398 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 1000 votes\n\n" + "\t AVote3: 2 votes (0.20%)\n" + + "\t Abstained: 398 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 1000 votes\n\n" assert_equal(expected, result) end def test_sum_position_votes - assert_equal(602, OutputPrinter.sum_position_votes({ "AVote3" => 2, 'AVote4' => 600 })) - assert_equal(800, OutputPrinter.sum_position_votes({ "AVote3" => 200, 'AVote4' => 600 })) - assert_equal(0, OutputPrinter.sum_position_votes({ "AVote3" => 0, 'AVote4' => 0 })) + assert_equal(602, OutputPrinter.sum_position_votes({ 'AVote3' => 2, 'AVote4' => 600 })) + assert_equal(800, OutputPrinter.sum_position_votes({ 'AVote3' => 200, 'AVote4' => 600 })) + assert_equal(0, OutputPrinter.sum_position_votes({ 'AVote3' => 0, 'AVote4' => 0 })) assert_equal(0, OutputPrinter.sum_position_votes({})) assert_equal( - 1204, - OutputPrinter.sum_position_votes( - { "AVote1" => 2, 'AVote2' => 600, "AVote3" => 2, 'AVote4' => 600 } - ) + 1204, + OutputPrinter.sum_position_votes( + { 'AVote1' => 2, 'AVote2' => 600, 'AVote3' => 2, 'AVote4' => 600 } + ) ) assert_equal( - 1400, - OutputPrinter.sum_position_votes( - { - "AVote1" => 2, - 'AVote2' => 600, - "AVote3" => 2, - 'AVote4' => 600, - 'AVote5' => 100, - 'AVote6' => 46, - 'AVote7' => 50 - } - ) + 1400, + OutputPrinter.sum_position_votes( + { + 'AVote1' => 2, + 'AVote2' => 600, + 'AVote3' => 2, + 'AVote4' => 600, + 'AVote5' => 100, + 'AVote6' => 46, + 'AVote7' => 50 + } + ) ) end def test_majority_reached - assert_true(OutputPrinter.majority_reached?(100, { "AVote3" => 20, 'AVote4' => 60 })) + assert_true(OutputPrinter.majority_reached?(100, { 'AVote3' => 20, 'AVote4' => 60 })) assert_true(OutputPrinter.majority_reached?(100, { 'AVote4' => 100 })) - assert_true(OutputPrinter.majority_reached?(100, { "AVote3" => 49, 'AVote4' => 51 })) - assert_true(OutputPrinter.majority_reached?(100, { "AVote3" => 99, 'AVote4' => 1 })) - assert_true(OutputPrinter.majority_reached?(1, { "AVote3" => 0, 'AVote4' => 1 })) - assert_true(OutputPrinter.majority_reached?(3, { "AVote3" => 2, 'AVote4' => 1 })) - assert_true(OutputPrinter.majority_reached?(100, { "AVote1" => 2, "AVote2" => 2, "AVote3" => 2, 'AVote4' => 1, 'AVote5' => 1, 'AVote6' => 1, 'AVote7' => 1, 'AVote8' => 90 })) - assert_true(OutputPrinter.majority_reached?(1, { "AVote1" => 1 })) - - assert_false(OutputPrinter.majority_reached?(100, { "AVote1" => 2, "AVote2" => 2, "AVote3" => 2, 'AVote4' => 1, 'AVote5' => 1, 'AVote6' => 1, 'AVote7' => 1, 'AVote8' => 9 })) + assert_true(OutputPrinter.majority_reached?(100, { 'AVote3' => 49, 'AVote4' => 51 })) + assert_true(OutputPrinter.majority_reached?(100, { 'AVote3' => 99, 'AVote4' => 1 })) + assert_true(OutputPrinter.majority_reached?(1, { 'AVote3' => 0, 'AVote4' => 1 })) + assert_true(OutputPrinter.majority_reached?(3, { 'AVote3' => 2, 'AVote4' => 1 })) + assert_true(OutputPrinter.majority_reached?(100, { 'AVote1' => 2, 'AVote2' => 2, 'AVote3' => 2, 'AVote4' => 1, 'AVote5' => 1, 'AVote6' => 1, 'AVote7' => 1, 'AVote8' => 90 })) + assert_true(OutputPrinter.majority_reached?(1, { 'AVote1' => 1 })) + + assert_false(OutputPrinter.majority_reached?(100, { 'AVote1' => 2, 'AVote2' => 2, 'AVote3' => 2, 'AVote4' => 1, 'AVote5' => 1, 'AVote6' => 1, 'AVote7' => 1, 'AVote8' => 9 })) assert_false(OutputPrinter.majority_reached?(100, {})) assert_false(OutputPrinter.majority_reached?(0, {})) - assert_false(OutputPrinter.majority_reached?(2, { "AVote1" => 1 })) + assert_false(OutputPrinter.majority_reached?(2, { 'AVote1' => 1 })) end def test_position_report assert_equal( - "President\n" + - "\t*AVote4: 60 votes (60.00%)\n" + - "\t AVote3: 20 votes (20.00%)\n" + - "\t Abstained: 20 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 100 votes", - OutputPrinter.position_report( - 100, - 'President', - { "AVote3" => 20, 'AVote4' => 60 } - ).strip + "President\n" + + "\t*AVote4: 60 votes (60.00%)\n" + + "\t AVote3: 20 votes (20.00%)\n" + + "\t Abstained: 20 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 100 votes", + OutputPrinter.position_report( + 100, + 'President', + { 'AVote3' => 20, 'AVote4' => 60 } + ).strip ) assert_equal( - "President (No Majority)\n" + - "\t AVote3: 20 votes (20.00%)\n" + - "\t AVote4: 20 votes (20.00%)\n" + - "\t Abstained: 60 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 100 votes", - OutputPrinter.position_report( - 100, - 'President', - { "AVote4" => 20, 'AVote3' => 20 } - ).strip + "President (No Majority)\n" + + "\t AVote3: 20 votes (20.00%)\n" + + "\t AVote4: 20 votes (20.00%)\n" + + "\t Abstained: 60 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 100 votes", + OutputPrinter.position_report( + 100, + 'President', + { 'AVote4' => 20, 'AVote3' => 20 } + ).strip ) assert_equal( - "President (No Majority)\n" + - "\t AVote4: 21 votes (21.00%)\n" + - "\t AVote2: 20 votes (20.00%)\n" + - "\t AVote3: 20 votes (20.00%)\n" + - "\t Abstained: 39 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 100 votes", - OutputPrinter.position_report( - 100, - 'President', - { "AVote2" => 20, "AVote3" => 20, 'AVote4' => 21 } - ).strip + "President (No Majority)\n" + + "\t AVote4: 21 votes (21.00%)\n" + + "\t AVote2: 20 votes (20.00%)\n" + + "\t AVote3: 20 votes (20.00%)\n" + + "\t Abstained: 39 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 100 votes", + OutputPrinter.position_report( + 100, + 'President', + { 'AVote2' => 20, 'AVote3' => 20, 'AVote4' => 21 } + ).strip ) end def test_vote_report vote_counts = { - 1 => { "George Washington" => 6 }, - 2 => { "Aaron Burr" => 4, "John Adams" => 2 }, - 3 => { "Thomas Jefferson" => 3 }, - 4 => { "Alexander Hamilton" => 3, "Someone Else" => 2, "Another Person" => 1 } + 1 => { 'George Washington' => 6 }, + 2 => { 'Aaron Burr' => 4, 'John Adams' => 2 }, + 3 => { 'Thomas Jefferson' => 3 }, + 4 => { 'Alexander Hamilton' => 3, 'Someone Else' => 2, 'Another Person' => 1 } } column_headers = %w[Password President VP Secretary Treasurer] vote_count = 6 expected = "President\n" + - "\t*George Washington: 6 votes (100.00%)\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes\n\n\n" + - "VP\n" + - "\t*Aaron Burr: 4 votes (66.67%)\n" + - "\t John Adams: 2 votes (33.33%)\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes\n\n\n" + - "Secretary (No Majority)\n" + - "\t Thomas Jefferson: 3 votes (50.00%)\n" + - "\t Abstained: 3 votes\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes\n\n\n" + - "Treasurer (No Majority)\n" + - "\t Alexander Hamilton: 3 votes (50.00%)\n" + - "\t Someone Else: 2 votes (33.33%)\n" + - "\t Another Person: 1 vote (16.67%)\n" + - "-------------------------------------------------\n" + - "\t Total: 6 votes" + "\t*George Washington: 6 votes (100.00%)\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes\n\n\n" + + "VP\n" + + "\t*Aaron Burr: 4 votes (66.67%)\n" + + "\t John Adams: 2 votes (33.33%)\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes\n\n\n" + + "Secretary (No Majority)\n" + + "\t Thomas Jefferson: 3 votes (50.00%)\n" + + "\t Abstained: 3 votes\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes\n\n\n" + + "Treasurer (No Majority)\n" + + "\t Alexander Hamilton: 3 votes (50.00%)\n" + + "\t Someone Else: 2 votes (33.33%)\n" + + "\t Another Person: 1 vote (16.67%)\n" + + "-------------------------------------------------\n" + + "\t Total: 6 votes" assert_equal(expected, OutputPrinter.vote_report(vote_count, column_headers, vote_counts).strip) end end diff --git a/vote_parser.rb b/vote_parser.rb index 46535d7..4619135 100644 --- a/vote_parser.rb +++ b/vote_parser.rb @@ -7,6 +7,7 @@ require 'csv' require 'optparse' +# :nocov: options = { reverse: true } OptionParser.new do |opt| opt.on( @@ -16,6 +17,7 @@ 'If the votes should be counted in chronological order, keeping the first (defaults to false)' ) { |o| options[:reverse] = o } end.parse! +# :nocov: # Read the contents of the given CSV file # @@ -194,11 +196,7 @@ def self.write_election_report(file, election_report, warning = '') # @param [Integer] votes The number of votes they received # @return [String] a formatted string of a single ballot entry def self.ballot_entry_string(candidate_name, votes, percent) - majority_mark = if percent > 50 - '*' - else - ' ' - end + majority_mark = percent > 50 ? '*' : ' ' format("\t%s%-20s %4d vote%s (%.2f%%)\n", Name: "#{candidate_name}:", Votes: votes, S: votes == 1 ? ' ' : 's', MjrMarker: majority_mark, Per: percent) @@ -289,11 +287,7 @@ def self.position_report(vote_count, position_title, position_vote_record) pos_total = sum_position_votes(position_vote_record) individual_report = position_report_individuals(vote_count, pos_total, position_vote_record) - majority_reached_str = if majority_reached?(vote_count, position_vote_record) - '' - else - ' (No Majority)' - end + majority_reached_str = majority_reached?(vote_count, position_vote_record) ? '' : ' (No Majority)' format("\n%s%s\n%s", Pos: position_title, Maj: majority_reached_str, Individuals: individual_report) end