basic expression rendering for SQL::Naive
[dbsrgits/Data-Query.git] / t / expr.t
CommitLineData
9ee33178 1use strictures 1;
2use Test::More qw(no_plan);
3use Data::Query::ExprBuilder::Identifier;
b616bc41 4use Data::Query::Constants qw(DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE);
9e0200bc 5use Test::Exception;
9ee33178 6
b616bc41 7sub expr (&) {
8 _mk_expr($_[0]);
9}
10
11sub _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
21sub expr_is (&;@) {
22 my $sub = shift;
b616bc41 23 is_deeply(_mk_expr($sub), @_);
9ee33178 24}
25
26expr_is { $_->foo }
27 {
28 type => DQ_IDENTIFIER,
29 elements => [ 'foo' ]
30 },
31 'Simple identifier ok';
32
33
34expr_is { $_->foo->bar }
35 {
36 type => DQ_IDENTIFIER,
37 elements => [ 'foo', 'bar' ]
38 },
39 'Nested identifier ok';
b616bc41 40
41expr_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
56expr_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
71expr_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
86throws_ok {
87 expr { $_->foo <=> 3 }
88} qr/\QCan't use operator <=>/, 'Exception on bad operator';
7e599929 89
90expr_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
101expr_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
112expr_is { !$_->foo }
113 {
114 type => DQ_OPERATOR,
d68bc999 115 operator => { Perl => '!' },
c3633a0e 116 args => [ expr { $_->foo } ],
117 },
118 '! ok';