From: Ash Berlin Date: Sat, 4 Apr 2009 21:52:05 +0000 (+0100) Subject: Refactor test code a bit X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=96bbece0b664b9225ef1f94a5224f5d5bcee4207;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Refactor test code a bit --- diff --git a/Makefile.PL b/Makefile.PL index 810d684..af625b4 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -12,6 +12,7 @@ requires 'Devel::PartialDump' => '0.9'; test_requires 'Test::More'; test_requires 'Test::Differences'; +test_requires 'Sub::Exporter'; auto_provides_class(); WriteAll; diff --git a/t/compat/ast/01.t b/t/compat/ast/01.t index e42e20e..fe1b384 100644 --- a/t/compat/ast/01.t +++ b/t/compat/ast/01.t @@ -3,7 +3,12 @@ use warnings; use FindBin; use lib "$FindBin::Bin/../../lib"; -use SQLADumperSort; +use Test::SQL::Abstract::Util qw/ + mk_name + mk_value + field_op_value + :dumper_sort +/; use SQL::Abstract::Compat; @@ -324,30 +329,6 @@ eq_or_diff }, "Complex AST [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] }, q => {'not in' => [10,11] } } ]"; -sub field_op_value { - my ($field, $op, $value) = @_; - - $field = ref $field eq 'HASH' - ? $field - : ref $field eq 'ARRAY' - ? { -type => 'identifier', elements => $field } - : { -type => 'identifier', elements => [$field] }; - - my @value = ref $value eq 'HASH' - ? $value - : ref $value eq 'ARRAY' - ? @$value - : { -type => 'value', value => $value }; - - return { - -type => 'expr', - op => $op, - args => [ - $field, - @value - ] - }; -} sub upper { expr(UPPER => @_) } @@ -361,16 +342,3 @@ sub expr { }; } -sub mk_name { - my ($field) = @_; - $field = ref $field eq 'HASH' - ? $field - : ref $field eq 'ARRAY' - ? { -type => 'identifier', elements => $field } - : { -type => 'identifier', elements => [$field] }; - return $field; -} - -sub mk_value { - return { -type => 'value', value => $_[0] } -} diff --git a/t/lib/SQLADumperSort.pm b/t/lib/SQLADumperSort.pm deleted file mode 100644 index 52e6e3f..0000000 --- a/t/lib/SQLADumperSort.pm +++ /dev/null @@ -1,27 +0,0 @@ -BEGIN { - require Data::Dumper; - my $Dump = Data::Dumper->can('Dump'); - - no warnings 'redefine'; - - *Data::Dumper::Dump = sub { - local $Data::Dumper::Sortkeys = sub { - my $hash = @_[0]; - my @keys = sort { - my $a_minus = substr($a,0,1) eq '-'; - my $b_minus = substr($b,0,1) eq '-'; - - return $a cmp $b if $a_minus || $b_minus; - - return -1 if $a eq 'op'; - return 1 if $b eq 'op'; - return $a cmp $b; - } keys %$hash; - - return \@keys; - }; - return $Dump->(@_); - }; -} - -1; diff --git a/t/lib/Test/SQL/Abstract/Util.pm b/t/lib/Test/SQL/Abstract/Util.pm new file mode 100644 index 0000000..4455c29 --- /dev/null +++ b/t/lib/Test/SQL/Abstract/Util.pm @@ -0,0 +1,90 @@ +package Test::SQL::Abstract::Util; + +use strict; +use warnings; + +use Sub::Exporter -setup => { + exports => [qw/ + mk_name + mk_value + mk_alias + field_op_value + /], + groups => [ + dumper_sort => sub { + + require Data::Dumper; + my $Dump = Data::Dumper->can('Dump'); + + no warnings 'redefine'; + + *Data::Dumper::Dump = sub { + local $Data::Dumper::Sortkeys = sub { + my $hash = $_[0]; + my @keys = sort { + my $a_minus = substr($a,0,1) eq '-'; + my $b_minus = substr($b,0,1) eq '-'; + + return $a cmp $b if $a_minus || $b_minus; + + return -1 if $a eq 'op'; + return 1 if $b eq 'op'; + return $a cmp $b; + } keys %$hash; + + return \@keys; + }; + return $Dump->(@_); + }; + return {}; + } + ], +}; + +sub mk_alias { + return { + -type => 'alias', + ident => shift, + as => shift, + }; +} + +sub mk_name { + my ($field) = shift; + $field = ref $field eq 'HASH' + ? $field + : ref $field eq 'ARRAY' + ? { -type => 'identifier', elements => $field } + : { -type => 'identifier', elements => [$field,@_] }; + return $field; +} + +sub mk_value { + return { -type => 'value', value => $_[0] } +} + + +sub field_op_value { + my ($field, $op, $value) = @_; + + $field = ref $field eq 'HASH' + ? $field + : mk_name($field); + + my @value = ref $value eq 'HASH' + ? $value + : ref $value eq 'ARRAY' + ? @$value + : mk_value($value); + + return { + -type => 'expr', + op => $op, + args => [ + $field, + @value + ] + }; +} + +1;