| 1 | package RPC::ExtDirect::Test::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 34 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use Test::More; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 34 | our @EXPORT = qw/ | |||||||||||||||||||||||||||
| 13 | ref_ok | ||||||||||||||||||||||||||||
| 14 | is_deep | ||||||||||||||||||||||||||||
| 15 | cmp_api | ||||||||||||||||||||||||||||
| 16 | prepare_input | ||||||||||||||||||||||||||||
| 17 | /; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 34 | our @EXPORT_OK = qw/ | |||||||||||||||||||||||||||
| 20 | cmp_json | ||||||||||||||||||||||||||||
| 21 | /; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # Replacement for isa_ok that actually checks that wanted value | ||||||||||||||||||||||||||||
| 26 | # is a blessed object and not a string with package name. :( | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 38 | ▶ | sub ref_ok { | ||||||||||||||||||||||||||
|
Subroutine
ref_ok: called CC=4 SCAR=14.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 38 | my ($have, $want, $desc) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 38 | ▶ | $desc = "Object isa $want" unless $desc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 38 | ▶ | ok( (ref $have eq $want) && $have->isa($want), $desc ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $have eq $want && $have->isa($want)
MC/DC0%0/2
ref $have eq $want && $have->isa($want)
ref $have eq $want
$have->isa($want)
Truth table33%1/3
A && B
A= ref $have eq $wantB= $have->isa($want)
| |||||||||||||||||||||||||||||
| 35 | or diag explain "Expected '", $have, "' to be an object blessed into ", | ||||||||||||||||||||||||||||
| 36 | $want, " package"; | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 40 | # | ||||||||||||||||||||||||||||
| 41 | # A wrapper around Test::More::is_deeply() that will print | ||||||||||||||||||||||||||||
| 42 | # the diagnostics if a test fails | ||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 303 | ▶ | sub is_deep { | ||||||||||||||||||||||||||
|
Subroutine
is_deep: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 303 | ▶ | is_deeply @_ or diag explain "Expected: ", $_[1], "Actual: ", $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # Compare two JavaScript API declarations | ||||||||||||||||||||||||||||
| 52 | # | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 8 | ▶ | sub cmp_api { | ||||||||||||||||||||||||||
|
Subroutine
cmp_api: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | # This can be called either as a class method, or a plain sub | ||||||||||||||||||||||||||||
| 56 | 8 | ▶ | shift if $_[0] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 8 | my ($have, $want, $desc) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 8 | ▶ | $have = deparse_api($have) unless ref $have; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 8 | ▶ | $want = deparse_api($want) unless ref $want; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 8 | is_deep $have, $want, $desc; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 67 | # | ||||||||||||||||||||||||||||
| 68 | # Compare two strings ignoring the whitespace | ||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub cmp_str { | ||||||||||||||||||||||||||
|
Subroutine
cmp_str: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | # This can be called either as a class method, or a plain sub | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | shift if $_[0] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my ($have, $want, $desc) = @_; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | $_ =~ s/\s//g for ($have, $want); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | is $have, $want, $desc; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # Compare two JSON structures, ignoring the whitespace | ||||||||||||||||||||||||||||
| 85 | # | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 28 | ▶ | sub cmp_json { | ||||||||||||||||||||||||||
|
Subroutine
cmp_json: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | # This can be called either as a class method, or a plain sub | ||||||||||||||||||||||||||||
| 89 | 28 | ▶ | shift if $_[0] eq __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 28 | my ($have_json, $want_json, $desc) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 28 | $_ =~ s/\s//g for ($have_json, $want_json); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 28 | my $have = JSON::from_json($have_json); | |||||||||||||||||||||||||||
| 96 | 28 | my $want = JSON::from_json($want_json); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 28 | is_deep $have, $want, $desc; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | ### NON EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | # Deparse and normalize a JavaScript string with Ext.Direct API | ||||||||||||||||||||||||||||
| 104 | # declaration into Perl data structures suitable for deep comparison | ||||||||||||||||||||||||||||
| 105 | # | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 16 | ▶ | sub deparse_api { | ||||||||||||||||||||||||||
|
Subroutine
deparse_api: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 16 | my ($api_str) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 16 | $api_str =~ s/\s*//gms; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 16 | my @parts = split /;\s*/, $api_str; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 16 | for my $part ( @parts ) { | |||||||||||||||||||||||||||
| 115 | 40 | ▶ | next unless $part =~ /=\{/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 26 | my ($var, $json) = split /=/, $part; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 26 | my $api_def = JSON::from_json($json); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 26 | my $actions = sort_action_methods($api_def->{actions}); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 26 | ▶ | if ( defined $actions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 16 | $api_def->{actions} = $actions; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 26 | $part = { $var => $api_def }; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 16 | return [ @parts ]; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | ### EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | # Convert a test input hashref into the actual object | ||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub prepare_input { | ||||||||||||||||||||||||||
|
Subroutine
prepare_input: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my ($mod, $input) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return $input unless ref $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Package name should be in the RPC::ExtDirect::Test::Util namespace | ||||||||||||||||||||||||||||
| 144 | 0 | my $pkg = __PACKAGE__.'::'.$mod; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Convertor sub name goes first | ||||||||||||||||||||||||||||
| 147 | 0 | my $conv = $input->{type}; | |||||||||||||||||||||||||||
| 148 | 0 | my $arg = $input->{arg}; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Calling the sub as a class method is easier | ||||||||||||||||||||||||||||
| 151 | # than taking its ref, blah blah | ||||||||||||||||||||||||||||
| 152 | 0 | my $result = $pkg->$conv(@$arg); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | return $result; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | ### NON EXPORTED PUBLIC PACKAGE SUBROUTINE ### | ||||||||||||||||||||||||||||
| 158 | # | ||||||||||||||||||||||||||||
| 159 | # Sort the Method hashrefs on an Action object | ||||||||||||||||||||||||||||
| 160 | # | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 26 | ▶ | sub sort_action_methods { | ||||||||||||||||||||||||||
|
Subroutine
sort_action_methods: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 163 | 26 | my ($api_href) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # %$api_href will auto-vivify if $api_href is undef | ||||||||||||||||||||||||||||
| 166 | # This can bite your ass. | ||||||||||||||||||||||||||||
| 167 | 26 | ▶ | return unless $api_href; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 16 | my $new_href = {}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # map() looks too unwieldy here | ||||||||||||||||||||||||||||
| 172 | 16 | for my $action_name ( keys %$api_href ) { | |||||||||||||||||||||||||||
| 173 | 52 | my @methods = @{ $api_href->{ $action_name } }; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $new_href->{ $action_name } | ||||||||||||||||||||||||||||
| 176 | 52 | = [ sort { $a->{name} cmp $b->{name} } @methods ]; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 16 | return $new_href; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 34 | 1; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||