[Bps-public-commit] r16966 - Parse-BooleanLogic/t

ruz at bestpractical.com ruz at bestpractical.com
Fri Nov 21 16:55:42 EST 2008


Author: ruz
Date: Fri Nov 21 16:55:42 2008
New Revision: 16966

Added:
   Parse-BooleanLogic/t/custom_googlish.t
   Parse-BooleanLogic/t/custom_ops.t
   Parse-BooleanLogic/t/custom_parens.t
   Parse-BooleanLogic/t/utils.pl
Modified:
   Parse-BooleanLogic/t/basics.t

Log:
* more tests

Modified: Parse-BooleanLogic/t/basics.t
==============================================================================
--- Parse-BooleanLogic/t/basics.t	(original)
+++ Parse-BooleanLogic/t/basics.t	Fri Nov 21 16:55:42 2008
@@ -2,35 +2,34 @@
 use strict;
 use warnings;
 
-use Test::More tests => 14;
-use Test::Deep;
+use Test::More tests => 16;
+BEGIN { require "t/utils.pl" };
 
 use_ok 'Parse::BooleanLogic';
 
 
 my $parser = new Parse::BooleanLogic;
 
-sub parse_cmp($$) {
-    my ($string, $struct) = @_;
-    cmp_deeply $parser->as_array($string), $struct, $string;
-}
-
 parse_cmp
+    $parser,
     '',
     [],
 ;
 
 parse_cmp
+    $parser,
     'x = 10',
     [{ operand => 'x = 10' }],
 ;
 
 parse_cmp
+    $parser,
     '(x = 10)',
     [[{ operand => 'x = 10' }]],
 ;
 
 parse_cmp
+    $parser,
     '(x = 10) OR y = "Y"',
     [
         [{ operand => 'x = 10' }],
@@ -40,46 +39,73 @@
 ;
 
 parse_cmp
+    $parser,
+    ' (x)',
+    [
+        [{ operand => 'x' }],
+    ],
+;
+
+parse_cmp
+    $parser,
+    '(x) OR (y)',
+    [
+        [{ operand => 'x' }],
+        'OR',
+        [{ operand => 'y' }]
+    ],
+;
+
+parse_cmp
+    $parser,
     'just a string',
     [{ operand => 'just a string' }],
 ;
 
 parse_cmp
+    $parser,
     '"quoted string"',
     [{ operand => '"quoted string"' }],
 ;
 
 parse_cmp
+    $parser,
     '"quoted string (with parens)"',
     [{ operand => '"quoted string (with parens)"' }],
 ;
 
 parse_cmp
+    $parser,
     'string "quoted" in the middle',
     [{ operand => 'string "quoted" in the middle' }],
 ;
 
 parse_cmp
+    $parser,
     'string OR string',
     [{ operand => 'string' }, 'OR', { operand => 'string' }],
 ;
 
 parse_cmp
+    $parser,
     '"OR" OR string',
     [{ operand => '"OR"' }, 'OR', { operand => 'string' }],
 ;
 
 parse_cmp
+    $parser,
     "recORd = 3",
     [{ operand => "recORd = 3" }],
 ;
 
 parse_cmp
+    $parser,
     "op ORheading = 3",
     [{ operand => "op ORheading = 3" }],
 ;
 
 parse_cmp
+    $parser,
     "operAND",
     [{ operand => "operAND" }],
 ;

Added: Parse-BooleanLogic/t/custom_googlish.t
==============================================================================
--- (empty file)
+++ Parse-BooleanLogic/t/custom_googlish.t	Fri Nov 21 16:55:42 2008
@@ -0,0 +1,28 @@
+use strict;
+use warnings;
+
+use Test::More tests => 4;
+BEGIN { require "t/utils.pl" };
+
+use_ok 'Parse::BooleanLogic';
+
+my $parser = Parse::BooleanLogic->new( operators => ['', 'OR'] );
+
+parse_cmp
+    $parser,
+    'x y',
+    [ { operand => 'x' }, '', { operand => 'y' } ],
+;
+
+parse_cmp
+    $parser,
+    'test from:me subject:"like this"',
+    [ { operand => 'test' }, '', { operand => 'from:me' }, '', { operand => 'subject:"like this"' } ],
+;
+
+parse_cmp
+    $parser,
+    'test (from:me OR to:me)',
+    [ { operand => 'test' }, '', [{ operand => 'from:me' }, 'OR', { operand => 'to:me' }] ],
+;
+

Added: Parse-BooleanLogic/t/custom_ops.t
==============================================================================
--- (empty file)
+++ Parse-BooleanLogic/t/custom_ops.t	Fri Nov 21 16:55:42 2008
@@ -0,0 +1,53 @@
+
+use strict;
+use warnings;
+
+use Test::More tests => 8;
+BEGIN { require "t/utils.pl" };
+
+use_ok 'Parse::BooleanLogic';
+
+my $parser = Parse::BooleanLogic->new( operators => [qw(& |)] );
+
+parse_cmp
+    $parser,
+    'x = 10',
+    [{ operand => 'x = 10' }],
+;
+
+parse_cmp
+    $parser,
+    'x | y',
+    [ { operand => 'x' }, '|', { operand => 'y' } ],
+;
+
+parse_cmp
+    $parser,
+    'x| y',
+    [ { operand => 'x' }, '|', { operand => 'y' } ],
+;
+
+parse_cmp
+    $parser,
+    'x |y',
+    [ { operand => 'x' }, '|', { operand => 'y' } ],
+;
+
+parse_cmp
+    $parser,
+    '(x) | (y)',
+    [ [{ operand => 'x' }], '|', [{ operand => 'y' }] ],
+;
+
+parse_cmp
+    $parser,
+    '(x)| (y)',
+    [ [{ operand => 'x' }], '|', [{ operand => 'y' }] ],
+;
+
+parse_cmp
+    $parser,
+    '(x) |(y)',
+    [ [{ operand => 'x' }], '|', [{ operand => 'y' }] ],
+;
+

Added: Parse-BooleanLogic/t/custom_parens.t
==============================================================================
--- (empty file)
+++ Parse-BooleanLogic/t/custom_parens.t	Fri Nov 21 16:55:42 2008
@@ -0,0 +1,62 @@
+
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+BEGIN { require "t/utils.pl" };
+
+use_ok 'Parse::BooleanLogic';
+
+my $parser = Parse::BooleanLogic->new( parens => [qw({ })] );
+
+parse_cmp
+    $parser,
+    '',
+    [],
+;
+
+parse_cmp
+    $parser,
+    'x = 10',
+    [{ operand => 'x = 10' }],
+;
+
+parse_cmp
+    $parser,
+    '(x = 10)',
+    [{ operand => '(x = 10)' }],
+;
+parse_cmp
+    $parser,
+    '{x = 10}',
+    [[{ operand => 'x = 10' }]],
+;
+
+parse_cmp
+    $parser,
+    '{x = 10} OR y = "Y"',
+    [
+        [{ operand => 'x = 10' }],
+        'OR',
+        { operand => 'y = "Y"' }
+    ],
+;
+
+parse_cmp
+    $parser,
+    'just a string',
+    [{ operand => 'just a string' }],
+;
+
+parse_cmp
+    $parser,
+    '"quoted string {with parens}"',
+    [{ operand => '"quoted string {with parens}"' }],
+;
+
+parse_cmp
+    $parser,
+    'string OR string',
+    [{ operand => 'string' }, 'OR', { operand => 'string' }],
+;
+

Added: Parse-BooleanLogic/t/utils.pl
==============================================================================
--- (empty file)
+++ Parse-BooleanLogic/t/utils.pl	Fri Nov 21 16:55:42 2008
@@ -0,0 +1,33 @@
+
+use strict;
+use warnings;
+
+use Data::Dumper;
+use Test::Deep;
+
+{
+my ($tree, $node, @pnodes, @linear);
+my %callback;
+$callback{'open_paren'} = sub {
+    push @pnodes, $node;
+    push @{ $pnodes[-1] }, $node = [];
+    push @linear, '(';
+};
+$callback{'close_paren'} = sub { $node = pop @pnodes; push @linear, ')' };
+$callback{'operator'} = sub { push @$node, $_[0]; push @linear, '<operator, '. $_[0] .'>'; };
+$callback{'operand'} = sub { push @$node, { operand => $_[0] }; push @linear, '<operand, "'. $_[0] .'">' };
+$callback{'error'} = sub { die "$_[0]\n\nAt this point we have: ". Dumper \@linear; };
+    
+sub parse_cmp($$$) {
+    local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+    my ($parser, $string, $expected) = @_;
+
+    $node = $tree = [];
+    @pnodes = @linear = ();
+
+    $parser->parse( string => $string, callback => \%callback );
+    cmp_deeply $tree, $expected, $string or diag Dumper $tree;
+} }
+
+1;



More information about the Bps-public-commit mailing list