faa229ce760b1e70cf3bfac574c58f2ed529e6e6
[wormy.git] / t / parser.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Data::Dumper;
8
9 plan tests => 7;
10
11 use_ok('Parse::Binary::Nested');
12
13 my @example = (
14         foos => [
15                 'C',
16                 message => 'Z*',
17                 period  => 'a',
18         ],
19         trail => 'a*',
20 );
21 my $testdata = "\2foo\0!\0.rest";
22 my $testresult = {
23         foos => [
24                 {message => 'foo', period => '!'},
25                 {message => '',    period => '.'},
26         ],
27         trail => 'rest',
28 };
29
30 my $parser = Parse::Binary::Nested->new(\@example);
31 ok($parser, 'new object');
32 is_deeply($parser->unpackf($testdata), $testresult, 'object unpackf');
33
34 Parse::Binary::Nested->import('unpackf');
35 is_deeply(
36         unpackf(\@example, $testdata),
37         $testresult,
38         'unprepared unpackf'
39 );
40
41 my @commonargs = ('cxaXv', "\1\2hi\0");
42 is_deeply(
43         [ values %{ unpackf(@commonargs) } ],
44         [[ unpack($commonargs[0], $commonargs[1]) ]],
45         'unpack compatibility'
46 );
47
48 is_deeply(
49         unpackf([ lstr => 'C/a3', rest => 'a*' ], "\2quux"),
50         { lstr => 'qu', rest => 'x' },
51         'length string'
52 );
53
54 is_deeply(
55         unpackf([ ignoreme => 'x2X', value => 'xC' ], "\0\1\2"),
56         { value => 2 },
57         'empty values'
58 );
59