@@ -11,7 +11,7 @@ our @EXPORT = qw(protocol_redis_ok);
11
11
use Test::More;
12
12
require Carp;
13
13
14
- sub protocol_redis_ok ($$) {
14
+ sub protocol_redis_ok {
15
15
my ($redis_class , $api_version ) = @_ ;
16
16
17
17
if ($api_version == 1) {
@@ -26,28 +26,36 @@ sub _apiv1_ok {
26
26
my $redis_class = shift ;
27
27
28
28
subtest ' Protocol::Redis APIv1 ok' => sub {
29
- plan tests => 43 ;
29
+ plan tests => 44 ;
30
30
31
31
use_ok $redis_class ;
32
32
33
- my $redis = new_ok $ redis_class, [ api => 1] ;
33
+ _test_version_1( $ redis_class) ;
34
34
35
- can_ok $redis , ' parse' , ' api' , ' on_message' , ' encode' ;
35
+ _test_unknown_version($redis_class );
36
+ }
37
+ }
38
+
39
+ sub _test_version_1 {
40
+ my $redis_class = shift ;
41
+
42
+ my $redis = new_ok $redis_class , [api => 1];
43
+
44
+ can_ok $redis , ' parse' , ' api' , ' on_message' , ' encode' ;
36
45
37
- is $redis -> api, 1, ' $redis->api' ;
46
+ is $redis -> api, 1, ' $redis->api' ;
38
47
39
- # Parsing method tests
40
- $redis -> on_message(undef );
41
- _parse_string_ok($redis );
42
- _parse_bulk_ok($redis );
43
- _parse_multi_bulk_ok($redis );
48
+ # Parsing method tests
49
+ $redis -> on_message(undef );
50
+ _parse_string_ok($redis );
51
+ _parse_bulk_ok($redis );
52
+ _parse_multi_bulk_ok($redis );
44
53
45
- # on_message works
46
- _on_message_ok($redis );
54
+ # on_message works
55
+ _on_message_ok($redis );
47
56
48
- # Encoding method tests
49
- _encode_ok($redis );
50
- }
57
+ # Encoding method tests
58
+ _encode_ok($redis );
51
59
}
52
60
53
61
sub _parse_string_ok {
@@ -117,8 +125,7 @@ sub _parse_bulk_ok {
117
125
$redis -> parse(" \$ -1\r\n " );
118
126
119
127
my $message = $redis -> get_message;
120
- ok defined ($message ) && !defined ($message -> {data }),
121
- ' nil bulk message' ;
128
+ ok defined ($message ) && !defined ($message -> {data }), ' nil bulk message' ;
122
129
123
130
# Two chunked bulk messages
124
131
$redis -> parse(join (" \r\n " , ' $4' , ' test' , ' +OK' ));
@@ -149,8 +156,8 @@ sub _parse_multi_bulk_ok {
149
156
$redis -> parse(" \$ 5\r\n test2\r\n " );
150
157
$redis -> parse(" \$ 5\r\n test3\r\n " );
151
158
152
- is_deeply $redis -> get_message,
153
- { type => ' *' ,
159
+ is_deeply $redis -> get_message, {
160
+ type => ' *' ,
154
161
data => [
155
162
{type => ' $' , data => ' test1' },
156
163
{type => ' $' , data => ' test2' },
@@ -177,8 +184,8 @@ sub _parse_multi_bulk_ok {
177
184
178
185
# Multi bulk message with status items
179
186
$redis -> parse(join (" \r\n " , (' *2' , ' +OK' , ' $4' , ' test' ), ' ' ));
180
- is_deeply $redis -> get_message,
181
- { type => ' *' ,
187
+ is_deeply $redis -> get_message, {
188
+ type => ' *' ,
182
189
data => [{type => ' +' , data => ' OK' }, {type => ' $' , data => ' test' }]
183
190
};
184
191
@@ -203,7 +210,7 @@ sub _parse_multi_bulk_ok {
203
210
{type => ' $' , data => ' test2' },
204
211
{type => ' $' , data => ' test3' }
205
212
]
206
- };
213
+ };
207
214
208
215
# Complex string
209
216
$redis -> parse(" \* 4\r\n " );
@@ -217,11 +224,11 @@ sub _parse_multi_bulk_ok {
217
224
{type => ' :' , data => 42},
218
225
{type => ' +' , data => ' test3' }
219
226
]
220
- };
227
+ };
221
228
is_deeply $redis -> get_message, {
222
229
type => ' $' ,
223
230
data => ' 12345' ,
224
- };
231
+ };
225
232
226
233
# pipelined multi-bulk
227
234
$redis -> parse(
@@ -230,8 +237,8 @@ sub _parse_multi_bulk_ok {
230
237
(' *1' , ' $3' , ' ok3' ), ' ' )
231
238
);
232
239
233
- is_deeply $redis -> get_message,
234
- { type => ' *' ,
240
+ is_deeply $redis -> get_message, {
241
+ type => ' *' ,
235
242
data => [{type => ' $' , data => ' ok1' }, {type => ' $' , data => ' ok2' }]
236
243
};
237
244
is_deeply $redis -> get_message,
@@ -292,8 +299,8 @@ sub _encode_ok {
292
299
join (" \r\n " , (' *1' , ' $4' , ' test' ), ' ' ),
293
300
' encode multi-bulk' ;
294
301
295
- is $redis -> encode(
296
- { type => ' *' ,
302
+ is $redis -> encode({
303
+ type => ' *' ,
297
304
data => [
298
305
{type => ' $' , data => ' test1' }, {type => ' $' , data => ' test2' }
299
306
]
@@ -308,8 +315,8 @@ sub _encode_ok {
308
315
is $redis -> encode({type => ' *' , data => undef }), " \* -1\r\n " ,
309
316
' encode nil multi-bulk' ;
310
317
311
- is $redis -> encode(
312
- { type => ' *' ,
318
+ is $redis -> encode({
319
+ type => ' *' ,
313
320
data => [
314
321
{type => ' $' , data => ' foo' },
315
322
{type => ' $' , data => undef },
@@ -321,6 +328,13 @@ sub _encode_ok {
321
328
' encode multi-bulk with nil element' ;
322
329
}
323
330
331
+ sub _test_unknown_version {
332
+ my $redis_class = shift ;
333
+
334
+ eval { new($redis_class , api => 0); };
335
+ ok($@ , ' unknown version raises an exception' );
336
+ }
337
+
324
338
1;
325
339
__END__
326
340
@@ -354,7 +368,7 @@ L<Protocol::Redis>
354
368
355
369
=head1 COPYRIGHT AND LICENSE
356
370
357
- Copyright (C) 2010-2011, Sergey Zasenko
371
+ Copyright (C) 2010-2024, Serhii Zasenko
358
372
359
373
This program is free software, you can redistribute it and/or modify it under
360
374
the terms of the Artistic License version 2.0.
0 commit comments