| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Test::JSON::More; |
|
2
|
2
|
|
|
2
|
|
21995
|
use strict; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
52
|
|
|
3
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
46
|
|
|
4
|
2
|
|
|
2
|
|
1276
|
use Test::Differences; |
|
|
2
|
|
|
|
|
34853
|
|
|
|
2
|
|
|
|
|
121
|
|
|
5
|
2
|
|
|
2
|
|
787
|
use parent 'Test::Builder::Module'; |
|
|
2
|
|
|
|
|
437
|
|
|
|
2
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
my $JSON; |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub import { |
|
12
|
2
|
|
|
2
|
|
13
|
my $class = shift; |
|
13
|
2
|
|
50
|
|
|
14
|
my $json_module = shift || 'JSON'; |
|
14
|
|
|
|
|
|
|
|
|
15
|
2
|
|
|
|
|
2
|
my $caller = caller; |
|
16
|
|
|
|
|
|
|
|
|
17
|
2
|
|
|
|
|
4
|
for my $func (qw/ ok_json cmp_json parsed_json ok_json_schema /) { |
|
18
|
2
|
|
|
2
|
|
8473
|
no strict 'refs'; ## no critic |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
980
|
|
|
19
|
8
|
|
|
|
|
7
|
*{"${caller}::$func"} = \&{"${class}::$func"}; |
|
|
8
|
|
|
|
|
26
|
|
|
|
8
|
|
|
|
|
12
|
|
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
5
|
$JSON = _load_module($json_module)->new; |
|
23
|
|
|
|
|
|
|
} |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _load_module { |
|
26
|
4
|
|
|
4
|
|
9
|
my $module = shift; |
|
27
|
|
|
|
|
|
|
|
|
28
|
4
|
|
|
|
|
5
|
my $lib = $module; |
|
29
|
4
|
|
|
|
|
8
|
$lib =~ s!::!/!g; |
|
30
|
4
|
|
|
|
|
1650
|
require "$lib.pm"; ## no critic |
|
31
|
4
|
|
|
|
|
46103
|
$lib->import; |
|
32
|
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
230
|
$module; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
our $PARSED_HASH; |
|
37
|
1
|
|
|
1
|
1
|
8
|
sub parsed_json { $PARSED_HASH } |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub ok_json { |
|
40
|
1
|
|
|
1
|
1
|
5
|
my ($input_json, $test_name) = @_; |
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
7
|
my $test = __PACKAGE__->builder; |
|
43
|
|
|
|
|
|
|
|
|
44
|
1
|
50
|
|
|
|
10
|
$test->croak("ok_json: undefined JSON") unless defined $input_json; |
|
45
|
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
2
|
eval { $PARSED_HASH = $JSON->decode($input_json) }; |
|
|
1
|
|
|
|
|
9
|
|
|
47
|
|
|
|
|
|
|
|
|
48
|
1
|
50
|
|
|
|
3
|
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
|
|
|
|
|
3
|
$test->ok( 1, $test_name ); |
|
55
|
1
|
|
|
|
|
234
|
return 1; |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub cmp_json { |
|
60
|
1
|
|
|
1
|
1
|
4
|
my ($input_json, $expected_json, $test_name) = @_; |
|
61
|
|
|
|
|
|
|
|
|
62
|
1
|
|
|
|
|
3
|
my $test = __PACKAGE__->builder; |
|
63
|
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
8
|
$test->croak("cmp_json: input JSON is undefined") unless defined $input_json; |
|
65
|
1
|
50
|
|
|
|
2
|
$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
|
|
|
|
|
9
|
|
|
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
|
|
|
|
|
3
|
$json_for{ $item->[0] } = $PARSED_HASH; |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
} |
|
80
|
1
|
|
|
|
|
2
|
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
|
696
|
my ( $input_json, $schema, $test_name ) = @_; |
|
86
|
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
9
|
my $test = __PACKAGE__->builder; |
|
88
|
|
|
|
|
|
|
|
|
89
|
2
|
50
|
|
|
|
12
|
$test->croak("ok_json_schema: input JSON is undefined") unless defined $input_json; |
|
90
|
|
|
|
|
|
|
|
|
91
|
2
|
|
|
|
|
3
|
eval { $PARSED_HASH = $JSON->decode($input_json) }; |
|
|
2
|
|
|
|
|
14
|
|
|
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
|
|
|
|
|
17
|
my $jsv = _load_module('JSV::Validator')->new; |
|
99
|
2
|
100
|
66
|
|
|
32266
|
$schema = (!defined $schema || ref($schema) eq 'HASH') ? $schema : $JSON->decode($schema); |
|
100
|
2
|
|
|
|
|
7
|
my $res = $jsv->validate($schema, $PARSED_HASH); |
|
101
|
|
|
|
|
|
|
|
|
102
|
2
|
50
|
50
|
|
|
880
|
if ( $res->error || scalar @{$res->errors} ) { |
|
|
2
|
|
|
|
|
30
|
|
|
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
|
|
|
|
9
|
if (!defined $schema) { |
|
111
|
0
|
0
|
|
|
|
0
|
$test->carp("Schema is undefined". ($test_name ? ":$test_name" : "")); |
|
112
|
|
|
|
|
|
|
} |
|
113
|
2
|
|
|
|
|
8
|
$test->ok( 1, $test_name ); |
|
114
|
2
|
|
|
|
|
451
|
return 1; |
|
115
|
|
|
|
|
|
|
} |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
__END__ |