X-Git-Url: http://git.shiar.nl/wormy.git/blobdiff_plain/ce0df5c87882d5275a22ab3bb3f5642c577927b6..f4886cc6c2159afd00025585fcc6ea7aac820391:/t/parser.t diff --git a/t/parser.t b/t/parser.t index 5e53315..faa229c 100644 --- a/t/parser.t +++ b/t/parser.t @@ -6,35 +6,53 @@ use warnings; use Test::More; use Data::Dumper; -plan tests => 6; +plan tests => 7; use_ok('Parse::Binary::Nested'); -my $example = Parse::Binary::Nested->new([ +my @example = ( foos => [ 'C', message => 'Z*', - period => 'C', + period => 'a', ], trail => 'a*', -]); -ok($example, 'example parser'); -my $data = $example->unpackf("\2foo\0!\0.rest"); -is(ref $data, 'HASH', 'output structure'); -is($data->{foos}->[1]->{period}, ord '.', 'sample element'); +); +my $testdata = "\2foo\0!\0.rest"; +my $testresult = { + foos => [ + {message => 'foo', period => '!'}, + {message => '', period => '.'}, + ], + trail => 'rest', +}; + +my $parser = Parse::Binary::Nested->new(\@example); +ok($parser, 'new object'); +is_deeply($parser->unpackf($testdata), $testresult, 'object unpackf'); + +Parse::Binary::Nested->import('unpackf'); +is_deeply( + unpackf(\@example, $testdata), + $testresult, + 'unprepared unpackf' +); + +my @commonargs = ('cxaXv', "\1\2hi\0"); +is_deeply( + [ values %{ unpackf(@commonargs) } ], + [[ unpack($commonargs[0], $commonargs[1]) ]], + 'unpack compatibility' +); is_deeply( - Parse::Binary::Nested->new( - [ lstr => 'Ca3', rest => 'a*' ] - )->unpackf("\2quux"), + unpackf([ lstr => 'C/a3', rest => 'a*' ], "\2quux"), { lstr => 'qu', rest => 'x' }, 'length string' ); is_deeply( - Parse::Binary::Nested->new( - [ ignoreme => 'x2X', value => 'xC' ] - )->unpackf("\0\1\2"), + unpackf([ ignoreme => 'x2X', value => 'xC' ], "\0\1\2"), { value => 2 }, 'empty values' );