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