add compose routine and refactor FetchFirst to functional style
[dbsrgits/Data-Query.git] / t / expr.t
index d408e42..3621e11 100644 (file)
--- a/t/expr.t
+++ b/t/expr.t
@@ -1,21 +1,12 @@
 use strictures 1;
 use Test::More qw(no_plan);
-use Data::Query::ExprBuilder::Identifier;
-use Data::Query::Constants qw(DQ_IDENTIFIER);
-
-sub mk_expr {
-  local $_ = Data::Query::ExprBuilder::Identifier->new({
-    expr => {
-      type => DQ_IDENTIFIER,
-      elements => [],
-    },
-  });
-  $_[0]->()->{expr};
-}
+use Test::Exception;
+
+BEGIN { require 't/expr.include' }
 
 sub expr_is (&;@) {
   my $sub = shift;
-  is_deeply(mk_expr($sub), @_);
+  is_deeply(_run_expr($sub)->{expr}, @_);
 }
 
 expr_is { $_->foo }
@@ -32,3 +23,82 @@ expr_is { $_->foo->bar }
     elements => [ 'foo', 'bar' ]
   },
   'Nested identifier ok';
+
+expr_is { $_->foo == 3 }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => '==' },
+    args => [
+      expr { $_->foo },
+      {
+        type => DQ_VALUE,
+        subtype => { Perl => 'Scalar' },
+        value => 3,
+      },
+    ],
+  },
+  'Simple equality ok';
+
+expr_is { $_->foo == 3 }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => '==' },
+    args => [
+      expr { $_->foo },
+      {
+        type => DQ_VALUE,
+        subtype => { Perl => 'Scalar' },
+        value => 3,
+      },
+    ],
+  },
+  'Simple equality ok';
+
+expr_is { 3 == $_->foo }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => '==' },
+    args => [
+      {
+        type => DQ_VALUE,
+        subtype => { Perl => 'Scalar' },
+        value => 3,
+      },
+      expr { $_->foo },
+    ],
+  },
+  'Operand reversed equality ok';
+
+throws_ok {
+  expr { $_->foo <=> 3 }
+} qr/\QCan't use operator <=>/, 'Exception on bad operator';
+
+expr_is { $_->foo & $_->bar }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => 'and' },
+    args => [
+      expr { $_->foo },
+      expr { $_->bar },
+    ],
+  },
+  'Masquerade for & as and ok';
+
+expr_is { $_->foo | $_->bar }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => 'or' },
+    args => [
+      expr { $_->foo },
+      expr { $_->bar },
+    ],
+  },
+  'Masquerade for | as or ok';
+
+expr_is { !$_->foo }
+  {
+    type => DQ_OPERATOR,
+    operator => { Perl => '!' },
+    args => [ expr { $_->foo } ],
+  },
+  '! ok';