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