add html profile
[dbsrgits/SQL-Abstract.git] / t / 11unparse.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use SQL::Abstract::Tree;
6
7 subtest no_formatting => sub {
8    my $sqlat = SQL::Abstract::Tree->new;
9
10    {
11       my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
12       my $expected_sql =
13          "SELECT a, b, c FROM foo WHERE foo.a = 1 AND foo.b LIKE 'station' ";
14       is($sqlat->format($sql), $expected_sql,
15          'simple statement formatted correctly'
16       );
17    }
18
19    {
20       my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
21       my $expected_sql =
22          "SELECT * FROM (SELECT * FROM foobar ) WHERE foo.a = 1 AND foo.b LIKE 'station' ";
23       is($sqlat->format($sql), $expected_sql,
24          'subquery statement formatted correctly'
25       );
26    }
27
28    {
29       my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
30       my $expected_sql =
31          "SELECT * FROM lolz WHERE (foo.a = 1) AND foo.b LIKE 'station' ";
32
33       is($sqlat->format($sql), $expected_sql,
34          'simple statement with parens in where formatted correctly'
35       );
36    }
37    done_testing;
38 };
39
40 subtest console_monochrome => sub {
41    my $sqlat = SQL::Abstract::Tree->new({
42       profile => 'console_monochrome',
43    });
44
45    {
46       my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
47       my $expected_sql =
48          qq{SELECT a, b, c \n} .
49          qq{  FROM foo \n} .
50          qq{  WHERE foo.a = 1 AND foo.b LIKE 'station' };
51       is($sqlat->format($sql), $expected_sql,
52          'simple statement formatted correctly'
53       );
54    }
55
56    {
57       my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
58       my $expected_sql =
59          qq{SELECT * \n} .
60          qq{  FROM (\n} .
61          qq{    SELECT * \n} .
62          qq{      FROM foobar \n} .
63          qq{  ) \n} .
64          qq{  WHERE foo.a = 1 AND foo.b LIKE 'station' };
65
66       is($sqlat->format($sql), $expected_sql,
67          'subquery statement formatted correctly'
68       );
69    }
70
71    {
72       my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
73       my $expected_sql =
74          qq{SELECT * \n} .
75          qq{  FROM lolz \n} .
76          qq{  WHERE (foo.a = 1) AND foo.b LIKE 'station' };
77
78       is($sqlat->format($sql), $expected_sql,
79          'simple statement with parens in where formatted correctly'
80       );
81    }
82    done_testing;
83 };
84
85 subtest html => sub {
86    my $sqlat = SQL::Abstract::Tree->new({
87       profile => 'html',
88    });
89
90    {
91       my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
92       my $expected_sql =
93          qq{<span class="select">SELECT</span> a, b, c <br />\n} .
94          qq{&nbsp;&nbsp;<span class="from">FROM</span> foo <br />\n} .
95          qq{&nbsp;&nbsp;<span class="where">WHERE</span> foo.a = 1 AND foo.b LIKE 'station' };
96       is($sqlat->format($sql), $expected_sql,
97          'simple statement formatted correctly'
98       );
99    }
100
101    {
102       my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
103       my $expected_sql =
104          qq{<span class="select">SELECT</span> * <br />\n} .
105          qq{&nbsp;&nbsp;<span class="from">FROM</span> (<br />\n} .
106          qq{&nbsp;&nbsp;&nbsp;&nbsp;<span class="select">SELECT</span> * <br />\n} .
107          qq{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="from">FROM</span> foobar <br />\n} .
108          qq{&nbsp;&nbsp;) <br />\n} .
109          qq{&nbsp;&nbsp;<span class="where">WHERE</span> foo.a = 1 AND foo.b LIKE 'station' };
110
111       is($sqlat->format($sql), $expected_sql,
112          'subquery statement formatted correctly'
113       );
114    }
115
116    {
117       my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
118       my $expected_sql =
119          qq{<span class="select">SELECT</span> * <br />\n} .
120          qq{&nbsp;&nbsp;<span class="from">FROM</span> lolz <br />\n} .
121          qq{&nbsp;&nbsp;<span class="where">WHERE</span> (foo.a = 1) AND foo.b LIKE 'station' };
122
123       is($sqlat->format($sql), $expected_sql,
124          'simple statement with parens in where formatted correctly'
125       );
126    }
127    done_testing;
128 };
129
130 subtest configuration => sub {
131    my $sqlat = SQL::Abstract::Tree->new({
132       profile => 'console_monochrome',
133       indent_string => "\t",
134       indent_amount => 1,
135       newline => "\r\n",
136    });
137
138    {
139       my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'";
140       my $expected_sql =
141          qq{SELECT a, b, c \r\n} .
142          qq{\tFROM foo \r\n} .
143          qq{\tWHERE foo.a = 1 AND foo.b LIKE 'station' };
144       is($sqlat->format($sql), $expected_sql,
145          'simple statement formatted correctly'
146       );
147    }
148
149    {
150       my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'";
151       my $expected_sql =
152          qq{SELECT * \r\n} .
153          qq{\tFROM (\r\n} .
154          qq{\t\tSELECT * \r\n} .
155          qq{\t\t\tFROM foobar \r\n} .
156          qq{\t) \r\n} .
157          qq{\tWHERE foo.a = 1 AND foo.b LIKE 'station' };
158
159       is($sqlat->format($sql), $expected_sql,
160          'subquery statement formatted correctly'
161       );
162    }
163
164    {
165       my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'";
166       my $expected_sql =
167          qq{SELECT * \r\n} .
168          qq{\tFROM lolz \r\n} .
169          qq{\tWHERE (foo.a = 1) AND foo.b LIKE 'station' };
170
171       is($sqlat->format($sql), $expected_sql,
172          'simple statement with parens in where formatted correctly'
173       );
174    }
175    done_testing;
176 };
177
178 done_testing;
179 # stuff we want:
180 #    Max Width
181 #    Color coding (html)