#
#
-BEGIN { print "1..6\n"; }
-
use strict;
use SQL::Translator;
-$SQL::Translator::DEBUG = 0;
+use Test::More tests => 9;
sub silly_parser {
my ($tr, $data) = @_;
my @fields = split /$pargs->{'delimiter'}/, $data;
- return \@fields;
+ my $schema = $tr->schema;
+ my $table = $schema->add_table( name => 'foo') or die $schema->error;
+ for my $value ( @fields ) {
+ my $field = $table->add_field( name => $value ) or die $table->error;
+ }
+
+ return 1;
}
# The "data" to be parsed
$tr->parser(\&silly_parser);
$tr->parser_args(delimiter => '\|');
-my $pargs = $tr->parser_args;
-my $parsed = $tr->translate(\$data);
+my $pargs = $tr->parser_args;
+$tr->translate(\$data);
+my $schema = $tr->schema;
-print "not " unless ($pargs->{'delimiter'} eq '\|');
-print "ok 1 # parser_args works when called directly\n";
+is($pargs->{'delimiter'}, '\|', "parser_args works when called directly");
+my @tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+my $table = shift @tables;
+my @fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields");
-print "not " unless (scalar @{$parsed} == 4);
-print "ok 2 # right number of fields\n";
+#
+# Blow away the existing schema object.
+#
+$tr->reset;
# Now, pass parser_args indirectly...
$tr->parser(\&silly_parser, { delimiter => "\t" });
$data =~ s/\|/\t/g;
$pargs = $tr->parser_args;
-$parsed = $tr->translate(\$data);
+$tr->translate(\$data);
-print "not " unless ($pargs->{'delimiter'} eq "\t");
-print "ok 3 # parser_args works when called indirectly\n";
+is($pargs->{'delimiter'}, "\t",
+ "parser_args works when called indirectly");
-print "not " unless (scalar @{$parsed} == 4);
-print "ok 4 # right number of fields with new delimiter\n";
+@tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+$table = shift @tables;
+@fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields");
undef $tr;
$tr = SQL::Translator->new(parser => \&silly_parser,
parser_args => { delimiter => ":" });
$data =~ s/\t/:/g;
$pargs = $tr->parser_args;
-$parsed = $tr->translate(\$data);
-
-print "not " unless ($pargs->{'delimiter'} eq ":");
-print "ok 5 # parser_args works when called as constructor arg\n";
+$tr->translate(\$data);
-print "not " unless (scalar @{$parsed} == 4);
-print "ok 6 # right number of fields with new delimiter\n";
+is($pargs->{'delimiter'}, ":",
+ "parser_args works when called as constructor arg");
+@tables = $schema->get_tables;
+is(scalar @tables, 1, "right number of tables");
+$table = shift @tables;
+@fields = $table->get_fields;
+is(scalar @fields, 4, "right number of fields with new delimiter");