line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::JSON::Type; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
86895
|
use base qw(Test::Builder::Module); |
|
4
|
|
|
|
|
24
|
|
|
4
|
|
|
|
|
1318
|
|
4
|
4
|
|
|
4
|
|
146984
|
use strict; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
71
|
|
5
|
4
|
|
|
4
|
|
33
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
106
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
2522
|
use Cpanel::JSON::XS; |
|
4
|
|
|
|
|
15086
|
|
|
4
|
|
|
|
|
217
|
|
8
|
4
|
|
|
4
|
|
1238
|
use Cpanel::JSON::XS::Type; |
|
4
|
|
|
|
|
2331
|
|
|
4
|
|
|
|
|
328
|
|
9
|
4
|
|
|
4
|
|
845
|
use English; |
|
4
|
|
|
|
|
5921
|
|
|
4
|
|
|
|
|
24
|
|
10
|
4
|
|
|
4
|
|
2823
|
use Error::Pure qw(err); |
|
4
|
|
|
|
|
13925
|
|
|
4
|
|
|
|
|
67
|
|
11
|
4
|
|
|
4
|
|
188
|
use Readonly; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
127
|
|
12
|
4
|
|
|
4
|
|
1836
|
use Test::Differences qw(eq_or_diff); |
|
4
|
|
|
|
|
61082
|
|
|
4
|
|
|
|
|
2449
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Readonly::Array our @EXPORT => qw(is_json_type is_json_type_struct); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = 0.02; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub is_json_type { |
19
|
12
|
|
|
12
|
1
|
23314
|
my ($json, $json_expected, $test_name) = @_; |
20
|
|
|
|
|
|
|
|
21
|
12
|
100
|
|
|
|
31
|
if (! defined $json) { |
22
|
1
|
|
|
|
|
3
|
err 'JSON string to compare is required.'; |
23
|
|
|
|
|
|
|
} |
24
|
11
|
100
|
|
|
|
17
|
if (! defined $json_expected) { |
25
|
1
|
|
|
|
|
3
|
err 'Expected JSON string to compare is required.'; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
10
|
|
|
|
|
32
|
my $test = __PACKAGE__->builder; |
29
|
10
|
|
|
|
|
91
|
my $json_obj = Cpanel::JSON::XS->new; |
30
|
|
|
|
|
|
|
|
31
|
10
|
|
|
|
|
13
|
my $type_hr; |
32
|
10
|
|
|
|
|
11
|
eval { |
33
|
10
|
|
|
|
|
72
|
$json_obj->decode($json, $type_hr); |
34
|
|
|
|
|
|
|
}; |
35
|
10
|
100
|
|
|
|
26
|
if ($EVAL_ERROR) { |
36
|
1
|
|
|
|
|
4
|
err "JSON string isn't valid.", |
37
|
|
|
|
|
|
|
'Error', $EVAL_ERROR, |
38
|
|
|
|
|
|
|
; |
39
|
|
|
|
|
|
|
} |
40
|
9
|
|
|
|
|
17
|
_readable_types($type_hr); |
41
|
9
|
|
|
|
|
10
|
my $type_expected_hr; |
42
|
9
|
|
|
|
|
10
|
eval { |
43
|
9
|
|
|
|
|
38
|
$json_obj->decode($json_expected, $type_expected_hr); |
44
|
|
|
|
|
|
|
}; |
45
|
9
|
100
|
|
|
|
21
|
if ($EVAL_ERROR) { |
46
|
1
|
|
|
|
|
3
|
err "Expected JSON string isn't valid.", |
47
|
|
|
|
|
|
|
'Error', $EVAL_ERROR, |
48
|
|
|
|
|
|
|
; |
49
|
|
|
|
|
|
|
} |
50
|
8
|
|
|
|
|
15
|
_readable_types($type_expected_hr); |
51
|
|
|
|
|
|
|
|
52
|
8
|
|
|
|
|
13
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
53
|
8
|
|
|
|
|
16
|
return eq_or_diff($type_hr, $type_expected_hr, $test_name); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub is_json_type_struct { |
57
|
13
|
|
|
13
|
1
|
14748
|
my ($json, $type_expected_hr, $test_name) = @_; |
58
|
|
|
|
|
|
|
|
59
|
13
|
100
|
|
|
|
30
|
if (! defined $json) { |
60
|
1
|
|
|
|
|
5
|
err 'JSON string to compare is required.'; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
12
|
|
|
|
|
34
|
my $test = __PACKAGE__->builder; |
64
|
12
|
|
|
|
|
107
|
my $json_obj = Cpanel::JSON::XS->new; |
65
|
|
|
|
|
|
|
|
66
|
12
|
|
|
|
|
18
|
my $type_hr; |
67
|
12
|
|
|
|
|
13
|
my $json_hr = eval { |
68
|
12
|
|
|
|
|
69
|
$json_obj->decode($json, $type_hr); |
69
|
|
|
|
|
|
|
}; |
70
|
12
|
100
|
|
|
|
36
|
if ($EVAL_ERROR) { |
71
|
1
|
|
|
|
|
4
|
err "JSON string isn't valid.", |
72
|
|
|
|
|
|
|
'Error', $EVAL_ERROR, |
73
|
|
|
|
|
|
|
; |
74
|
|
|
|
|
|
|
} |
75
|
11
|
|
|
|
|
23
|
_readable_types($type_hr); |
76
|
|
|
|
|
|
|
|
77
|
11
|
|
|
|
|
15
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
78
|
11
|
|
|
|
|
11
|
eval { |
79
|
11
|
|
|
|
|
59
|
$json_obj->encode($json_hr, $type_expected_hr); |
80
|
|
|
|
|
|
|
}; |
81
|
11
|
50
|
|
|
|
24
|
if ($EVAL_ERROR) { |
82
|
0
|
|
|
|
|
0
|
$test->ok(0, $test_name); |
83
|
0
|
|
|
|
|
0
|
$test->diag('Error: '.$EVAL_ERROR); |
84
|
0
|
|
|
|
|
0
|
return; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
11
|
|
|
|
|
29
|
$test->ok(1, $test_name); |
88
|
11
|
|
|
|
|
2634
|
return 1; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub _change_type { |
92
|
43
|
|
|
43
|
|
45
|
my $value_sr = shift; |
93
|
|
|
|
|
|
|
|
94
|
43
|
100
|
|
|
|
45
|
if (${$value_sr} == JSON_TYPE_BOOL) { |
|
43
|
100
|
|
|
|
68
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
95
|
6
|
|
|
|
|
8
|
${$value_sr} = 'JSON_TYPE_BOOL'; |
|
6
|
|
|
|
|
8
|
|
96
|
37
|
|
|
|
|
48
|
} elsif (${$value_sr} == JSON_TYPE_INT) { |
97
|
16
|
|
|
|
|
19
|
${$value_sr} = 'JSON_TYPE_INT'; |
|
16
|
|
|
|
|
22
|
|
98
|
21
|
|
|
|
|
32
|
} elsif (${$value_sr} == JSON_TYPE_FLOAT) { |
99
|
6
|
|
|
|
|
9
|
${$value_sr} = 'JSON_TYPE_FLOAT'; |
|
6
|
|
|
|
|
10
|
|
100
|
15
|
|
|
|
|
25
|
} elsif (${$value_sr} == JSON_TYPE_STRING) { |
101
|
9
|
|
|
|
|
21
|
${$value_sr} = 'JSON_TYPE_STRING'; |
|
9
|
|
|
|
|
13
|
|
102
|
6
|
|
|
|
|
11
|
} elsif (${$value_sr} == JSON_TYPE_NULL) { |
103
|
6
|
|
|
|
|
8
|
${$value_sr} = 'JSON_TYPE_NULL'; |
|
6
|
|
|
|
|
8
|
|
104
|
|
|
|
|
|
|
} else { |
105
|
0
|
|
|
|
|
0
|
err "Unsupported value '${$value_sr}'."; |
|
0
|
|
|
|
|
0
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
43
|
|
|
|
|
55
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub _readable_types { |
112
|
83
|
|
|
83
|
|
93
|
my $type_r = shift; |
113
|
|
|
|
|
|
|
|
114
|
83
|
100
|
|
|
|
175
|
if (ref $type_r eq 'HASH') { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
115
|
31
|
|
|
|
|
32
|
foreach my $sub_key (keys %{$type_r}) { |
|
31
|
|
|
|
|
77
|
|
116
|
28
|
100
|
|
|
|
58
|
if (ref $type_r->{$sub_key}) { |
117
|
9
|
|
|
|
|
15
|
_readable_types($type_r->{$sub_key}); |
118
|
|
|
|
|
|
|
} else { |
119
|
19
|
|
|
|
|
33
|
_readable_types(\$type_r->{$sub_key}); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} elsif (ref $type_r eq 'ARRAY') { |
123
|
9
|
|
|
|
|
11
|
foreach my $sub_type (@{$type_r}) { |
|
9
|
|
|
|
|
14
|
|
124
|
27
|
100
|
|
|
|
45
|
if (ref $sub_type) { |
125
|
3
|
|
|
|
|
4
|
_readable_types($sub_type); |
126
|
|
|
|
|
|
|
} else { |
127
|
24
|
|
|
|
|
39
|
_readable_types(\$sub_type); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} elsif (ref $type_r eq 'SCALAR') { |
131
|
43
|
|
|
|
|
51
|
_change_type($type_r); |
132
|
|
|
|
|
|
|
} else { |
133
|
0
|
|
|
|
|
0
|
err "Unsupported value '$type_r'."; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
83
|
|
|
|
|
111
|
return; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__ |