Use ExprDeclare in tests instead of expr.include
[dbsrgits/Data-Query.git] / t / expr.t
1 use strictures 1;
2 use Test::More qw(no_plan);
3 use Test::Exception;
4
5 use Data::Query::ExprDeclare;
6 use Data::Query::Constants;
7
8 sub expr_is (&;@) {
9   my $sub = shift;
10   is_deeply(Data::Query::ExprDeclare::_run_expr($sub)->{expr}, @_);
11 }
12
13 expr_is { $_->foo }
14   {
15     type => DQ_IDENTIFIER,
16     elements => [ 'foo' ]
17   },
18   'Simple identifier ok';
19
20
21 expr_is { $_->foo->bar }
22   {
23     type => DQ_IDENTIFIER,
24     elements => [ 'foo', 'bar' ]
25   },
26   'Nested identifier ok';
27
28 expr_is { $_->foo == 3 }
29   {
30     type => DQ_OPERATOR,
31     operator => { Perl => '==' },
32     args => [
33       expr { $_->foo },
34       {
35         type => DQ_VALUE,
36         subtype => { Perl => 'Scalar' },
37         value => 3,
38       },
39     ],
40   },
41   'Simple equality ok';
42
43 expr_is { $_->foo == 3 }
44   {
45     type => DQ_OPERATOR,
46     operator => { Perl => '==' },
47     args => [
48       expr { $_->foo },
49       {
50         type => DQ_VALUE,
51         subtype => { Perl => 'Scalar' },
52         value => 3,
53       },
54     ],
55   },
56   'Simple equality ok';
57
58 expr_is { 3 == $_->foo }
59   {
60     type => DQ_OPERATOR,
61     operator => { Perl => '==' },
62     args => [
63       {
64         type => DQ_VALUE,
65         subtype => { Perl => 'Scalar' },
66         value => 3,
67       },
68       expr { $_->foo },
69     ],
70   },
71   'Operand reversed equality ok';
72
73 throws_ok {
74   expr { $_->foo <=> 3 }
75 } qr/\QCan't use operator <=>/, 'Exception on bad operator';
76
77 expr_is { $_->foo & $_->bar }
78   {
79     type => DQ_OPERATOR,
80     operator => { Perl => 'and' },
81     args => [
82       expr { $_->foo },
83       expr { $_->bar },
84     ],
85   },
86   'Masquerade for & as and ok';
87
88 expr_is { $_->foo | $_->bar }
89   {
90     type => DQ_OPERATOR,
91     operator => { Perl => 'or' },
92     args => [
93       expr { $_->foo },
94       expr { $_->bar },
95     ],
96   },
97   'Masquerade for | as or ok';
98
99 expr_is { !$_->foo }
100   {
101     type => DQ_OPERATOR,
102     operator => { Perl => '!' },
103     args => [ expr { $_->foo } ],
104   },
105   '! ok';