1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
package t::lib::Test;
use strict;
use Exporter ();
use File::Spec ();
use Test::More ();
use vars qw{@ISA @EXPORT};
BEGIN {
@ISA = qw{ Exporter };
@EXPORT = qw{
tests yaml_ok yaml_error slurp load_ok
test_data_directory
};
}
# Do we have the authorative YAML to test against
eval {
require YAML;
# This doesn't currently work, but is documented to.
# So if it ever turns up, use it.
$YAML::UseVersion = 1;
};
my $HAVE_YAMLPM = !! (
$YAML::VERSION
and
$YAML::VERSION >= 0.66
);
sub have_yamlpm { $HAVE_YAMLPM }
# Do we have YAML::Perl to test against?
eval {
require YAML::Perl;
};
my $HAVE_YAMLPERL = !! (
$YAML::Perl::VERSION
and
$YAML::Perl::VERSION >= 0.02
);
sub have_yamlperl { $HAVE_YAMLPERL }
# Do we have YAML::Syck to test against?
eval {
require YAML::Syck;
};
my $HAVE_SYCK = !! (
$YAML::Syck::VERSION
and
$YAML::Syck::VERSION >= 1.05
);
sub have_syck { $HAVE_SYCK }
# Do we have YAML::XS to test against?
eval {
require YAML::XS;
};
my $HAVE_XS = !! (
$YAML::XS::VERSION
and
$YAML::XS::VERSION >= 0.29
);
sub have_xs{ $HAVE_XS }
# 22 tests per call to yaml_ok
# 4 tests per call to load_ok
sub tests {
return ( tests => count(@_) );
}
sub test_data_directory {
return File::Spec->catdir( 't', 'data' );
}
sub count {
my $yaml_ok = shift || 0;
my $load_ok = shift || 0;
my $single = shift || 0;
my $count = $yaml_ok * 38 + $load_ok * 4 + $single;
return $count;
}
sub yaml_ok {
my $string = shift;
my $object = shift;
my $name = shift || 'unnamed';
my %options = ( @_ );
bless $object, 'YAML::Tiny';
# If YAML itself is available, test with it
SKIP: {
unless ( $HAVE_YAMLPM ) {
Test::More::skip( "Skipping YAML.pm, not available for testing", 7 );
}
if ( $options{noyamlpm} ) {
Test::More::skip( "Skipping YAML.pm for known-broken feature", 7 );
}
# Test writing with YAML.pm
my $yamlpm_out = eval { YAML::Dump( @$object ) };
Test::More::is( $@, '', "$name: YAML.pm saves without error" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 4 ) if $@;
Test::More::ok(
!!(defined $yamlpm_out and ! ref $yamlpm_out),
"$name: YAML.pm serializes correctly",
);
my @yamlpm_round = eval { YAML::Load( $yamlpm_out ) };
Test::More::is( $@, '', "$name: YAML.pm round-trips without error" );
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
my $round = bless [ @yamlpm_round ], 'YAML::Tiny';
Test::More::is_deeply( $round, $object, "$name: YAML.pm round-trips correctly" );
}
# Test reading with YAML.pm
my $yamlpm_copy = $string;
my @yamlpm_in = eval { YAML::Load( $yamlpm_copy ) };
Test::More::is( $@, '', "$name: YAML.pm loads without error" );
Test::More::is( $yamlpm_copy, $string, "$name: YAML.pm does not modify the input string" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 1 ) if $@;
Test::More::is_deeply( \@yamlpm_in, $object, "$name: YAML.pm parses correctly" );
}
}
# If YAML::Syck itself is available, test with it
SKIP: {
unless ( $HAVE_SYCK ) {
Test::More::skip( "Skipping YAML::Syck, not available for testing", 7 );
}
if ( $options{nosyck} ) {
Test::More::skip( "Skipping YAML::Syck for known-broken feature", 7 );
}
unless ( @$object == 1 ) {
Test::More::skip( "Skipping YAML::Syck for unsupported feature", 7 );
}
# Test writing with YAML::Syck
my $syck_out = eval { YAML::Syck::Dump( @$object ) };
Test::More::is( $@, '', "$name: YAML::Syck saves without error" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 4 ) if $@;
Test::More::ok(
!!(defined $syck_out and ! ref $syck_out),
"$name: YAML::Syck serializes correctly",
);
my @syck_round = eval { YAML::Syck::Load( $syck_out ) };
Test::More::is( $@, '', "$name: YAML::Syck round-trips without error" );
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
my $round = bless [ @syck_round ], 'YAML::Tiny';
Test::More::is_deeply( $round, $object, "$name: YAML::Syck round-trips correctly" );
}
# Test reading with YAML::Syck
my $syck_copy = $string;
my @syck_in = eval { YAML::Syck::Load( $syck_copy ) };
Test::More::is( $@, '', "$name: YAML::Syck loads without error" );
Test::More::is( $syck_copy, $string, "$name: YAML::Syck does not modify the input string" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 1 ) if $@;
Test::More::is_deeply( \@syck_in, $object, "$name: YAML::Syck parses correctly" );
}
}
# If YAML::XS itself is available, test with it
SKIP: {
unless ( $HAVE_XS ) {
Test::More::skip( "Skipping YAML::XS, not available for testing", 7 );
}
if ( $options{noxs} ) {
Test::More::skip( "Skipping YAML::XS for known-broken feature", 7 );
}
# Test writing with YAML::XS
my $xs_out = eval { YAML::XS::Dump( @$object ) };
Test::More::is( $@, '', "$name: YAML::XS saves without error" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 4 ) if $@;
Test::More::ok(
!!(defined $xs_out and ! ref $xs_out),
"$name: YAML::XS serializes correctly",
);
my @xs_round = eval { YAML::XS::Load( $xs_out ) };
Test::More::is( $@, '', "$name: YAML::XS round-trips without error" );
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
my $round = bless [ @xs_round ], 'YAML::Tiny';
Test::More::is_deeply( $round, $object, "$name: YAML::XS round-trips correctly" );
}
# Test reading with YAML::XS
my $xs_copy = $string;
my @xs_in = eval { YAML::XS::Load( $xs_copy ) };
Test::More::is( $@, '', "$name: YAML::XS loads without error" );
Test::More::is( $xs_copy, $string, "$name: YAML::XS does not modify the input string" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 1 ) if $@;
Test::More::is_deeply( \@xs_in, $object, "$name: YAML::XS parses correctly" );
}
}
# If YAML::Perl is available, test with it
SKIP: {
unless ( $HAVE_YAMLPERL ) {
Test::More::skip( "Skipping YAML::Perl, not available for testing", 7 );
}
if ( $options{noyamlperl} ) {
Test::More::skip( "Skipping YAML::Perl for known-broken feature", 7 );
}
# Test writing with YAML.pm
my $yamlperl_out = eval { YAML::Perl::Dump( @$object ) };
Test::More::is( $@, '', "$name: YAML::Perl saves without error" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 4 ) if $@;
Test::More::ok(
!!(defined $yamlperl_out and ! ref $yamlperl_out),
"$name: YAML::Perl serializes correctly",
);
my @yamlperl_round = eval { YAML::Perl::Load( $yamlperl_out ) };
Test::More::is( $@, '', "$name: YAML::Perl round-trips without error" );
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
my $round = bless [ @yamlperl_round ], 'YAML::Tiny';
Test::More::is_deeply( $round, $object, "$name: YAML::Perl round-trips correctly" );
}
# Test reading with YAML::Perl
my $yamlperl_copy = $string;
my @yamlperl_in = eval { YAML::Perl::Load( $yamlperl_copy ) };
Test::More::is( $@, '', "$name: YAML::Perl loads without error" );
Test::More::is( $yamlperl_copy, $string, "$name: YAML::Perl does not modify the input string" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 1 ) if $@;
Test::More::is_deeply( \@yamlperl_in, $object, "$name: YAML::Perl parses correctly" );
}
}
# Does the string parse to the structure
my $yaml_copy = $string;
my $yaml = eval { YAML::Tiny->read_string( $yaml_copy ); };
Test::More::is( $@, '', "$name: YAML::Tiny parses without error" );
Test::More::is( $yaml_copy, $string, "$name: YAML::Tiny does not modify the input string" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
Test::More::isa_ok( $yaml, 'YAML::Tiny' );
Test::More::is_deeply( $yaml, $object, "$name: YAML::Tiny parses correctly" );
}
# Does the structure serialize to the string.
# We can't test this by direct comparison, because any
# whitespace or comments would be lost.
# So instead we parse back in.
my $output = eval { $object->write_string };
Test::More::is( $@, '', "$name: YAML::Tiny serializes without error" );
SKIP: {
Test::More::skip( "Shortcutting after failure", 5 ) if $@;
Test::More::ok(
!!(defined $output and ! ref $output),
"$name: YAML::Tiny serializes correctly",
);
my $roundtrip = eval { YAML::Tiny->read_string( $output ) };
Test::More::is( $@, '', "$name: YAML::Tiny round-trips without error" );
Test::More::skip( "Shortcutting after failure", 2 ) if $@;
Test::More::isa_ok( $roundtrip, 'YAML::Tiny' );
Test::More::is_deeply( $roundtrip, $object, "$name: YAML::Tiny round-trips correctly" );
# Testing the serialization
Test::More::skip( "Shortcutting perfect serialization tests", 1 ) unless $options{serializes};
Test::More::is( $output, $string, 'Serializes ok' );
}
# Return true as a convenience
return 1;
}
sub yaml_error {
my $string = shift;
my $like = shift;
my $yaml = YAML::Tiny->read_string( $string );
Test::More::is( $yaml, undef, '->read_string returns undef' );
Test::More::ok( YAML::Tiny->errstr =~ /$like/, "Got expected error" );
# NOTE: like() gives better diagnostics (but requires 5.005)
# Test::More::like( $@, qr/$_[0]/, "YAML::Tiny throws expected error" );
}
sub slurp {
my $file = shift;
local $/ = undef;
open( FILE, " $file" ) or die "open($file) failed: $!";
binmode( FILE, $_[0] ) if @_ > 0 && $] > 5.006;
# binmode(FILE); # disable perl's BOM interpretation
my $source = <FILE>;
close( FILE ) or die "close($file) failed: $!";
$source;
}
sub load_ok {
my $name = shift;
my $file = shift;
my $size = shift;
Test::More::ok( -f $file, "Found $name" );
Test::More::ok( -r $file, "Can read $name" );
my $content = slurp( $file );
Test::More::ok( (defined $content and ! ref $content), "Loaded $name" );
Test::More::ok( ($size < length $content), "Content of $name larger than $size bytes" );
return $content;
}
1;
|