line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::JSON::More; |
2
|
2
|
|
|
2
|
|
13366
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
158
|
|
3
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
42
|
|
4
|
2
|
|
|
2
|
|
1005
|
use Test::Differences; |
|
2
|
|
|
|
|
32500
|
|
|
2
|
|
|
|
|
169
|
|
5
|
2
|
|
|
2
|
|
945
|
use parent 'Test::Builder::Module'; |
|
2
|
|
|
|
|
557
|
|
|
2
|
|
|
|
|
8
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $JSON; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub import { |
12
|
2
|
|
|
2
|
|
12
|
my $class = shift; |
13
|
2
|
|
50
|
|
|
14
|
my $json_module = shift || 'JSON'; |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
|
|
4
|
my $caller = caller; |
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
|
|
2
|
for my $func (qw/ ok_json cmp_json parsed_json ok_json_schema /) { |
18
|
2
|
|
|
2
|
|
9505
|
no strict 'refs'; ## no critic |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
1000
|
|
19
|
8
|
|
|
|
|
10
|
*{"${caller}::$func"} = \&{"${class}::$func"}; |
|
8
|
|
|
|
|
21
|
|
|
8
|
|
|
|
|
14
|
|
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
4
|
$JSON = _load_module($json_module)->new; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _load_module { |
26
|
4
|
|
|
4
|
|
15
|
my $module = shift; |
27
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
6
|
my $lib = $module; |
29
|
4
|
|
|
|
|
8
|
$lib =~ s!::!/!g; |
30
|
4
|
|
|
|
|
1663
|
require "$lib.pm"; ## no critic |
31
|
4
|
|
|
|
|
48386
|
$lib->import; |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
259
|
$module; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $PARSED_HASH; |
37
|
1
|
|
|
1
|
1
|
9
|
sub parsed_json { $PARSED_HASH } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub ok_json { |
40
|
1
|
|
|
1
|
1
|
8
|
my ($input_json, $test_name) = @_; |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
13
|
my $test = __PACKAGE__->builder; |
43
|
|
|
|
|
|
|
|
44
|
1
|
50
|
|
|
|
20
|
$test->croak("ok_json: undefined JSON") unless defined $input_json; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
2
|
eval { $PARSED_HASH = $JSON->decode($input_json) }; |
|
1
|
|
|
|
|
15
|
|
47
|
|
|
|
|
|
|
|
48
|
1
|
50
|
|
|
|
4
|
if ( my $error = $@ ) { |
49
|
0
|
|
|
|
|
0
|
$test->ok( 0, $test_name ); |
50
|
0
|
|
|
|
|
0
|
$test->diag("invalid JSON:\n\n\t$error"); |
51
|
0
|
|
|
|
|
0
|
return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
1
|
|
|
|
|
5
|
$test->ok( 1, $test_name ); |
55
|
1
|
|
|
|
|
343
|
return 1; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub cmp_json { |
60
|
1
|
|
|
1
|
1
|
4
|
my ($input_json, $expected_json, $test_name) = @_; |
61
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
4
|
my $test = __PACKAGE__->builder; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
8
|
$test->croak("cmp_json: input JSON is undefined") unless defined $input_json; |
65
|
1
|
50
|
|
|
|
3
|
$test->croak("cmp_json: expected JSON is undefined") unless defined $expected_json; |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
1
|
my %json_for; |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
4
|
for my $item ( [ input => $input_json ], [ expected => $expected_json ] ) { |
70
|
2
|
|
|
|
|
2
|
eval { $PARSED_HASH = $JSON->decode($item->[1]) }; |
|
2
|
|
|
|
|
10
|
|
71
|
2
|
50
|
|
|
|
5
|
if ( my $error = $@ ) { |
72
|
0
|
|
|
|
|
0
|
$test->ok( 0, $test_name ); |
73
|
0
|
|
|
|
|
0
|
$test->diag("$item->[0] is invalid JSON:\n\n\t$error"); |
74
|
0
|
|
|
|
|
0
|
return; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
2
|
|
|
|
|
4
|
$json_for{ $item->[0] } = $PARSED_HASH; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
1
|
|
|
|
|
3
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
81
|
1
|
|
|
|
|
4
|
eq_or_diff( $json_for{input}, $json_for{expected}, $test_name ); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub ok_json_schema { |
85
|
2
|
|
|
2
|
1
|
744
|
my ( $input_json, $schema, $test_name ) = @_; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
9
|
my $test = __PACKAGE__->builder; |
88
|
|
|
|
|
|
|
|
89
|
2
|
50
|
|
|
|
16
|
$test->croak("ok_json_schema: input JSON is undefined") unless defined $input_json; |
90
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
2
|
eval { $PARSED_HASH = $JSON->decode($input_json) }; |
|
2
|
|
|
|
|
17
|
|
92
|
2
|
50
|
|
|
|
8
|
if ( my $error = $@ ) { |
93
|
0
|
|
|
|
|
0
|
$test->ok( 0, $test_name ); |
94
|
0
|
|
|
|
|
0
|
$test->diag("invalid JSON: $error"); |
95
|
0
|
|
|
|
|
0
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
|
|
18
|
my $jsv = _load_module('JSV::Validator')->new; |
99
|
2
|
100
|
66
|
|
|
33516
|
$schema = (!defined $schema || ref($schema) eq 'HASH') ? $schema : $JSON->decode($schema); |
100
|
2
|
|
|
|
|
9
|
my $res = $jsv->validate($schema, $PARSED_HASH); |
101
|
|
|
|
|
|
|
|
102
|
2
|
50
|
50
|
|
|
1012
|
if ( $res->error || scalar @{$res->errors} ) { |
|
2
|
|
|
|
|
31
|
|
103
|
0
|
|
|
|
|
0
|
$test->ok( 0, $test_name ); |
104
|
0
|
|
|
|
|
0
|
for my $error ( $res->get_error ) { |
105
|
0
|
0
|
|
|
|
0
|
$test->diag("schema error: $error->{message}". ($error->{pointer} ? " pointer:$error->{pointer}" : "")); |
106
|
|
|
|
|
|
|
} |
107
|
0
|
|
|
|
|
0
|
return; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
2
|
50
|
|
|
|
11
|
if (!defined $schema) { |
111
|
0
|
0
|
|
|
|
0
|
$test->carp("Schema is undefined". ($test_name ? ":$test_name" : "")); |
112
|
|
|
|
|
|
|
} |
113
|
2
|
|
|
|
|
7
|
$test->ok( 1, $test_name ); |
114
|
2
|
|
|
|
|
538
|
return 1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |