diff --git a/lib/DDG/Goodie/GoldenRatio.pm b/lib/DDG/Goodie/GoldenRatio.pm index 62920c82b..efc217319 100644 --- a/lib/DDG/Goodie/GoldenRatio.pm +++ b/lib/DDG/Goodie/GoldenRatio.pm @@ -4,11 +4,25 @@ package DDG::Goodie::GoldenRatio; use strict; use DDG::Goodie; -zci answer_type => "golden_ratio"; +zci answer_type => 'golden_ratio'; zci is_cached => 1; -triggers start => "golden ratio"; +triggers start => 'golden ratio'; + +sub answer { + my ($ratio, $explanation) = @_; + + return "Golden ratio: $ratio", structured_answer => { + data => { + title => "$ratio", + subtitle => "$explanation" + }, + templates => { + group => 'text' + } + }; +} handle remainder => sub { my $input = $_; @@ -16,14 +30,14 @@ handle remainder => sub { my $result = 0; if ($input =~ /^(?:(?:(\?)\s*:\s*(\d+(?:\.\d+)?))|(?:(\d+(?:\.\d+)?)\s*:\s*(\?)))$/) { - if ($1 && $1 eq "?") { + if ($1 && $1 eq '?') { # ? : x $result = $2 / $golden_ratio; - return "Golden ratio: $result : $2"; - } elsif ($4 && $4 eq "?") { + return answer("$result:$2", "Golden ratio for ?:$2"); + } elsif ($4 && $4 eq '?') { # x : ? $result = $3 * $golden_ratio; - return "Golden ratio: $3 : $result"; + return answer("$3:$result", "Golden ratio for $3:?"); } } return; diff --git a/t/GoldenRatio.t b/t/GoldenRatio.t index b5f11dd16..c38181b09 100644 --- a/t/GoldenRatio.t +++ b/t/GoldenRatio.t @@ -9,25 +9,39 @@ use DDG::Test::Goodie; zci answer_type => 'golden_ratio'; zci is_cached => 1; +sub build_structured_answer { + my ($left_side, $right_side, $input) = @_; + + return "Golden ratio: $left_side:$right_side", structured_answer => { + data => { + title => "$left_side:$right_side", + subtitle => "Golden ratio for $input" + }, + templates => { + group => 'text' + } + }; +} + ddg_goodie_test( [qw( DDG::Goodie::GoldenRatio )], - 'golden ratio 1:?' => test_zci('Golden ratio: 1 : 1.61803398874989'), - "golden ratio 2.5:?" => test_zci("Golden ratio: 2.5 : 4.04508497187474"), - "golden ratio 450:?" => test_zci("Golden ratio: 450 : 728.115294937453"), - "golden ratio 900:?" => test_zci("Golden ratio: 900 : 1456.23058987491"), - "golden ratio 1024.56:?" => test_zci("Golden ratio: 1024.56 : 1657.77290351359"), - "golden ratio ?:900" => test_zci("Golden ratio: 556.230589874905 : 900"), - "golden ratio ?:768.5" => test_zci("Golden ratio: 474.959120354294 : 768.5"), - "golden ratio ?:1680.12345678" => test_zci("Golden ratio: 1038.37340158601 : 1680.12345678"), - "golden ratio 1 : ?" => test_zci("Golden ratio: 1 : 1.61803398874989"), - "golden ratio 1 :?" => test_zci("Golden ratio: 1 : 1.61803398874989"), - "golden ratio 1: ?" => test_zci("Golden ratio: 1 : 1.61803398874989"), - "golden ratio ? : 9" => test_zci("Golden ratio: 5.56230589874905 : 9"), - "golden ratio ? :9" => test_zci("Golden ratio: 5.56230589874905 : 9"), - "golden ratio ?: 9" => test_zci("Golden ratio: 5.56230589874905 : 9"), - 'golden ratio ?:123.345' => test_zci('Golden ratio: 76.2314023423558 : 123.345'), + 'golden ratio 1:?' => test_zci(build_structured_answer('1', '1.61803398874989', '1:?')), + 'golden ratio 2.5:?' => test_zci(build_structured_answer('2.5', '4.04508497187474', '2.5:?')), + 'golden ratio 450:?' => test_zci(build_structured_answer('450', '728.115294937453', '450:?')), + 'golden ratio 900:?' => test_zci(build_structured_answer('900', '1456.23058987491', '900:?')), + 'golden ratio 1024.56:?' => test_zci(build_structured_answer('1024.56', '1657.77290351359', '1024.56:?')), + 'golden ratio ?:900' => test_zci(build_structured_answer('556.230589874905', '900', '?:900')), + 'golden ratio ?:768.5' => test_zci(build_structured_answer('474.959120354294', '768.5', '?:768.5')), + 'golden ratio ?:1680.12345678' => test_zci(build_structured_answer('1038.37340158601', '1680.12345678', '?:1680.12345678')), + 'golden ratio 1 : ?' => test_zci(build_structured_answer('1', '1.61803398874989', '1:?')), + 'golden ratio 1 :?' => test_zci(build_structured_answer('1', '1.61803398874989', '1:?')), + 'golden ratio 1: ?' => test_zci(build_structured_answer('1', '1.61803398874989', '1:?')), + 'golden ratio ? : 9' => test_zci(build_structured_answer('5.56230589874905', '9', '?:9')), + 'golden ratio ? :9' => test_zci(build_structured_answer('5.56230589874905', '9', '?:9')), + 'golden ratio ?: 9' => test_zci(build_structured_answer('5.56230589874905', '9', '?:9')), + 'golden ratio ?:123.345' => test_zci(build_structured_answer('76.2314023423558', '123.345', '?:123.345')), ); done_testing;