From: Arthur Axel "fREW" Schmidt Date: Sat, 4 Sep 2010 01:12:46 +0000 (+0000) Subject: tests and slightly better profiles X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FQ-Branch.git;a=commitdiff_plain;h=3be357b0bb2c31c729e14e3244428372810a4263 tests and slightly better profiles --- diff --git a/lib/SQL/Abstract/Tree.pm b/lib/SQL/Abstract/Tree.pm index d79f787..1c04069 100644 --- a/lib/SQL/Abstract/Tree.pm +++ b/lib/SQL/Abstract/Tree.pm @@ -5,7 +5,6 @@ use warnings; use Carp; -use Term::ANSIColor 'color'; use base 'Class::Accessor::Grouped'; __PACKAGE__->mk_group_accessors( simple => $_ ) for qw( @@ -83,13 +82,20 @@ my %profiles = ( indent_string => ' ', indent_amount => 2, newline => "\n", - colormap => { - select => [color('red'), color('reset')], - where => [color('green'), color('reset')], - from => [color('cyan'), color('reset')], + colormap => {}, + indentmap => { + select => 0, + where => 1, + from => 1, }, - indentmap => { - select => 0, + }, + console_monochrome => { + indent_string => ' ', + indent_amount => 2, + newline => "\n", + colormap => {}, + indentmap => { + select => 0, where => 1, from => 1, }, @@ -100,6 +106,15 @@ my %profiles = ( }, ); +eval { + require Term::ANSIColor; + $profiles{console}->{colormap} = { + select => [Term::ANSIColor::color('red'), Term::ANSIColor::color('reset')], + where => [Term::ANSIColor::color('green'), Term::ANSIColor::color('reset')], + from => [Term::ANSIColor::color('cyan'), Term::ANSIColor::color('reset')], + }; +}; + sub new { my ($class, $args) = @_; @@ -270,3 +285,15 @@ sub format { my $self = shift; $self->unparse($self->parse(@_)) } 1; +=pod + +=head1 SYNOPSIS + + my $sqla_tree = SQL::Abstract::Tree->new({ profile => 'console' }); + + print $sqla_tree->format('SELECT * FROM foo WHERE foo.a > 2'); + + # SELECT * + # FROM foo + # WHERE foo.a > 2 + diff --git a/t/11unparse.t b/t/11unparse.t index 5ed9f5f..e9f9623 100644 --- a/t/11unparse.t +++ b/t/11unparse.t @@ -1,31 +1,52 @@ use strict; use warnings; +use Test::More; use SQL::Abstract::Tree; -my $sqlat = SQL::Abstract::Tree->new({}); +my $sqlat = SQL::Abstract::Tree->new({ + profile => 'console_monochrome', +}); { my $sql = "SELECT a, b, c FROM foo WHERE foo.a =1 and foo.b LIKE 'station'"; - - print "$sql\n"; - print $sqlat->format($sql) . "\n"; + my $expected_sql = + qq{SELECT a, b, c \n} . + qq{ FROM foo \n} . + qq{ WHERE foo.a = 1 AND foo.b LIKE 'station' }; + is($sqlat->format($sql), $expected_sql, + 'simple statement formatted correctly' + ); } { my $sql = "SELECT * FROM (SELECT * FROM foobar) WHERE foo.a =1 and foo.b LIKE 'station'"; - - print "$sql\n"; - print $sqlat->format($sql) . "\n"; + my $expected_sql = + qq{SELECT * \n} . + qq{ FROM (\n} . + qq{ SELECT * \n} . + qq{ FROM foobar \n} . + qq{ ) \n} . + qq{ WHERE foo.a = 1 AND foo.b LIKE 'station' }; + + is($sqlat->format($sql), $expected_sql, + 'subquery statement formatted correctly' + ); } { my $sql = "SELECT * FROM lolz WHERE ( foo.a =1 ) and foo.b LIKE 'station'"; - - print "$sql\n"; - print $sqlat->format($sql) . "\n"; + my $expected_sql = + qq{SELECT * \n} . + qq{ FROM lolz \n} . + qq{ WHERE (foo.a = 1) AND foo.b LIKE 'station' }; + + is($sqlat->format($sql), $expected_sql, + 'simple statement with parens in where formatted correctly' + ); } +done_testing; # stuff we want: # Nested indentation # Max Width