Skip to content

Commit

Permalink
Merge pull request #17 from und3f/test-unknown-version
Browse files Browse the repository at this point in the history
Add unknown version test to the testing suite
  • Loading branch information
und3f committed Sep 2, 2024
2 parents bd50849 + 864517d commit f1a11df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/Protocol/Redis.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
76 changes: 45 additions & 31 deletions lib/Protocol/Redis/Test.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'},
Expand All @@ -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'}]
};

Expand All @@ -203,7 +210,7 @@ sub _parse_multi_bulk_ok {
{type => '$', data => 'test2'},
{type => '$', data => 'test3'}
]
};
};

# Complex string
$redis->parse("\*4\r\n");
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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'}
]
Expand All @@ -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},
Expand All @@ -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__
Expand Down Expand Up @@ -354,7 +368,7 @@ L<Protocol::Redis>
=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.
Expand Down

0 comments on commit f1a11df

Please sign in to comment.