File: 07p_args.t

package info (click to toggle)
libsql-translator-perl 0.11011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,380 kB
  • sloc: perl: 251,748; sql: 3,805; xml: 233; makefile: 7
file content (81 lines) | stat: -rw-r--r-- 2,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl
# vim: set ft=perl:
#
#

use strict;

use SQL::Translator;
use Test::More tests => 9;

sub silly_parser {
    my ($tr, $data) = @_;
    my $pargs = $tr->parser_args;

    my @fields = split /$pargs->{'delimiter'}/, $data;

    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
my $data = q(Id|Name|Phone Number|Favorite Flavor|);

my $tr = SQL::Translator->new;

# Pass parser_args as an explicit method call
$tr->parser(\&silly_parser);
$tr->parser_args(delimiter => '\|');

my $pargs  = $tr->parser_args;
$tr->translate(\$data);
my $schema = $tr->schema;

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");

#
# Blow away the existing schema object.
#
$tr->schema (undef);

# Now, pass parser_args indirectly...
$tr->parser(\&silly_parser, { delimiter => "\t" });
$data =~ s/\|/\t/g;

$pargs = $tr->parser_args;
$tr->translate(\$data);

is($pargs->{'delimiter'}, "\t",
    "parser_args works when called indirectly");

@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;
$tr->translate(\$data);

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");