diff --git a/lib/DDG/Goodie/Coin.pm b/lib/DDG/Goodie/Coin.pm old mode 100644 new mode 100755 index 1fcdbffbb..c35eed337 --- a/lib/DDG/Goodie/Coin.pm +++ b/lib/DDG/Goodie/Coin.pm @@ -32,13 +32,15 @@ handle query_lc => sub { return unless @output; my $result = join(', ', @output); - return ( - $result . ' (random)', - structured_answer => { - input => [$flips], - operation => 'Flip coin', - result => $result - }); + return $result . ' (random)', structured_answer => { + data => { + title => $result, + subtitle => "Flip coin: $flips" + }, + templates => { + group => 'text' + } + }; }; 1; diff --git a/t/Coin.t b/t/Coin.t old mode 100644 new mode 100755 index 28ed8549c..ee6078f82 --- a/t/Coin.t +++ b/t/Coin.t @@ -9,104 +9,36 @@ use DDG::Test::Goodie; zci answer_type => 'coin'; zci is_cached => 0; +sub build_test { + my ($input, $text, $title) = @_; + return test_zci($text, structured_answer => { + data => { + title => $title, + subtitle => "Flip coin: $input" + }, + templates => { + group => 'text' + } + }); +} + +my $single_text_re = re(qr/(heads|tails) \(random\)/); +my $single_title_re = re(qr/^(heads|tails)$/); + ddg_goodie_test( [qw( DDG::Goodie::Coin )], - 'flip a coin' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'flip coin' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'coin flip' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'coin toss' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'flip 1 coin' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'flip 2 coins' => test_zci( - re(qr/(heads|tails), (heads|tails) \(random\)/), - structured_answer => { - input => [2], - operation => 'Flip coin', - result => re(qr/(heads|tails), /) - } - ), - 'toss a coin' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'toss 1 coin' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'toss 2 coins' => test_zci( - re(qr/(heads|tails), (heads|tails) \(random\)/), - structured_answer => { - input => [2], - operation => 'Flip coin', - result => re(qr/(heads|tails), /) - } - ), - 'heads or tails' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'heads or tails?' => test_zci( - re(qr/(heads|tails) \(random\)/), - structured_answer => { - input => [1], - operation => 'Flip coin', - result => re(qr/^(heads|tails)$/) - } - ), - 'flip 4 coins' => test_zci( - re(qr/((heads|tails),? ){4}\(random\)/), - structured_answer => { - input => [4], - operation => 'Flip coin', - result => re(qr/(heads|tails),? /) - } - ), + 'flip a coin' => build_test(1, $single_text_re, $single_title_re), + 'flip coin' => build_test(1, $single_text_re, $single_title_re), + 'coin flip' => build_test(1, $single_text_re, $single_title_re), + 'coin toss' => build_test(1, $single_text_re, $single_title_re), + 'flip 1 coin' => build_test(1, $single_text_re, $single_title_re), + 'heads or tails' => build_test(1, $single_text_re, $single_title_re), + 'heads or tails?' => build_test(1, $single_text_re, $single_title_re), + 'toss a coin' => build_test(1, $single_text_re, $single_title_re), + 'toss 1 coin' => build_test(1, $single_text_re, $single_title_re), + 'flip 2 coins' => build_test(2, re(qr/(heads|tails), (heads|tails) \(random\)/), re(qr/(heads|tails), /)), + 'toss 2 coins' => build_test(2, re(qr/(heads|tails), (heads|tails) \(random\)/), re(qr/(heads|tails), /)), + 'flip 4 coins' => build_test(4, re(qr/((heads|tails),? ){4}\(random\)/), re(qr/(heads|tails),? /)) ); done_testing;