diff --git a/lib/Protocol/Redis.pm b/lib/Protocol/Redis.pm index 88c1484..a0da55d 100644 --- a/lib/Protocol/Redis.pm +++ b/lib/Protocol/Redis.pm @@ -289,7 +289,7 @@ Yaroslav Korshak (yko) =head1 COPYRIGHT AND LICENSE -Copyright (C) 2011-2019, Sergey Zasenko. +Copyright (C) 2011-2024, Serhii Zasenko. This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10. diff --git a/lib/Protocol/Redis/Test.pm b/lib/Protocol/Redis/Test.pm index d5ca4bb..1ba462f 100644 --- a/lib/Protocol/Redis/Test.pm +++ b/lib/Protocol/Redis/Test.pm @@ -11,7 +11,7 @@ our @EXPORT = qw(protocol_redis_ok); use Test::More; require Carp; -sub protocol_redis_ok($$) { +sub protocol_redis_ok { my ($redis_class, $api_version) = @_; if ($api_version == 1) { @@ -26,28 +26,36 @@ sub _apiv1_ok { my $redis_class = shift; subtest 'Protocol::Redis APIv1 ok' => sub { - plan tests => 43; + plan tests => 44; use_ok $redis_class; - my $redis = new_ok $redis_class, [api => 1]; + _test_version_1($redis_class); - can_ok $redis, 'parse', 'api', 'on_message', 'encode'; + _test_unknown_version($redis_class); + } +} + +sub _test_version_1 { + my $redis_class = shift; + + my $redis = new_ok $redis_class, [api => 1]; + + can_ok $redis, 'parse', 'api', 'on_message', 'encode'; - is $redis->api, 1, '$redis->api'; + is $redis->api, 1, '$redis->api'; - # Parsing method tests - $redis->on_message(undef); - _parse_string_ok($redis); - _parse_bulk_ok($redis); - _parse_multi_bulk_ok($redis); + # Parsing method tests + $redis->on_message(undef); + _parse_string_ok($redis); + _parse_bulk_ok($redis); + _parse_multi_bulk_ok($redis); - # on_message works - _on_message_ok($redis); + # on_message works + _on_message_ok($redis); - # Encoding method tests - _encode_ok($redis); - } + # Encoding method tests + _encode_ok($redis); } sub _parse_string_ok { @@ -117,8 +125,7 @@ sub _parse_bulk_ok { $redis->parse("\$-1\r\n"); my $message = $redis->get_message; - ok defined($message) && !defined($message->{data}), - 'nil bulk message'; + ok defined($message) && !defined($message->{data}), 'nil bulk message'; # Two chunked bulk messages $redis->parse(join("\r\n", '$4', 'test', '+OK')); @@ -149,8 +156,8 @@ sub _parse_multi_bulk_ok { $redis->parse("\$5\r\ntest2\r\n"); $redis->parse("\$5\r\ntest3\r\n"); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [ {type => '$', data => 'test1'}, {type => '$', data => 'test2'}, @@ -177,8 +184,8 @@ sub _parse_multi_bulk_ok { # Multi bulk message with status items $redis->parse(join("\r\n", ('*2', '+OK', '$4', 'test'), '')); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [{type => '+', data => 'OK'}, {type => '$', data => 'test'}] }; @@ -203,7 +210,7 @@ sub _parse_multi_bulk_ok { {type => '$', data => 'test2'}, {type => '$', data => 'test3'} ] - }; + }; # Complex string $redis->parse("\*4\r\n"); @@ -217,11 +224,11 @@ sub _parse_multi_bulk_ok { {type => ':', data => 42}, {type => '+', data => 'test3'} ] - }; + }; is_deeply $redis->get_message, { type => '$', data => '12345', - }; + }; # pipelined multi-bulk $redis->parse( @@ -230,8 +237,8 @@ sub _parse_multi_bulk_ok { ('*1', '$3', 'ok3'), '') ); - is_deeply $redis->get_message, - { type => '*', + is_deeply $redis->get_message, { + type => '*', data => [{type => '$', data => 'ok1'}, {type => '$', data => 'ok2'}] }; is_deeply $redis->get_message, @@ -292,8 +299,8 @@ sub _encode_ok { join("\r\n", ('*1', '$4', 'test'), ''), 'encode multi-bulk'; - is $redis->encode( - { type => '*', + is $redis->encode({ + type => '*', data => [ {type => '$', data => 'test1'}, {type => '$', data => 'test2'} ] @@ -308,8 +315,8 @@ sub _encode_ok { is $redis->encode({type => '*', data => undef}), "\*-1\r\n", 'encode nil multi-bulk'; - is $redis->encode( - { type => '*', + is $redis->encode({ + type => '*', data => [ {type => '$', data => 'foo'}, {type => '$', data => undef}, @@ -321,6 +328,13 @@ sub _encode_ok { 'encode multi-bulk with nil element'; } +sub _test_unknown_version { + my $redis_class = shift; + + eval { new($redis_class, api => 0); }; + ok($@, 'unknown version raises an exception'); +} + 1; __END__ @@ -354,7 +368,7 @@ L =head1 COPYRIGHT AND LICENSE -Copyright (C) 2010-2011, Sergey Zasenko +Copyright (C) 2010-2024, Serhii Zasenko This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.