test reversed operands
[dbsrgits/Data-Query.git] / t / expr.t
1 use strictures 1;
2 use Test::More qw(no_plan);
3 use Data::Query::ExprBuilder::Identifier;
4 use Data::Query::Constants qw(DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE);
5
6 sub expr (&) {
7   _mk_expr($_[0]);
8 }
9
10 sub _mk_expr {
11   local $_ = Data::Query::ExprBuilder::Identifier->new({
12     expr => {
13       type => DQ_IDENTIFIER,
14       elements => [],
15     },
16   });
17   $_[0]->()->{expr};
18 }
19
20 sub expr_is (&;@) {
21   my $sub = shift;
22   is_deeply(_mk_expr($sub), @_);
23 }
24
25 expr_is { $_->foo }
26   {
27     type => DQ_IDENTIFIER,
28     elements => [ 'foo' ]
29   },
30   'Simple identifier ok';
31
32
33 expr_is { $_->foo->bar }
34   {
35     type => DQ_IDENTIFIER,
36     elements => [ 'foo', 'bar' ]
37   },
38   'Nested identifier ok';
39
40 expr_is { $_->foo == 3 }
41   {
42     type => DQ_OPERATOR,
43     operator => { perl => '==' },
44     args => [
45       expr { $_->foo },
46       {
47         type => DQ_VALUE,
48         subtype => { perl => 'Scalar' },
49         value => 3,
50       },
51     ],
52   },
53   'Simple equality ok';
54
55 expr_is { $_->foo == 3 }
56   {
57     type => DQ_OPERATOR,
58     operator => { perl => '==' },
59     args => [
60       expr { $_->foo },
61       {
62         type => DQ_VALUE,
63         subtype => { perl => 'Scalar' },
64         value => 3,
65       },
66     ],
67   },
68   'Simple equality ok';
69
70 expr_is { 3 == $_->foo }
71   {
72     type => DQ_OPERATOR,
73     operator => { perl => '==' },
74     args => [
75       {
76         type => DQ_VALUE,
77         subtype => { perl => 'Scalar' },
78         value => 3,
79       },
80       expr { $_->foo },
81     ],
82   },
83   'Operand reversed equality ok';