unary operators
[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 use Test::Exception;
6
7 sub expr (&) {
8   _mk_expr($_[0]);
9 }
10
11 sub _mk_expr {
12   local $_ = Data::Query::ExprBuilder::Identifier->new({
13     expr => {
14       type => DQ_IDENTIFIER,
15       elements => [],
16     },
17   });
18   $_[0]->()->{expr};
19 }
20
21 sub expr_is (&;@) {
22   my $sub = shift;
23   is_deeply(_mk_expr($sub), @_);
24 }
25
26 expr_is { $_->foo }
27   {
28     type => DQ_IDENTIFIER,
29     elements => [ 'foo' ]
30   },
31   'Simple identifier ok';
32
33
34 expr_is { $_->foo->bar }
35   {
36     type => DQ_IDENTIFIER,
37     elements => [ 'foo', 'bar' ]
38   },
39   'Nested identifier ok';
40
41 expr_is { $_->foo == 3 }
42   {
43     type => DQ_OPERATOR,
44     operator => { perl => '==' },
45     args => [
46       expr { $_->foo },
47       {
48         type => DQ_VALUE,
49         subtype => { perl => 'Scalar' },
50         value => 3,
51       },
52     ],
53   },
54   'Simple equality ok';
55
56 expr_is { $_->foo == 3 }
57   {
58     type => DQ_OPERATOR,
59     operator => { perl => '==' },
60     args => [
61       expr { $_->foo },
62       {
63         type => DQ_VALUE,
64         subtype => { perl => 'Scalar' },
65         value => 3,
66       },
67     ],
68   },
69   'Simple equality ok';
70
71 expr_is { 3 == $_->foo }
72   {
73     type => DQ_OPERATOR,
74     operator => { perl => '==' },
75     args => [
76       {
77         type => DQ_VALUE,
78         subtype => { perl => 'Scalar' },
79         value => 3,
80       },
81       expr { $_->foo },
82     ],
83   },
84   'Operand reversed equality ok';
85
86 throws_ok {
87   expr { $_->foo <=> 3 }
88 } qr/\QCan't use operator <=>/, 'Exception on bad operator';
89
90 expr_is { $_->foo & $_->bar }
91   {
92     type => DQ_OPERATOR,
93     operator => { perl => 'and' },
94     args => [
95       expr { $_->foo },
96       expr { $_->bar },
97     ],
98   },
99   'Masquerade for & as and ok';
100
101 expr_is { $_->foo | $_->bar }
102   {
103     type => DQ_OPERATOR,
104     operator => { perl => 'or' },
105     args => [
106       expr { $_->foo },
107       expr { $_->bar },
108     ],
109   },
110   'Masquerade for | as or ok';
111
112 expr_is { !$_->foo }
113   {
114     type => DQ_OPERATOR,
115     operator => { perl => '!' },
116     args => [ expr { $_->foo } ],
117   },
118   '! ok';