Release commit for 1.62
[dbsrgits/SQL-Translator.git] / t / 07p_args.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3 #
4 #
5
6 use strict;
7
8 use SQL::Translator;
9 use Test::More tests => 9;
10
11 sub silly_parser {
12     my ($tr, $data) = @_;
13     my $pargs = $tr->parser_args;
14
15     my @fields = split /$pargs->{'delimiter'}/, $data;
16
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;
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
35 my $pargs  = $tr->parser_args;
36 $tr->translate(\$data);
37 my $schema = $tr->schema;
38
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->reset;
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;
56 $tr->translate(\$data);
57
58 is($pargs->{'delimiter'}, "\t",
59     "parser_args works when called indirectly");
60
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");
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;
72 $tr->translate(\$data);
73
74 is($pargs->{'delimiter'}, ":",
75     "parser_args works when called as constructor arg");
76
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");