Release 0.09005
[dbsrgits/SQL-Translator.git] / t / 07p_args.t
CommitLineData
e2158c40 1#!/usr/bin/perl
2# vim: set ft=perl:
3#
4#
5
e2158c40 6use strict;
7
8use SQL::Translator;
307d9560 9use Test::More tests => 9;
e2158c40 10
11sub silly_parser {
12 my ($tr, $data) = @_;
13 my $pargs = $tr->parser_args;
14
15 my @fields = split /$pargs->{'delimiter'}/, $data;
16
307d9560 17 my $schema = $tr->schema;
18 my $table = $schema->add_table( name => 'foo') or die $schema->error;
19 for my $value ( @fields ) {
20 my $field = $table->add_field( name => $value ) or die $table->error;
21 }
22
23 return 1;
e2158c40 24}
25
26# The "data" to be parsed
27my $data = q(Id|Name|Phone Number|Favorite Flavor|);
28
29my $tr = SQL::Translator->new;
30
31# Pass parser_args as an explicit method call
32$tr->parser(\&silly_parser);
33$tr->parser_args(delimiter => '\|');
34
307d9560 35my $pargs = $tr->parser_args;
36$tr->translate(\$data);
37my $schema = $tr->schema;
e2158c40 38
307d9560 39is($pargs->{'delimiter'}, '\|', "parser_args works when called directly");
40my @tables = $schema->get_tables;
41is(scalar @tables, 1, "right number of tables");
42my $table = shift @tables;
43my @fields = $table->get_fields;
44is(scalar @fields, 4, "right number of fields");
45
46#
47# Blow away the existing schema object.
48#
49$tr->{'schema'} = undef;
e2158c40 50
51# Now, pass parser_args indirectly...
52$tr->parser(\&silly_parser, { delimiter => "\t" });
53$data =~ s/\|/\t/g;
54
55$pargs = $tr->parser_args;
307d9560 56$tr->translate(\$data);
e2158c40 57
44fb27ae 58is($pargs->{'delimiter'}, "\t",
59 "parser_args works when called indirectly");
e2158c40 60
307d9560 61@tables = $schema->get_tables;
62is(scalar @tables, 1, "right number of tables");
63$table = shift @tables;
64@fields = $table->get_fields;
65is(scalar @fields, 4, "right number of fields");
e2158c40 66
67undef $tr;
68$tr = SQL::Translator->new(parser => \&silly_parser,
69 parser_args => { delimiter => ":" });
70$data =~ s/\t/:/g;
71$pargs = $tr->parser_args;
307d9560 72$tr->translate(\$data);
e2158c40 73
44fb27ae 74is($pargs->{'delimiter'}, ":",
75 "parser_args works when called as constructor arg");
e2158c40 76
307d9560 77@tables = $schema->get_tables;
78is(scalar @tables, 1, "right number of tables");
79$table = shift @tables;
80@fields = $table->get_fields;
81is(scalar @fields, 4, "right number of fields with new delimiter");