line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::JSON; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
154730
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
11
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
181
|
|
5
|
2
|
|
|
2
|
|
2414
|
use Test::Differences; |
|
2
|
|
|
|
|
45840
|
|
|
2
|
|
|
|
|
188
|
|
6
|
2
|
|
|
2
|
|
2149
|
use JSON::Any; |
|
2
|
|
|
|
|
101960
|
|
|
2
|
|
|
|
|
14
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
47000
|
use base 'Test::Builder::Module'; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
2746
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw/is_json is_valid_json/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Test::JSON - Test JSON data |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.11 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.11'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $JSON = JSON::Any->new; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 SYNOPSIS |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
use Test::JSON; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
is_valid_json $json, '... json is well formed'; |
30
|
|
|
|
|
|
|
is_json $json, $expected_json, '... and it matches what we expected'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 EXPORT |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over 4 |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item * is_valid_json |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item * is_json |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=back |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
JavaScript Object Notation (JSON) is a lightweight data interchange format. |
45
|
|
|
|
|
|
|
L makes it easy to verify that you have built valid JSON and that |
46
|
|
|
|
|
|
|
it matches your expected output. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
See L for more information. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 TESTS |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 is_valid_json |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
is_valid_json $json, '... json is well formed'; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Test passes if the string passed is valid JSON. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 is_json |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
is_json $json, $expected_json, '... and it matches what we expected'; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Test passes if the two JSON strings are valid JSON and evaluate to the same |
63
|
|
|
|
|
|
|
data structure. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
L is used to provide easy diagnostics of why the JSON |
66
|
|
|
|
|
|
|
structures did not match. For example: |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
Failed test '... and identical JSON should match' |
69
|
|
|
|
|
|
|
in t/10testjson.t at line 14. |
70
|
|
|
|
|
|
|
+----+---------------------------+---------------------------+ |
71
|
|
|
|
|
|
|
| Elt|Got |Expected | |
72
|
|
|
|
|
|
|
+----+---------------------------+---------------------------+ |
73
|
|
|
|
|
|
|
| 0|{ |{ | |
74
|
|
|
|
|
|
|
| 1| bool => '1', | bool => '1', | |
75
|
|
|
|
|
|
|
| 2| description => bless( { | description => bless( { | |
76
|
|
|
|
|
|
|
| 3| value => undef | value => undef | |
77
|
|
|
|
|
|
|
| 4| }, 'JSON::NotString' ), | }, 'JSON::NotString' ), | |
78
|
|
|
|
|
|
|
| 5| id => '1', | id => '1', | |
79
|
|
|
|
|
|
|
* 6| name => 'foo' | name => 'fo' * |
80
|
|
|
|
|
|
|
| 7|} |} | |
81
|
|
|
|
|
|
|
+----+---------------------------+---------------------------+ |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=cut |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub is_valid_json ($;$) { |
86
|
2
|
|
|
2
|
1
|
26185
|
my ( $input, $test_name ) = @_; |
87
|
2
|
50
|
|
|
|
13
|
croak "usage: is_valid_json(input,test_name)" |
88
|
|
|
|
|
|
|
unless defined $input; |
89
|
2
|
|
|
|
|
4
|
eval { $JSON->decode($input) }; |
|
2
|
|
|
|
|
14
|
|
90
|
2
|
|
|
|
|
84
|
my $test = __PACKAGE__->builder; |
91
|
2
|
100
|
|
|
|
18
|
if ( my $error = $@ ) { |
92
|
1
|
|
|
|
|
8
|
$test->ok( 0, $test_name ); |
93
|
1
|
|
|
|
|
113
|
$test->diag("Input was not valid JSON:\n\n\t$error"); |
94
|
1
|
|
|
|
|
37
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else { |
97
|
1
|
|
|
|
|
9
|
$test->ok( 1, $test_name ); |
98
|
1
|
|
|
|
|
133
|
return 1; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub is_json ($$;$) { |
103
|
4
|
|
|
4
|
1
|
9747
|
my ( $input, $expected, $test_name ) = @_; |
104
|
4
|
50
|
33
|
|
|
25
|
croak "usage: is_json(input,expected,test_name)" |
105
|
|
|
|
|
|
|
unless defined $input && defined $expected; |
106
|
|
|
|
|
|
|
|
107
|
4
|
|
|
|
|
4
|
my %json_for; |
108
|
4
|
|
|
|
|
13
|
foreach my $item ( [ input => $input ], [ expected => $expected ] ) { |
109
|
8
|
|
|
|
|
11
|
my $json = eval { $JSON->decode( $item->[1] ) }; |
|
8
|
|
|
|
|
29
|
|
110
|
8
|
|
|
|
|
195
|
my $test = __PACKAGE__->builder; |
111
|
8
|
100
|
|
|
|
45
|
if ( my $error = $@ ) { |
112
|
1
|
|
|
|
|
9
|
$test->ok( 0, $test_name ); |
113
|
1
|
|
|
|
|
123
|
$test->diag("$item->[0] was not valid JSON: $error"); |
114
|
1
|
|
|
|
|
35
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
7
|
|
|
|
|
20
|
$json_for{ $item->[0] } = $json; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
3
|
|
|
|
|
9
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
121
|
3
|
|
|
|
|
15
|
eq_or_diff( $json_for{input}, $json_for{expected}, $test_name ); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 AUTHOR |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Curtis "Ovid" Poe, C<< >> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 BUGS |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
131
|
|
|
|
|
|
|
C, or through the web interface at |
132
|
|
|
|
|
|
|
L. |
133
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
134
|
|
|
|
|
|
|
your bug as I make changes. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 SEE ALSO |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This test module uses L and L. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The development of this module was sponsored by Kineticode, |
143
|
|
|
|
|
|
|
L, the leading provider of services for the |
144
|
|
|
|
|
|
|
Bricolage content management system, L. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Thanks to Makamaka Hannyaharamitu C for a patch to make |
147
|
|
|
|
|
|
|
this work with JSON 2.0. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Thanks to Stevan Little for suggesting a switch to L. This makes |
150
|
|
|
|
|
|
|
it easier for this module to work with whatever JSON module you have |
151
|
|
|
|
|
|
|
installed. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Copyright 2005-2007 Curtis "Ovid" Poe, all rights reserved. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
158
|
|
|
|
|
|
|
under the same terms as Perl itself. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
1; |