Commit | Line | Data |
9ee33178 |
1 | use strictures 1; |
2 | use Test::More qw(no_plan); |
3 | use Data::Query::ExprBuilder::Identifier; |
b616bc41 |
4 | use Data::Query::Constants qw(DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE); |
9e0200bc |
5 | use Test::Exception; |
9ee33178 |
6 | |
b616bc41 |
7 | sub expr (&) { |
8 | _mk_expr($_[0]); |
9 | } |
10 | |
11 | sub _mk_expr { |
9ee33178 |
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; |
b616bc41 |
23 | is_deeply(_mk_expr($sub), @_); |
9ee33178 |
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'; |
b616bc41 |
40 | |
41 | expr_is { $_->foo == 3 } |
42 | { |
43 | type => DQ_OPERATOR, |
d68bc999 |
44 | operator => { Perl => '==' }, |
b616bc41 |
45 | args => [ |
46 | expr { $_->foo }, |
47 | { |
48 | type => DQ_VALUE, |
d68bc999 |
49 | subtype => { Perl => 'Scalar' }, |
b616bc41 |
50 | value => 3, |
51 | }, |
52 | ], |
53 | }, |
54 | 'Simple equality ok'; |
ef94e94d |
55 | |
56 | expr_is { $_->foo == 3 } |
57 | { |
58 | type => DQ_OPERATOR, |
d68bc999 |
59 | operator => { Perl => '==' }, |
ef94e94d |
60 | args => [ |
61 | expr { $_->foo }, |
62 | { |
63 | type => DQ_VALUE, |
d68bc999 |
64 | subtype => { Perl => 'Scalar' }, |
ef94e94d |
65 | value => 3, |
66 | }, |
67 | ], |
68 | }, |
69 | 'Simple equality ok'; |
70 | |
71 | expr_is { 3 == $_->foo } |
72 | { |
73 | type => DQ_OPERATOR, |
d68bc999 |
74 | operator => { Perl => '==' }, |
ef94e94d |
75 | args => [ |
76 | { |
77 | type => DQ_VALUE, |
d68bc999 |
78 | subtype => { Perl => 'Scalar' }, |
ef94e94d |
79 | value => 3, |
80 | }, |
81 | expr { $_->foo }, |
82 | ], |
83 | }, |
84 | 'Operand reversed equality ok'; |
9e0200bc |
85 | |
86 | throws_ok { |
87 | expr { $_->foo <=> 3 } |
88 | } qr/\QCan't use operator <=>/, 'Exception on bad operator'; |
7e599929 |
89 | |
90 | expr_is { $_->foo & $_->bar } |
91 | { |
92 | type => DQ_OPERATOR, |
d68bc999 |
93 | operator => { Perl => 'and' }, |
7e599929 |
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, |
d68bc999 |
104 | operator => { Perl => 'or' }, |
7e599929 |
105 | args => [ |
106 | expr { $_->foo }, |
107 | expr { $_->bar }, |
108 | ], |
109 | }, |
110 | 'Masquerade for | as or ok'; |
c3633a0e |
111 | |
112 | expr_is { !$_->foo } |
113 | { |
114 | type => DQ_OPERATOR, |
d68bc999 |
115 | operator => { Perl => '!' }, |
c3633a0e |
116 | args => [ expr { $_->foo } ], |
117 | }, |
118 | '! ok'; |