/
10basic.t
206 lines (186 loc) · 7.01 KB
/
10basic.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use strict;
use warnings;
use autodie;
use Data::FlexSerializer;
use Storable qw/nfreeze thaw/;
use Compress::Zlib qw(Z_DEFAULT_COMPRESSION);
use JSON::XS qw/encode_json decode_json/;
use Test::More;
use constant DF => 'Data::FlexSerializer';
# Test default settings
my $default_szer = DF->new();
isa_ok($default_szer, 'Data::FlexSerializer');
my %defaults = (
assume_compression => 1,
detect_compression => 0,
compress_output => 1,
compression_level => undef,
detect_storable => 0,
output_format => 'json',
);
foreach my $setting (sort keys %defaults) {
is($default_szer->$setting, $defaults{$setting}, "defaults for $setting");
}
# check whether assume_compression is turned off implicitly if detect_compression is set
SCOPE: {
my %opt = %defaults;
delete $opt{assume_compression};
$opt{detect_compression} = 1;
my $s = DF->new(%opt);
ok(!$s->assume_compression, "detect_compression implies assume_compression==0");
}
my %opt_no_compress = (
assume_compression => 0,
detect_compression => 0,
compress_output => 0,
);
my %opt_accept_compress = (
assume_compression => 0,
detect_compression => 1,
compress_output => 0,
);
my %opt_flex_in = (
%opt_accept_compress,
detect_storable => 1,
);
my %opt_storable = (
output_format => 'storable',
detect_storable => 1,
);
my %serializers = (
default => $default_szer,
json_compress => $default_szer,
json_no_compress => DF->new(%opt_no_compress),
json_flex_compress => DF->new(%opt_accept_compress, compress_output => 1),
flex_compress => DF->new(detect_storable => 1, compress_output => 1),
flex_no_compress => DF->new(%opt_flex_in, %opt_no_compress),
flex_flex_compress => DF->new(%opt_flex_in),
s_compress => DF->new(%opt_storable),
s_no_compress => DF->new(%opt_storable, %opt_no_compress),
s_flex_compress => DF->new(%opt_storable, %opt_accept_compress, compress_output => 1),
);
isa_ok($serializers{$_}, 'Data::FlexSerializer', 'Serializer for "$_"') for sort keys %serializers;
my %data = (
raw => {foo => 'bar', baz => [2, 3, 4]},
garbage => 'asdkj2qdal2djalkd',
);
$data{storable} = nfreeze($data{raw});
$data{json} = encode_json($data{raw});
$data{comp_json} = Compress::Zlib::compress(\$data{json}, Z_DEFAULT_COMPRESSION);
$data{comp_storable} = Compress::Zlib::compress(\$data{storable}, Z_DEFAULT_COMPRESSION);
$data{comp_garbage} = Compress::Zlib::compress(\$data{garbage}, Z_DEFAULT_COMPRESSION); # dubious
# assert that serialize and deserialize die if called in scalar context and
# passed multiple args:
ok(
not(eval {my $x = $serializers{flex_flex_compress}->serialize([], {}); 1}),
"serialize dies if called in scalar context with multiple arguments"
);
ok(
not(eval {my $x = $serializers{flex_flex_compress}->deserialize(@data{qw(json storable)}); 1}),
"deserialize dies if called in scalar context with multiple arguments"
);
# input is implied to be the raw data on serialization, output listed
my %results_serialize = (
'default' => 'comp_json',
'json_compress' => 'comp_json',
'json_no_compress' => 'json',
'json_flex_compress' => 'comp_json',
'flex_compress' => 'comp_json',
'flex_no_compress' => 'json',
'flex_flex_compress' => 'json',
's_compress' => 'comp_storable',
's_no_compress' => 'storable',
's_flex_compress' => 'comp_storable',
);
foreach my $s_name (sort keys %results_serialize) {
my $serializer = $serializers{$s_name} or die;
my $data = $data{raw}; # always raw input for serialization
my $tname = "serialization with $s_name";
my $res;
my @res;
eval {$res = $serializer->serialize($data); 1} && defined $res
? pass("$tname in scalar context does not crash and returns non-undef")
: fail("$tname in scalar context throws exception or results in undef");
eval {@res = $serializer->serialize($data); 1} && @res == 1 && defined($res[0])
? pass("$tname in array context does not crash and returns non-undef")
: fail("$tname in array context throws exception or results in undef");
is($res, $res[0], "$tname same result in array and scalar context");
# test actual output
my $expected_output = $data{ $results_serialize{$s_name} } or die;
is($res, $expected_output, "$tname output as expected");
# now assert that garbage throws exceptions
ok(not eval {$res = $serializer->serialize($data{garbage}); 1});
}
# maps input => expected output for each serializer.
# \undef output means exception
my %results_deserialize = (
'default' => {
'comp_json' => 'raw',
(map {$_ => \undef} qw(json storable comp_storable garbage)),
},
'json_compress' => {
'comp_json' => 'raw',
(map {$_ => \undef} qw(json storable comp_storable garbage)),
},
'json_no_compress' => {
'json' => 'raw',
(map {$_ => \undef} qw(comp_json storable comp_storable garbage)),
},
'json_flex_compress' => {
(map {$_ => 'raw'} qw(comp_json json)),
(map {$_ => \undef} qw(storable comp_storable garbage)),
},
'flex_compress' => {
(map {$_ => 'raw'} qw(comp_json comp_storable)),
(map {$_ => \undef} qw(storable json garbage)),
},
'flex_no_compress' => {
(map {$_ => 'raw'} qw(json storable)),
(map {$_ => \undef} qw(comp_json comp_storable garbage)),
},
'flex_flex_compress' => {
(map {$_ => 'raw'} qw(json comp_json storable comp_storable)),
(map {$_ => \undef} qw(garbage)),
},
's_compress' => {
(map {$_ => 'raw'} qw(comp_json comp_storable)),
(map {$_ => \undef} qw(json storable garbage)),
},
's_no_compress' => {
(map {$_ => 'raw'} qw(json storable)),
(map {$_ => \undef} qw(comp_json comp_storable garbage)),
},
's_flex_compress' => {
(map {$_ => 'raw'} qw(comp_json comp_storable json storable)),
(map {$_ => \undef} qw(garbage)),
},
);
foreach my $s_name (sort keys %results_deserialize) {
my $serializer = $serializers{$s_name} or die;
my $testset = $results_deserialize{$s_name};
foreach my $input_data_name (sort keys %$testset) {
my $exp_out = $testset->{$input_data_name};
my $input_data = $data{$input_data_name};
my $tname = "deserialization with $s_name for input '$input_data_name'";
my $res;
my @res;
if (ref $exp_out && not defined $$exp_out) { # exception expected
ok(not eval {$res = $serializer->deserialize($input_data); 1} );
ok(not eval {@res = $serializer->deserialize($input_data); 1} );
}
else {
$exp_out = $data{$exp_out};
eval {$res = $serializer->deserialize($input_data); 1} && defined $res
? pass("$tname in scalar context does not crash and returns non-undef")
: fail("$tname in scalar context throws exception or results in undef");
is_deeply($res, $exp_out, "$tname in scalar context yields correct result");
eval {@res = $serializer->deserialize($input_data); 1} && @res == 1 && defined($res[0])
? pass("$tname in array context does not crash and returns non-undef")
: fail("$tname in array context throws exception or results in undef");
is_deeply($res[0], $exp_out, "$tname in array context yields correct result");
}
}
}
# TODO test file read/write logic
# TODO test debugging facilities
done_testing();