Scenario/Serializer/JSON.pm untested

filter: Statementstmt 0.0% 0 / 26 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 42 CC 4 SCAR 17.6
CC
4
Cov
0%
decode_scenario
12.0
encode_scenario
6.0
new
2.0
CRAP 5.8
1 package Test::HTTP::Scenario::Serializer::JSON;
2  
3 use strict;
4 use warnings;
5 use Carp qw(croak carp);
6 use JSON::MaybeXS ();
7  
8 #----------------------------------------------------------------------#
9 # Constructor
10 #----------------------------------------------------------------------#
11  
12 sub new {
13 my ($class) = @_;
14  
15 # Entry: class name
16 # Exit: new serializer object
17 # Side effects: none
18 # Notes: holds a JSON encoder and decoder
19  
20 my $json = JSON::MaybeXS->new(
21 utf8 => 0,
22 pretty => 1,
23 canonical => 1,
24 allow_nonref => 0,
25 );
26  
27 my $self = bless {
28 json => $json,
29 }, $class;
30  
31 return $self;
32 }
33  
34 #----------------------------------------------------------------------#
35 # Encoding
36 #----------------------------------------------------------------------#
37  
38 sub encode_scenario {
39 my ($self, $data) = @_;
40  
41 # Entry: hashref representing a scenario
42 # Exit: JSON string
43 # Side effects: none
44 # Notes: canonical output for deterministic fixtures
45  
46 croak 'encode_scenario() requires a hashref'
47 unless ref $data eq 'HASH';
48  
49 my $json = $self->{json}->encode($data);
50  
51 return $json;
52 }
53  
54 #----------------------------------------------------------------------#
55 # Decoding
56 #----------------------------------------------------------------------#
57  
58 sub decode_scenario {
59 my ($self, $text) = @_;
60  
61 # Entry: JSON string
62 # Exit: hashref representing a scenario
63 # Side effects: none
64 # Notes: validates that result is a hashref
65  
66 croak 'decode_scenario() requires a defined string'
67 unless defined $text;
68  
69 my $data = $self->{json}->decode($text);
70  
71 croak 'decode_scenario() did not return a hashref'
72 unless ref $data eq 'HASH';
73  
74 return $data;
75 }
76  
77 1;
78  
79 __END__