summaryrefslogtreecommitdiffstats
path: root/t/netstring.t
diff options
context:
space:
mode:
Diffstat (limited to 't/netstring.t')
-rw-r--r--t/netstring.t44
1 files changed, 44 insertions, 0 deletions
diff --git a/t/netstring.t b/t/netstring.t
new file mode 100644
index 0000000..de0b61e
--- /dev/null
+++ b/t/netstring.t
@@ -0,0 +1,44 @@
+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: