Commit | Line | Data |
e2158c40 |
1 | #!/usr/bin/perl |
2 | # vim: set ft=perl: |
3 | # |
4 | # |
5 | |
e2158c40 |
6 | use strict; |
7 | |
8 | use SQL::Translator; |
307d9560 |
9 | use Test::More tests => 9; |
e2158c40 |
10 | |
11 | sub 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 |
27 | my $data = q(Id|Name|Phone Number|Favorite Flavor|); |
28 | |
29 | my $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 |
35 | my $pargs = $tr->parser_args; |
36 | $tr->translate(\$data); |
37 | my $schema = $tr->schema; |
e2158c40 |
38 | |
307d9560 |
39 | is($pargs->{'delimiter'}, '\|', "parser_args works when called directly"); |
40 | my @tables = $schema->get_tables; |
41 | is(scalar @tables, 1, "right number of tables"); |
42 | my $table = shift @tables; |
43 | my @fields = $table->get_fields; |
44 | is(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 |
58 | is($pargs->{'delimiter'}, "\t", |
59 | "parser_args works when called indirectly"); |
e2158c40 |
60 | |
307d9560 |
61 | @tables = $schema->get_tables; |
62 | is(scalar @tables, 1, "right number of tables"); |
63 | $table = shift @tables; |
64 | @fields = $table->get_fields; |
65 | is(scalar @fields, 4, "right number of fields"); |
e2158c40 |
66 | |
67 | undef $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 |
74 | is($pargs->{'delimiter'}, ":", |
75 | "parser_args works when called as constructor arg"); |
e2158c40 |
76 | |
307d9560 |
77 | @tables = $schema->get_tables; |
78 | is(scalar @tables, 1, "right number of tables"); |
79 | $table = shift @tables; |
80 | @fields = $table->get_fields; |
81 | is(scalar @fields, 4, "right number of fields with new delimiter"); |