Deprecate -nest in favor of -paren
[dbsrgits/SQL-Abstract.git] / t / 04modifiers.t
old mode 100755 (executable)
new mode 100644 (file)
index 1d73dff..5815af3
@@ -10,7 +10,7 @@ use Data::Dumper;
 use SQL::Abstract;
 
 =begin
-Test -and -or and -nest modifiers, assuming the following:
+Test -and -or modifiers, assuming the following:
 
   * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
     limitation of one modifier type per hahsref)
@@ -145,7 +145,6 @@ my @and_or_tests = (
   },
   # test column multi-cond in arrayref (even more useful)
   {
-#    todo => 'Clarify semantics in 1.52',
     where => { x => { '!=' => [ -and => (1 .. 3) ] } },
     stmt => 'WHERE x != ? AND x != ? AND x != ?',
     bind => [1..3],
@@ -153,12 +152,11 @@ my @and_or_tests = (
 
   # the -or should affect only the inner hashref, as we are not in an outer arrayref
   {
-#    todo => 'Clarify semantics in 1.52',
     where => { x => {
       -or => { '!=', 1, '>=', 2 }, -like => 'x%'
     }},
-    stmt => 'WHERE (x != ? OR x >= ?) AND x LIKE ?',
-    bind => [qw/1 2 x%/],
+    stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
+    bind => [qw/x% 1 2/],
   },
 
   # the -and should affect the OUTER arrayref, while the internal structures remain intact
@@ -178,8 +176,8 @@ my @and_or_tests = (
 
   {
     where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
-    stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ((l = ? OR l = ?) OR c = ? OR d = ? ) AND x = ?',
-    bind => [qw/1 2 11 12 21 22 3 4 9/],
+    stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
+    bind => [qw/1 2 11 12 3 4 21 22 9/],
   },
 
   {
@@ -198,8 +196,8 @@ my @and_or_tests = (
     # explicit OR logic in arrays should leave everything intact
     args => { logic => 'or' },
     where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } }  },
-    stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( l = ? OR l = ? OR c = ? OR d = ? ) AND x = ? ',
-    bind => [qw/1 2 11 12 21 22 3 4 9/],
+    stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
+    bind => [qw/1 2 11 12 3 4 21 22 9/],
   },
 
   {
@@ -301,29 +299,30 @@ my @and_or_tests = (
 );
 
 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
-my @numbered_mods = (
+# -nest is renamed to -paren
+my @backcompat_mods = (
   {
     backcompat => {
-      -and => [a => 10, b => 11],
-      -and2 => [ c => 20, d => 21 ],
+      -and5 => [a => 10, b => 11],
+      -and_2 => [ c => 20, d => 21 ],
       -nest => [ x => 1 ],
       -nest2 => [ y => 2 ],
-      -or => { m => 7, n => 8 },
-      -or2 => { m => 17, n => 18 },
+      -or7 => { m => 7, n => 8 },
+      -or_2 => { m => 17, n => 18 },
     },
     correct => { -and => [
       -and => [a => 10, b => 11],
       -and => [ c => 20, d => 21 ],
-      -nest => [ x => 1 ],
-      -nest => [ y => 2 ],
+      -paren => [ x => 1 ],
+      -paren => [ y => 2 ],
       -or => { m => 7, n => 8 },
       -or => { m => 17, n => 18 },
     ] },
   },
   {
     backcompat => {
-      -and2 => [a => 10, b => 11],
-      -and_3 => [ c => 20, d => 21 ],
+      -and => [a => 10, b => 11],
+      -and2 => [ c => 20, d => 21 ],
       -nest2 => [ x => 1 ],
       -nest_3 => [ y => 2 ],
       -or2 => { m => 7, n => 8 },
@@ -332,15 +331,48 @@ my @numbered_mods = (
     correct => [ -and => [
       -and => [a => 10, b => 11],
       -and => [ c => 20, d => 21 ],
-      -nest => [ x => 1 ],
-      -nest => [ y => 2 ],
+      -paren => [ x => 1 ],
+      -paren => [ y => 2 ],
       -or => { m => 7, n => 8 },
       -or => { m => 17, n => 18 },
     ] ],
   },
 );
 
-plan tests => @and_or_tests*3 + @numbered_mods*4;
+my @paren_tests = (
+ {
+   where => {a => 1, -paren => [b => 2, c => 3]},
+   stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
+   bind  => [qw/2 3 1/],
+ },
+ {
+   where => {a => 1, -paren => {b => 2, c => 3}},
+   stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
+   bind  => [qw/2 3 1/],
+ },
+ {
+   where => {a => 1, -or => {-paren => {b => 2, c => 3}}},
+   stmt  => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
+   bind  => [qw/2 3 1/],
+ },
+ {
+   where => {a => 1, -or => {-paren => [b => 2, c => 3]}},
+   stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
+   bind  => [qw/2 3 1/],
+ },
+ {
+   where => {a => 1, -paren => {-or => {b => 2, c => 3}}},
+   stmt  => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
+   bind  => [qw/2 3 1/],
+ },
+ {
+   where => [a => 1, -paren => {b => 2, c => 3}, -paren => [d => 4, e => 5]],
+   stmt  => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
+   bind  => [qw/1 2 3 4 5/],
+ },
+);
+
+plan tests => @and_or_tests*3 + @backcompat_mods*4 + @paren_tests*2;
 
 for my $case (@and_or_tests) {
   TODO: {
@@ -366,12 +398,47 @@ for my $case (@and_or_tests) {
   }
 }
 
-my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
-for my $case (@numbered_mods) {
+for my $case (@paren_tests) {
+  TODO: {
+    local $TODO = $case->{todo} if $case->{todo};
+
+    local $SQL::Abstract::Test::parenthesis_significant = 1;
     local $Data::Dumper::Terse = 1;
 
-    my @w;
-    local $SIG{__WARN__} = sub { push @w, @_ };
+    my $sql = SQL::Abstract->new ($case->{args} || {});
+    lives_ok (sub {
+      my ($stmt, @bind) = $sql->where($case->{where});
+      is_same_sql_bind(
+        $stmt,
+        \@bind,
+        $case->{stmt},
+        $case->{bind},
+      )
+        || diag "Search term:\n" . Dumper $case->{where};
+    });
+  }
+}
+
+my $numw_str = "\QUse of op_N modifiers is deprecated and will be removed in SQLA v2.0\E";
+my $nestw_str = "\QThe -nest modifier is deprecated in favor of -paren and will be removed in SQLA v2.0\E";
+for my $case (@backcompat_mods) {
+  TODO: {
+    local $TODO = $case->{todo} if $case->{todo};
+
+    local $Data::Dumper::Terse = 1;
+
+    my $w;
+    local $SIG{__WARN__} = sub {
+      if ($_[0] =~ /$numw_str/) {
+        push @{$w->{num}}, $_[0];
+      }
+      elsif ($_[0] =~ /$nestw_str/) {
+        push @{$w->{nest}}, $_[0];
+      }
+      else {
+        warn $_[0];
+      }
+    };
     my $sql = SQL::Abstract->new ($case->{args} || {});
     lives_ok (sub {
       my ($old_s, @old_b) = $sql->where($case->{backcompat});
@@ -386,14 +453,8 @@ for my $case (@numbered_mods) {
         };
     });
 
-    ok (@w, 'Warnings were emitted about a mod_N construct');
-
-    my @non_match;
-    for (@w) {
-      push @non_match, $_ if ($_ !~ /$w_str/);
-    }
-
-    is (@non_match, 0, 'All warnings match the deprecation message')
-      || diag join "\n", 'Rogue warnings:', @non_match;
+    is (@{$w->{num} || []}, 5, 'Correct number of warnings were emitted about a mod_N operator');
+    is (@{$w->{nest} || []}, 2, 'Correct number of warnings were emitted about a -nest operator');
+  }
 }