1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4######################### We start with some black magic to print on failure.
5
6# Change 1..1 below to 1..last_test_to_print .
7# (It may become useful if the test is moved to ./t subdirectory.)
8
9BEGIN { $| = 1; print "1..11\n"; }
10END {print "not ok 1\n" unless $loaded;}
11use Parse::FixedLength;
12$loaded = 1;
13print "ok 1\n";
14
15######################### End of black magic.
16my $not = 'not ';
17
18my $parser1 = Parse::FixedLength->new([qw(
19    first_name:10
20    last_name:10
21    widgets_this_month:5R0
22)], {delim=>":"});
23
24print $not unless defined $parser1;
25print "ok 2\n";
26
27my $parser2 = Parse::FixedLength->new([
28    seq_id     => 10,
29    first_name => 10,
30    last_name  => 10,
31    country    =>  3,
32    widgets_this_year => '10R0',
33]);
34
35print $not unless defined $parser2;
36print "ok 3\n";
37
38my $converter1 = $parser1->converter($parser2, {
39    widgets_this_month => widgets_this_year,
40},{
41    seq_id => do { my $cnt = '0' x $parser2->length('seq_id');
42                   sub { ++$cnt };
43                 },
44    widgets_this_year => sub { 12 * shift },
45    country => 'USA',
46}, {no_pack=>1});
47
48print $not unless defined $converter1;
49print "ok 4\n";
50
51my $str_in = 'BOB       JONES        24';
52my $data_out1 = $converter1->convert($str_in);
53print $not unless UNIVERSAL::isa($data_out1, 'HASH');
54print "ok 5\n";
55
56print $not unless $parser2->pack($data_out1)
57    eq '0000000001BOB       JONES     USA0000000288';
58print "ok 6\n";
59
60my $str_out = $converter1->convert($str_in, 0);
61print $not if ref($str_out);
62print "ok 7\n";
63
64print $not unless $str_out eq '0000000002BOB       JONES     USA0000000288';
65print "ok 8\n";
66
67my $converter2 = $parser1->converter($parser2, {
68    widgets_this_month => widgets_this_year,
69},{
70    seq_id => do { my $cnt = '0' x $parser2->length('seq_id');
71                   sub { ++$cnt };
72                 },
73    widgets_this_year => sub { 12 * shift },
74    country => 'USA',
75});
76
77print $not unless defined $converter2;
78print "ok 9\n";
79
80my $data_out2 = $converter2->convert($str_in, 1);
81print $not unless UNIVERSAL::isa($data_out2, 'HASH');
82print "ok 10\n";
83
84print $not unless $parser2->pack($data_out2)
85    eq '0000000001BOB       JONES     USA0000000288';
86print "ok 11\n";
87