fill out operator list and test not-currently-supported 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';