line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::YAML::Valid; |
2
|
|
|
|
|
|
|
|
3
|
10
|
|
|
10
|
|
436477
|
use warnings; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
350
|
|
4
|
10
|
|
|
10
|
|
131
|
use strict; |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
462
|
|
5
|
10
|
|
|
10
|
|
59
|
use Test::Builder; |
|
10
|
|
|
|
|
26
|
|
|
10
|
|
|
|
|
234
|
|
6
|
10
|
|
|
10
|
|
56
|
use base 'Exporter'; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
1217
|
|
7
|
10
|
|
|
10
|
|
61
|
use Carp qw(confess); |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
11678
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw(yaml_string_ok yaml_file_ok yaml_files_ok); |
10
|
|
|
|
|
|
|
our @EXPORT = @EXPORT_OK; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
13
|
9
|
|
|
9
|
|
87
|
my @_import = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# sort the import list into attempts to load alternate YAML |
16
|
|
|
|
|
|
|
# parsers ("requests"), and the actual import list to pass to |
17
|
|
|
|
|
|
|
# Exporter. |
18
|
9
|
|
|
|
|
17
|
my @requests; |
19
|
|
|
|
|
|
|
my @import; |
20
|
9
|
|
|
|
|
24
|
for my $elt (@_import){ |
21
|
15
|
100
|
|
|
|
80
|
if( $elt =~ /^-([A-Za-z::]+)$/ ){ |
22
|
6
|
|
|
|
|
41
|
push @requests, $1; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
else { |
25
|
9
|
|
|
|
|
28
|
push @import, $elt; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
9
|
50
|
|
|
|
55
|
confess 'You can only specify one YAML module to use; you specified '. scalar @requests |
30
|
|
|
|
|
|
|
if @requests > 1; |
31
|
|
|
|
|
|
|
|
32
|
9
|
|
|
|
|
20
|
my $request_ok = 0; |
33
|
9
|
|
|
|
|
147
|
my $request = $requests[0]; |
34
|
9
|
100
|
|
|
|
53
|
if($request){ |
35
|
6
|
|
|
|
|
12
|
eval { |
36
|
6
|
50
|
|
6
|
|
404
|
eval "use YAML::$request qw(Load LoadFile); 1" or die; |
|
6
|
|
|
|
|
35
|
|
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
313
|
|
37
|
6
|
|
|
|
|
15
|
$request_ok = 1; |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
9
|
100
|
|
|
|
36
|
if(!$request_ok){ |
42
|
3
|
|
|
|
|
2806
|
require YAML; |
43
|
3
|
|
|
3
|
|
30
|
eval "use YAML qw(Load LoadFile)"; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
249
|
|
|
3
|
|
|
|
|
38147
|
|
44
|
3
|
50
|
|
|
|
19
|
Test::Builder->new->diag("Falling back to YAML from YAML::$request") |
45
|
|
|
|
|
|
|
if $request; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
9
|
|
|
|
|
20125
|
__PACKAGE__->export_to_level(1, @import); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 NAME |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Test::YAML::Valid - Test for valid YAML |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 VERSION |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Version 0.04 |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=cut |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 SYNOPSIS |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
This module lets you easily test the validity of YAML: |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
use Test::More tests => 3; |
68
|
|
|
|
|
|
|
use Test::YAML::Valid; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
yaml_string_ok(YAML::Dump({foo => 'bar'}), 'YAML generates good YAML?'); |
71
|
|
|
|
|
|
|
yaml_string_ok('this is not YAML, is it?', 'This one will fail'); |
72
|
|
|
|
|
|
|
yaml_file_ok('/path/to/some/YAML', '/path/to/some/YAML is YAML'); |
73
|
|
|
|
|
|
|
yaml_files_ok('/path/to/YAML/files/*', 'all YAML files are valid'); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
You can also test with L instead of |
76
|
|
|
|
|
|
|
L by passing C<-Syck> in the import list: |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
use Test::YAML::Valid qw(-Syck); |
79
|
|
|
|
|
|
|
yaml_string_ok(...); # uses YAML::Syck::Load instead of YAML::Load |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
It's up to you to make sure you have YAML::Syck if you specify the |
82
|
|
|
|
|
|
|
C<-Syck> option, since it's an optional prerequisite to this module. |
83
|
|
|
|
|
|
|
If it's requested but not found, a warning will be issued and YAML |
84
|
|
|
|
|
|
|
will be used instead. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
As of version 0.04, you can use any module you want in the same way; |
87
|
|
|
|
|
|
|
C<-Tiny> for YAML::Tiny and C<-XS> for YAML::XS. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 EXPORT |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=over 4 |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=item * yaml_string_ok |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item * yaml_file_ok |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=item * yaml_files_ok |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 FUNCTIONS |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head2 yaml_string_ok($yaml, [$message]) |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Test will pass if C<$yaml> contains valid YAML (according to YAML.pm) |
106
|
|
|
|
|
|
|
and fail otherwise. Returns the result of loading the YAML. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# workaround for YAML::Syck -- it doesn't parse report errors!!!!! |
111
|
|
|
|
|
|
|
sub _is_undef_yaml($){ |
112
|
24
|
|
|
24
|
|
40
|
my $yaml = shift; |
113
|
24
|
50
|
|
|
|
518
|
return if !defined $yaml; |
114
|
24
|
50
|
|
|
|
358
|
return 1 if $yaml =~ /^(?:---(?:\s+~?)?\s+)+$/m; |
115
|
|
|
|
|
|
|
# XXX: ... should be OK: |
116
|
|
|
|
|
|
|
#/^(?:---)?(?: ~)?\n+(?:[.][.][.]\n+)?$/; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
0
|
return 0; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub _is_yaml($$){ |
122
|
152
|
|
66
|
152
|
|
1250
|
return (defined $_[0] || _is_undef_yaml($_[1])); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub yaml_string_ok($;$) { |
126
|
24
|
|
|
24
|
1
|
3911
|
my $yaml = shift; |
127
|
24
|
|
|
|
|
46
|
my $msg = shift; |
128
|
24
|
|
|
|
|
38
|
my $result; |
129
|
|
|
|
|
|
|
|
130
|
24
|
|
|
|
|
39
|
eval { |
131
|
24
|
|
|
|
|
475
|
$result = Load($yaml); |
132
|
|
|
|
|
|
|
}; |
133
|
|
|
|
|
|
|
|
134
|
24
|
|
|
|
|
81470
|
my $test = Test::Builder->new(); |
135
|
24
|
|
66
|
|
|
254
|
$test->ok(!$@ && _is_yaml($result,$yaml), $msg); |
136
|
24
|
|
|
|
|
15914
|
return $result; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 yaml_file_ok($filename, [$message]) |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Test will pass if C<$filename> is a valid YAML file (according to |
142
|
|
|
|
|
|
|
YAML.pm) and fail otherwise. Returns the result of loading the YAML. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub yaml_file_ok($;$) { |
147
|
24
|
|
|
24
|
1
|
4868
|
my $file = shift; |
148
|
24
|
|
|
|
|
41
|
my $msg = shift; |
149
|
24
|
|
|
|
|
39
|
my $result; |
150
|
|
|
|
|
|
|
my $yaml; |
151
|
24
|
|
|
|
|
38
|
eval { |
152
|
24
|
|
|
|
|
91
|
$result = LoadFile($file); |
153
|
16
|
50
|
|
|
|
77297
|
if(!defined $result){ # special case for YAML::Syck |
154
|
0
|
0
|
|
|
|
0
|
open my $fh, '<', $file or die "Can't open $file: $!"; |
155
|
0
|
|
|
|
|
0
|
$yaml = do {local $/; <$fh> }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
156
|
0
|
|
|
|
|
0
|
close $fh; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
|
160
|
24
|
|
|
|
|
9822
|
my $test = Test::Builder->new(); |
161
|
24
|
100
|
|
|
|
224
|
$msg = "$file contains valid YAML" unless $msg; |
162
|
24
|
|
66
|
|
|
147
|
$test->ok(!$@ && _is_yaml($result,$yaml), $msg); |
163
|
24
|
|
|
|
|
20912
|
return $result; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head2 yaml_files_ok($file_glob_string, [$message]) |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Test will pass if all files matching the glob C<$file_glob_string> |
169
|
|
|
|
|
|
|
contain valid YAML. If a file is not valid, the test will fail and no |
170
|
|
|
|
|
|
|
further files will be examined. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Returns a list of all loaded YAML; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub yaml_files_ok($;$) { |
177
|
28
|
|
|
28
|
1
|
5863
|
my $file_glob = shift; |
178
|
28
|
|
|
|
|
47
|
my $msg = shift; |
179
|
28
|
|
|
|
|
166
|
my @results; |
180
|
|
|
|
|
|
|
my $result; |
181
|
|
|
|
|
|
|
|
182
|
28
|
|
|
|
|
153
|
my $test = Test::Builder->new(); |
183
|
28
|
100
|
|
|
|
277
|
$msg = "$file_glob contains valid YAML files" unless $msg; |
184
|
28
|
|
|
|
|
6587
|
foreach my $file (glob($file_glob)) { |
185
|
136
|
|
|
|
|
208
|
my $yaml = ""; |
186
|
136
|
100
|
|
|
|
3419
|
next if -d $file; # skip directories |
187
|
128
|
|
|
|
|
190
|
eval { |
188
|
128
|
|
|
|
|
352
|
$result = LoadFile($file); |
189
|
120
|
100
|
|
|
|
101280
|
if(!defined $result){ # special case for YAML::Syck |
190
|
24
|
50
|
|
|
|
1038
|
open my $fh, '<', $file or die "Can't open $file: $!"; |
191
|
24
|
|
|
|
|
36
|
$yaml = do {local $/; <$fh> }; |
|
24
|
|
|
|
|
78
|
|
|
24
|
|
|
|
|
465
|
|
192
|
24
|
|
|
|
|
702
|
close $fh; |
193
|
|
|
|
|
|
|
} |
194
|
120
|
|
|
|
|
262
|
push @results, $result; |
195
|
|
|
|
|
|
|
}; |
196
|
128
|
100
|
66
|
|
|
10539
|
if ($@ || !_is_yaml($result,$yaml)) { |
197
|
8
|
|
|
|
|
181
|
$test->ok(0, $msg); |
198
|
8
|
|
|
|
|
25134
|
$test->diag(" Could not load file: $file."); |
199
|
8
|
|
|
|
|
674
|
return; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
20
|
|
|
|
|
128
|
$test->ok(1, $msg); |
204
|
20
|
|
|
|
|
8309
|
return \@results; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head1 AUTHOR |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Jonathan Rockway, C<< >> |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head1 BUGS |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
215
|
|
|
|
|
|
|
C, or through the web interface at |
216
|
|
|
|
|
|
|
L. |
217
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
218
|
|
|
|
|
|
|
your bug as I make changes. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 SUPPORT |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
perldoc Test::YAML::Valid |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
You can also look for information at: |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=over 4 |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
L |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=item * CPAN Ratings |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
L |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
L |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item * Search CPAN |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
L |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=back |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Stevan Little C<< >> contributed |
251
|
|
|
|
|
|
|
C and some more tests. |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Copyright 2007 Jonathan Rockway, all rights reserved. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
258
|
|
|
|
|
|
|
under the same terms as Perl itself. |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=cut |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
1; # End of Test::YAML::Valid |