revert previous revision
[dbsrgits/DBIx-Class-Historic.git] / t / lib / DBIC / SqlMakerTest.pm
index 0dce0d1..cf33fd9 100644 (file)
@@ -1,24 +1,22 @@
-package # hide from PAUSE
-  DBIC::SqlMakerTest;
+package DBIC::SqlMakerTest;
 
 use strict;
 use warnings;
 
 use base qw/Test::Builder::Module Exporter/;
 
-use Exporter;
-use Data::Dumper;
-
 our @EXPORT = qw/
   &is_same_sql_bind
+  &is_same_sql
+  &is_same_bind
   &eq_sql
   &eq_bind
+  &eq_sql_bind
 /;
 
 
 {
-  package # hide from PAUSE
-    DBIC::SqlMakerTest::SQLATest;
+  package DBIC::SqlMakerTest::SQLATest;
 
   # replacement for SQL::Abstract::Test if not available
 
@@ -27,7 +25,10 @@ our @EXPORT = qw/
 
   use base qw/Test::Builder::Module Exporter/;
 
+  use Scalar::Util qw(looks_like_number blessed reftype);
   use Data::Dumper;
+  use Test::Builder;
+  use Test::Deep qw(eq_deeply);
 
   our $tb = __PACKAGE__->builder;
 
@@ -41,19 +42,59 @@ our @EXPORT = qw/
     $tb->ok($same_sql && $same_bind, $msg);
 
     if (!$same_sql) {
-      $tb->diag("SQL expressions differ\n"
-        . "     got: $sql1\n"
-        . "expected: $sql2\n"
-      );
+      _sql_differ_diag($sql1, $sql2);
+    }
+    if (!$same_bind) {
+      _bind_differ_diag($bind_ref1, $bind_ref2);
+    }
+  }
+
+  sub is_same_sql
+  {
+    my ($sql1, $sql2, $msg) = @_;
+
+    my $same_sql = eq_sql($sql1, $sql2);
+
+    $tb->ok($same_sql, $msg);
+
+    if (!$same_sql) {
+      _sql_differ_diag($sql1, $sql2);
     }
+  }
+
+  sub is_same_bind
+  {
+    my ($bind_ref1, $bind_ref2, $msg) = @_;
+
+    my $same_bind = eq_bind($bind_ref1, $bind_ref2);
+
+    $tb->ok($same_bind, $msg);
+
     if (!$same_bind) {
-      $tb->diag("BIND values differ\n"
-        . "     got: " . Dumper($bind_ref1)
-        . "expected: " . Dumper($bind_ref2)
-      );
+      _bind_differ_diag($bind_ref1, $bind_ref2);
     }
   }
 
+  sub _sql_differ_diag
+  {
+    my ($sql1, $sql2) = @_;
+
+    $tb->diag("SQL expressions differ\n"
+      . "     got: $sql1\n"
+      . "expected: $sql2\n"
+    );
+  }
+
+  sub _bind_differ_diag
+  {
+    my ($bind_ref1, $bind_ref2) = @_;
+
+    $tb->diag("BIND values differ\n"
+      . "     got: " . Dumper($bind_ref1)
+      . "expected: " . Dumper($bind_ref2)
+    );
+  }
+
   sub eq_sql
   {
     my ($left, $right) = @_;
@@ -68,22 +109,14 @@ our @EXPORT = qw/
   {
     my ($bind_ref1, $bind_ref2) = @_;
 
-    return stringify_bind($bind_ref1) eq stringify_bind($bind_ref2);
+    return eq_deeply($bind_ref1, $bind_ref2);
   }
 
-  sub stringify_bind
+  sub eq_sql_bind
   {
-    my ($bind) = @_;
-
-    foreach (ref $bind) {
-      /^$/ and return $bind;
-      /^ARRAY$/ and return join("\n", map { stringify_bind($_) } @$bind);
-      /^HASH$/ and return join(
-        "\n", map { $_ . " => " . stringify_bind($bind->{$_}) } keys %$bind
-      );
-      /^SCALAR$/ and return "\\" . stringify_bind($$bind);
-      return '' . $bind;
-    }
+    my ($sql1, $bind_ref1, $sql2, $bind_ref2) = @_;
+
+    return eq_sql($sql1, $sql2) && eq_bind($bind_ref1, $bind_ref2);
   }
 }
 
@@ -92,14 +125,20 @@ if ($@ eq '') {
   # SQL::Abstract::Test available
 
   *is_same_sql_bind = \&SQL::Abstract::Test::is_same_sql_bind;
+  *is_same_sql = \&SQL::Abstract::Test::is_same_sql;
+  *is_same_bind = \&SQL::Abstract::Test::is_same_bind;
   *eq_sql = \&SQL::Abstract::Test::eq_sql;
   *eq_bind = \&SQL::Abstract::Test::eq_bind;
+  *eq_sql_bind = \&SQL::Abstract::Test::eq_sql_bind;
 } else {
   # old SQL::Abstract
 
   *is_same_sql_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_sql_bind;
+  *is_same_sql = \&DBIC::SqlMakerTest::SQLATest::is_same_sql;
+  *is_same_bind = \&DBIC::SqlMakerTest::SQLATest::is_same_bind;
   *eq_sql = \&DBIC::SqlMakerTest::SQLATest::eq_sql;
   *eq_bind = \&DBIC::SqlMakerTest::SQLATest::eq_bind;
+  *eq_sql_bind = \&DBIC::SqlMakerTest::SQLATest::eq_sql_bind;
 }
 
 
@@ -148,6 +187,28 @@ comparison of bind values.
 Compares given and expected pairs of C<($sql, \@bind)>, and calls
 L<Test::Builder/ok> on the result, with C<$test_msg> as message.
 
+=head2 is_same_sql
+
+  is_same_sql(
+    $given_sql,
+    $expected_sql,
+    $test_msg
+  );
+
+Compares given and expected SQL statement, and calls L<Test::Builder/ok> on the
+result, with C<$test_msg> as message.
+
+=head2 is_same_bind
+
+  is_same_bind(
+    \@given_bind, 
+    \@expected_bind,
+    $test_msg
+  );
+
+Compares given and expected bind value lists, and calls L<Test::Builder/ok> on
+the result, with C<$test_msg> as message.
+
 =head2 eq_sql
 
   my $is_same = eq_sql($given_sql, $expected_sql);
@@ -160,6 +221,16 @@ Compares the two SQL statements. Returns true IFF they are equivalent.
 
 Compares two lists of bind values. Returns true IFF their values are the same.
 
+=head2 eq_sql_bind
+
+  my $is_same = eq_sql_bind(
+    $given_sql, \@given_bind,
+    $expected_sql, \@expected_bind
+  );
+
+Compares the two SQL statements and the two lists of bind values. Returns true
+IFF they are equivalent and the bind values are the same.
+
 
 =head1 SEE ALSO