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