diff --git a/lib/DDG/Goodie/BinaryLogic.pm b/lib/DDG/Goodie/BinaryLogic.pm old mode 100644 new mode 100755 index 4224a29d5..c901df42f --- a/lib/DDG/Goodie/BinaryLogic.pm +++ b/lib/DDG/Goodie/BinaryLogic.pm @@ -11,7 +11,7 @@ triggers query_raw => qr/.*\s+(and|or|xor|⊕|∧|∨)\s+.*/; triggers query_raw => qr/not\s+.*/; triggers query_raw => qr/¬.*/; -# zci is_cached => 1; +zci is_cached => 1; zci answer_type => "binary_logic"; attribution @@ -31,10 +31,6 @@ code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DD category 'calculations'; topics 'math'; -# TODO: - add binary numbers. -# the parser callback is already available but the -# lexer grammar did not work. -# (BinaryLogic_Actions::bin_number) my $rules = <<'END_OF_GRAMMAR'; :default ::= action => ::first :start ::= Expression @@ -55,7 +51,7 @@ Number ::= | DecimalDigits DecimalDigits ~ [\d]+ -HexDigits ~ [\dA-Fa-f]+ +HexDigits ~ [0-9A-Fa-f]+ :discard ~ whitespace whitespace ~ [\s]+ @@ -92,13 +88,14 @@ sub BinaryLogic_Actions::do_not { } handle query_raw => sub { - my $grammar = Marpa::R2::Scanless::G->new( { source => \$rules } ); - my $recce = Marpa::R2::Scanless::R->new( - { grammar => $grammar, - semantics_package => 'BinaryLogic_Actions' } ); - + my $grammar = Marpa::R2::Scanless::G->new({ source => \$rules }); + my $recce = Marpa::R2::Scanless::R->new({ + grammar => $grammar, + semantics_package => 'BinaryLogic_Actions' + }); + my $input = $_; - + # Substitute the unicode characters. The parser does not seem to # like unicode. $input =~ s/⊕/ xor /; @@ -109,18 +106,16 @@ handle query_raw => sub { # using eval to catch possible errors with $@ eval { $recce->read( \$input ) }; - if ( $@ ) { - return; - } - + return if ( $@ ); + my $value_ref = $recce->value(); return if not defined $value_ref; - + my $text_output = ${$value_ref}; my $html_output = "
Result: " . ${$value_ref} . "
"; - my $heading = "Binary Logic: '" . $_ . "'"; - + my $heading = "Binary Logic"; + return answer => $text_output, html => $html_output, heading => $heading; }; diff --git a/t/BinaryLogic.t b/t/BinaryLogic.t old mode 100644 new mode 100755 index c9baffd44..2a54ef55d --- a/t/BinaryLogic.t +++ b/t/BinaryLogic.t @@ -7,41 +7,93 @@ use DDG::Test::Goodie; use utf8; zci answer_type => 'binary_logic'; -# zci is_cached => 1; +zci is_cached => 1; -sub bl_test { - my $head = $_[0]; - my $text = $_[1]; - - test_zci($text, - html => "
Result: " . $text . "
", - heading => "Binary Logic: '" . $head . "'"); -} ddg_goodie_test( [qw( - DDG::Goodie::BinaryLogic - )], - '4 xor 5' => bl_test('4 xor 5', '1'), - '4 ⊕ 5' => bl_test('4 ⊕ 5', '1'), + DDG::Goodie::BinaryLogic + )], + '4 xor 5' => test_zci('1', + html => "
Result: 1
", + heading => "Binary Logic" + ), + '4 ⊕ 5' => test_zci('1', + html => "
Result: 1
", + heading => "Binary Logic" + ), + '9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985' => test_zci('7378', + html => "
Result: 7378
", + heading => "Binary Logic" + ), - '9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985' => - bl_test('9489 xor 394 xor 9349 xor 39 xor 29 xor 4967 xor 3985', '7378'), + '10 and 12' => test_zci('8', + html => "
Result: 8
", + heading => "Binary Logic" + ), + '10 ∧ 12' => test_zci('8', + html => "
Result: 8
", + heading => "Binary Logic" + ), - '10 and 12' => bl_test('10 and 12', '8'), - '10 ∧ 12' => bl_test('10 ∧ 12', '8'), + '52 or 100' => test_zci('116', + html => "
Result: 116
", + heading => "Binary Logic" + ), + '52 ∨ 100' => test_zci('116', + html => "
Result: 116
", + heading => "Binary Logic" + ), - '52 or 100' => bl_test('52 or 100', '116'), - '52 ∨ 100' => bl_test('52 ∨ 100', '116'), + '23 and (30 or 128)' => test_zci('22', + html => "
Result: 22
", + heading => "Binary Logic" + ), + '23 ∧ (30 ∨ 128)' => test_zci('22', + html => "
Result: 22
", + heading => "Binary Logic" + ), - '23 and (30 or 128)' => bl_test('23 and (30 or 128)', '22'), - '23 ∧ (30 ∨ 128)' => bl_test('23 ∧ (30 ∨ 128)', '22'), + '0x999 xor 0x589' => test_zci('3088', + html => "
Result: 3088
", + heading => "Binary Logic" + ), + '0x999 ⊕ 0x589' => test_zci('3088', + html => "
Result: 3088
", + heading => "Binary Logic" + ), - '0x999 xor 0x589' => bl_test('0x999 xor 0x589', '3088'), - '0x999 ⊕ 0x589' => bl_test('0x999 ⊕ 0x589', '3088'), + 'not 1' => test_zci('18446744073709551614', + html => "
Result: 18446744073709551614
", + heading => "Binary Logic" + ), + '¬1' => test_zci('18446744073709551614', + html => "
Result: 18446744073709551614
", + heading => "Binary Logic" + ), + + '3 and 2' => test_zci('2', + html => "
Result: 2
", + heading => "Binary Logic" + ), + '1 or 1234' => test_zci('1235', + html => "
Result: 1235
", + heading => "Binary Logic" + ), + + '34 or 100' => test_zci('102', + html => "
Result: 102
", + heading => "Binary Logic" + ), + '10 and (30 or 128)' => test_zci('10', + html => "
Result: 10
", + heading => "Binary Logic" + ), + '0x01 or not 0X100' => test_zci('1', + html => "
Result: 1
", + heading => "Binary Logic" + ), - 'not 1' => bl_test('not 1', '18446744073709551614'), - '¬1' => bl_test('¬1', '18446744073709551614'), ); done_testing; \ No newline at end of file