Scenario/Serializer/YAML.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 61 CC 6 SCAR 19.8
CC
6
Cov
0%
decode_scenario
12.0
_normalize_structure
12.0
encode_scenario
6.0
CRAP 7.2
1 package Test::HTTP::Scenario::Serializer::YAML;
2  
3 use strict;
4 use warnings;
5 use Carp qw(croak carp);
6 use YAML::XS ();
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: stateless object
19  
20 my $self = bless {}, $class;
21  
22 return $self;
23 }
24  
25 #----------------------------------------------------------------------#
26 # Encoding
27 #----------------------------------------------------------------------#
28  
29 sub encode_scenario {
30 my ($self, $data) = @_;
31  
32 # Entry: hashref representing a scenario
33 # Exit: YAML string
34 # Side effects: none
35 # Notes: keys are sorted for deterministic output
36  
37 croak 'encode_scenario() requires a hashref'
38 unless ref $data eq 'HASH';
39  
40 my $normalized = _normalize_structure($data);
41  
42 my $yaml = YAML::XS::Dump($normalized);
43  
44 return $yaml;
45 }
46  
47 #----------------------------------------------------------------------#
48 # Decoding
49 #----------------------------------------------------------------------#
50  
51 sub decode_scenario {
52 my ($self, $text) = @_;
53  
54 # Entry: YAML string
55 # Exit: hashref representing a scenario
56 # Side effects: none
57 # Notes: validates that result is a hashref
58  
59 croak 'decode_scenario() requires a defined string'
60 unless defined $text;
61  
62 my $data = YAML::XS::Load($text);
63  
64 croak 'decode_scenario() did not return a hashref'
65 unless ref $data eq 'HASH';
66  
67 return $data;
68 }
69  
70 #----------------------------------------------------------------------#
71 # Internal normalization
72 #----------------------------------------------------------------------#
73  
74 sub _normalize_structure {
75 my ($value) = @_;
76  
77 # Entry: arbitrary Perl structure
78 # Exit: structure with hashes sorted by key
79 # Side effects: none
80 # Notes: recursive normalization for deterministic dumps
81  
82 if (ref $value eq 'HASH') {
83 my %out;
84 for my $k (sort keys %{$value}) {
85 $out{$k} = _normalize_structure($value->{$k});
86 }
87 return \%out;
88 }
89  
90 if (ref $value eq 'ARRAY') {
91 my @out = map { _normalize_structure($_) } @{$value};
92 return \@out;
93 }
94  
95 return $value;
96 }
97  
98 1;
99  
100 __END__