Skip to content

Commit 59f7892

Browse files
committed
Add support for YAML::XS
1 parent 3cefcad commit 59f7892

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ matrix:
1212
include:
1313
- perl: "5.18"
1414
env: WITH_JSON_XS=1 WITH_YAML_SYCK=1
15+
- perl: "5.18"
16+
env: WITH_JSON_XS=1 WITH_YAML_XS=1
1517
- perl: "5.18"
1618
env: WITH_JSON=1 WITH_YAML=1
1719
- perl: "5.18"
1820
env: WITH_YAML_SYCK=1
21+
- perl: "5.18"
22+
env: WITH_YAML_XS=1
1923
- perl: "5.18"
2024
env: WITH_YAML=1
2125
- perl: "5.18"
@@ -26,6 +30,7 @@ before_install:
2630
- '[ "$WITH_JSON_XS" = 1 ] && cpanm --quiet --notest JSON::XS || true'
2731
- '[ "$WITH_JSON" = 1 ] && cpanm --quiet --notest JSON || true'
2832
- '[ "$WITH_YAML_SYCK" = 1 ] && cpanm --quiet --notest YAML::Syck || true'
33+
- '[ "$WITH_YAML_XS" = 1 ] && cpanm --quiet --notest YAML::XS || true'
2934
- '[ "$WITH_YAML" = 1 ] && cpanm --quiet --notest YAML || true'
3035
- '[ "$WITH_IPC_RUN" = 1 ] && cpanm --quiet --notest IPC::Run || true'
3136
after_script:

pkwalify

+12-7
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ sub read_file {
7878
if (defined $parse_mod) {
7979
@try_order = ($parse_mod);
8080
} elsif ($file =~ m{\.json$}i) {
81-
@try_order = ('JSON::XS', 'JSON', 'YAML::Syck', 'YAML');
81+
@try_order = ('JSON::XS', 'JSON', 'YAML::Syck', 'YAML', 'YAML::XS');
8282
} else { # yaml or don't know
83-
@try_order = ('YAML::Syck', 'YAML', 'JSON::XS', 'JSON');
83+
@try_order = ('YAML::Syck', 'YAML', 'YAML::XS', 'JSON::XS', 'JSON');
8484
}
8585

8686
my @errors;
@@ -89,6 +89,10 @@ sub read_file {
8989
my @data = eval { YAML::Syck::LoadFile($file) };
9090
return @data if !$@;
9191
push @errors, $@;
92+
} elsif ($try eq 'YAML::XS' && eval { require YAML::XS; 1 }) {
93+
my @data = eval { YAML::XS::LoadFile($file) };
94+
return @data if !$@;
95+
push @errors, $@;
9296
} elsif ($try eq 'YAML' && eval { require YAML; 1 }) {
9397
my @data = eval { YAML::LoadFile($file) };
9498
return @data if !$@;
@@ -168,9 +172,9 @@ B<pkwalify> validates the data from I<datafile> (which may be a
168172
L<YAML> or L<JSON> file) against a schema defined with I<schemafile>
169173
(which also may be a YAML or JSON file).
170174
171-
It is required that either L<YAML> or L<YAML::Syck> is installed to
172-
parse YAML files, or either L<JSON> or L<JSON::XS> for JSON files.
173-
Or the module specified on the command-line.
175+
It is required that either L<YAML>, L<YAML::XS> or L<YAML::Syck> is
176+
installed to parse YAML files, or either L<JSON> or L<JSON::XS> for
177+
JSON files. Or the module specified on the command-line.
174178
175179
The program returns the number of errors found in the datafile. An
176180
exit status 0 means no errors.
@@ -186,7 +190,7 @@ Specify a schema file, either as YAML or JSON. Required.
186190
=item -m I<parse-mod>
187191
188192
Specify the YAML or JSON Perl module to use. Valid modules are:
189-
L<YAML>, L<YAML::Syck>, L<JSON> and L<JSON::XS>.
193+
L<YAML>, L<YAML::XS>, L<YAML::Syck>, L<JSON> and L<JSON::XS>.
190194
191195
=item -s
192196
@@ -208,7 +212,8 @@ Slaven Rezi
208212
209213
=head1 SEE ALSO
210214
211-
L<Kwalify>, L<kwalify(1)>, L<JSON>, L<JSON::XS>, L<YAML>, L<YAML::Syck>.
215+
L<Kwalify>, L<kwalify(1)>, L<JSON>, L<JSON::XS>, L<YAML>, L<YAML::XS>,
216+
L<YAML::Syck>.
212217
213218
=cut
214219

t/Kwalify.t

+12-7
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ BEGIN {
2222
}
2323
}
2424

25-
my $yaml_syck_tests;
25+
my $yaml_mod_tests;
2626
BEGIN {
27-
$yaml_syck_tests = 38;
28-
plan tests => 2 + $yaml_syck_tests + 60;
27+
$yaml_mod_tests = 38;
28+
plan tests => 2 + $yaml_mod_tests + 60;
2929
}
3030

3131
BEGIN {
@@ -37,16 +37,21 @@ $SIG{__WARN__} = sub { push @w, @_ };
3737

3838
use_ok('Schema::Kwalify');
3939

40+
my $can_yaml = (eval { require YAML::Syck; 1 } || eval { require YAML::XS; 1 });
41+
if ($can_yaml) {
42+
*YAML_Load = defined &YAML::Syck::Load ? \&YAML::Syck::Load : \&YAML::XS::Load;
43+
}
44+
4045
sub is_valid_yaml {
4146
my($schema, $document, $testname) = @_;
4247
local $Test::Builder::Level = $Test::Builder::Level+1;
43-
ok(validate(YAML::Syck::Load($schema), YAML::Syck::Load($document)), $testname);
48+
ok(validate(YAML_Load($schema), YAML_Load($document)), $testname);
4449
}
4550

4651
sub is_invalid_yaml {
4752
my($schema, $document, $errors, $testname) = @_;
4853
local $Test::Builder::Level = $Test::Builder::Level+1;
49-
ok(!eval { validate(YAML::Syck::Load($schema), YAML::Syck::Load($document)) }, $testname);
54+
ok(!eval { validate(YAML_Load($schema), YAML_Load($document)) }, $testname);
5055
for my $error (@$errors) {
5156
if (UNIVERSAL::isa($error, 'HASH')) {
5257
my($pattern, $testname) = @{$error}{qw(pattern testname)};
@@ -58,8 +63,8 @@ sub is_invalid_yaml {
5863
}
5964

6065
SKIP: {
61-
skip("Need YAML::Syck for tests", $yaml_syck_tests)
62-
if !eval { require YAML::Syck; 1 };
66+
skip("Need YAML::Syck, YAML or YAML::XS for tests", $yaml_mod_tests)
67+
if !$can_yaml;
6368

6469
my $schema01 = <<'EOF';
6570
type: seq

t/pkwalify.t

+11-10
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ require blib; # just to get blib's VERSION
2525
my $skip_warnings_test = $blib::VERSION < 1.01;
2626

2727
# Test cases with single documents
28-
my @yaml_syck_defs = (["schema05.yaml", "document05a.yaml", 1],
28+
my @yaml_mod_defs = (
29+
["schema05.yaml", "document05a.yaml", 1],
2930
["schema05.yaml", "document05b.yaml", 0],
30-
);
31+
);
3132

3233
# Test cases with multiple documents (by combining single documents)
3334
my %combined_document;
@@ -65,17 +66,17 @@ my %combined_document;
6566
}
6667
}
6768

68-
# Test cases for YAML/YAML::Syck (schema+document combinations)
69-
push @yaml_syck_defs, (
69+
# Test cases for YAML/YAML::XS/YAML::Syck (schema+document combinations)
70+
push @yaml_mod_defs, (
7071
[$combined_document{"invalid_schema"}, "document05a.yaml", 0],
7172
["schema05.yaml", $combined_document{"invalid_diff"}, 0],
7273
["schema05.yaml", $combined_document{"valid_same"}, 1],
7374
["schema05.yaml", $combined_document{"invalid_same"}, 0],
7475
);
7576

76-
my $can_yaml = (eval { require YAML::Syck; 1 } || eval { require YAML; 1 });
77+
my $can_yaml = (eval { require YAML::Syck; 1 } || eval { require YAML::XS; 1 } || eval { require YAML; 1 });
7778
if ($can_yaml) {
78-
*YAML_LoadFile = defined &YAML::Syck::LoadFile ? \&YAML::Syck::LoadFile : \&YAML::LoadFile;
79+
*YAML_LoadFile = defined &YAML::Syck::LoadFile ? \&YAML::Syck::LoadFile : defined &YAML::XS::LoadFile ? \&YAML::XS::LoadFile : \&YAML::LoadFile;
7980
}
8081
my $can_json = (eval { require JSON::XS; 1 } || eval { require JSON; 1 });
8182
if ($can_json) {
@@ -105,16 +106,16 @@ GetOptions("v!")
105106
or die "usage: $0 [-v]";
106107

107108
my $tests_per_case = 3;
108-
plan tests => 13 + $tests_per_case*(scalar(@yaml_syck_defs) + scalar(@json_defs));
109+
plan tests => 13 + $tests_per_case*(scalar(@yaml_mod_defs) + scalar(@json_defs));
109110

110111
my $script = "$FindBin::RealBin/../blib/script/pkwalify";
111112
my @cmd = ($^X, "-Mblib=$FindBin::RealBin/..", $script, "-s");
112113

113114
SKIP: {
114-
skip("Need YAML or YAML::Syck for tests", $tests_per_case*scalar(@yaml_syck_defs))
115+
skip("Need YAML, YAML::XS or YAML::Syck for tests", $tests_per_case*scalar(@yaml_mod_defs))
115116
if !$can_yaml;
116117

117-
for my $def (@yaml_syck_defs) {
118+
for my $def (@yaml_mod_defs) {
118119
any_test($def);
119120
}
120121
}
@@ -165,7 +166,7 @@ SKIP: {
165166
}
166167

167168
SKIP: {
168-
skip("Need YAML or YAML::Syck for tests", 4)
169+
skip("Need YAML, YAML::XS or YAML::Syck for tests", 4)
169170
if !$can_yaml;
170171

171172
my $schema_file = "schema05.yaml";

0 commit comments

Comments
 (0)