use strict; use warnings; use Test::More tests => 11; use Test::Exception; BEGIN { use_ok 'Spline::Socketmap', qw( netstring_read netstring_write ) or BAIL_OUT; } sub output($) { my $data = shift; my $output; open(my $fh, '>', \$output); netstring_write($fh, $data); return $output; } sub input($) { my $data = shift; open(my $fh, '<', \$data); return netstring_read($fh); } # Netstring output is(output('test'), '4:test,', 'Netstring output'); is(output(''), '0:,', 'Empty netstring output'); is(output('. '), '2:. ,', 'Preserve spaces'); # Netstring input is(input('4:test,'), 'test', 'Basic netstring input'); is(input('0:,'), '', 'Empty netstring input'); throws_ok { input('4:test') } qr/, missing/, 'Missing final ,'; throws_ok { input('5:test') } qr/4 of 5 bytes/, 'Data too short'; throws_ok { input('4test') } qr/Invalid length/, 'Format error'; throws_ok { input('') } qr/Cannot read .* length/, 'Empty input'; throws_ok { netstring_read('') } qr/Filehandle required/, 'No filehandle'; # vim: set et ts=4: