Skip Menu |

This queue is for tickets about the YAML-Tiny CPAN distribution.

Report information
The Basics
Id: 89416
Status: resolved
Priority: 0/
Queue: YAML-Tiny

People
Owner: Nobody in particular
Requestors: jkeenan [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Boost test coverage
The patch attached increases the cover which YAML-Tiny's test suite provides to its source code. At the start of my work on YAML-Tiny, coverage, as measured by Devel::Cover, was: file stmt bran cond sub blib/lib/YAML/Tiny.pm 88.6 77.7 73.0 92.6 At the point represented by the patch, coverage has increased to: file stmt bran cond sub blib/lib/YAML/Tiny.pm 91.3 83.0 73.0 100.0 There are areas in the code where, if it were cleaned up, coverage could probably be improved further. But at the very least, we now have coverage of all subroutines in lib/YAML/Tiny.pm. If there is a particular style for writing tests for YAML-Tiny to which all tests much conform, please let me know. You can ping me (kid51) on #toolchain when you see me on channel. Thank you very much. Jim Keenan
Subject: boost_coverage_1.diff
diff --git a/t/19_errors.t b/t/19_errors.t index a8a7b43..11223cb 100644 --- a/t/19_errors.t +++ b/t/19_errors.t @@ -10,7 +10,7 @@ BEGIN { use File::Spec::Functions ':ALL'; use t::lib::Test; -use Test::More tests => 20; +use Test::More tests => 25; use YAML::Tiny (); my $FEATURE = 'does not support a feature'; @@ -71,3 +71,46 @@ END_YAML yaml_error( <<'END_YAML', $PLAIN ); foo: `perl -V` END_YAML + +##################################################################### + +# tests for read() +{ + eval { YAML::Tiny->read(); }; + like(YAML::Tiny->errstr, qr/You did not specify a file name/, + "Got expected error: no filename provided to read()"); + $YAML::Tiny::errstr = ''; +} + +{ + my $file = catfile( test_data_directory(), 'nonexistent.yml' ); + eval { YAML::Tiny->read($file); }; + like(YAML::Tiny->errstr, qr/File '$file' does not exist/, + "Got expected error: nonexistent filename provided to read()"); + $YAML::Tiny::errstr = ''; +} + +{ + my $file = catfile( test_data_directory(), '/' ); + eval { YAML::Tiny->read($file); }; + like(YAML::Tiny->errstr, qr/'$file' is a directory, not a file/, + "Got expected error: directory provided to read()"); + $YAML::Tiny::errstr = ''; +} + +# tests for read_string() +{ + eval { YAML::Tiny->read_string(); }; + like(YAML::Tiny->errstr, qr/Did not provide a string to load/, + "Got expected error: no string provided to read_string()"); + $YAML::Tiny::errstr = ''; +} + +{ + my $str = join("\n" => ('---', '- foo', '---', '- bar', '---')); + eval { YAML::Tiny->read_string($str); }; + like(YAML::Tiny->errstr, qr/Stream does not end with newline character/, + "Got expected error: stream did not end with newline"); + $YAML::Tiny::errstr = ''; +} + diff --git a/t/24_misc.t b/t/24_misc.t new file mode 100644 index 0000000..81aade5 --- /dev/null +++ b/t/24_misc.t @@ -0,0 +1,131 @@ +# Testing of statements in the YAML-Tiny codebase not exercised elsewhere. +use strict; +use warnings; + +BEGIN { + $| = 1; + $^W = 1; +} + +use File::Spec::Functions ':ALL'; +use t::lib::Test; +use Test::More qw(no_plan); # tests => 24;; +use YAML::Tiny; +use File::Temp qw(tempfile); + +# tests for read() +{ + my ($obj, $str, $yaml, $file); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $file = catfile( test_data_directory(), 'one.yml' ); + eval { $yaml = $obj->read($file); }; + ok(! YAML::Tiny->errstr, "\$obj->read: No error, as expected"); + isa_ok( $yaml, 'YAML::Tiny' ); + $YAML::Tiny::errstr = ''; +} + +{ + my ($obj, $str, $yaml, $file); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $file = catfile( test_data_directory(), 'nonexistent.yml' ); + eval { $obj->read($file); }; + like(YAML::Tiny->errstr, qr/File '$file' does not exist/, + "Got expected error: nonexistent filename provided to read()"); + $YAML::Tiny::errstr = ''; +} + +# tests for read_string() +{ + my ($obj, $str, $yaml); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $str = join("\n" => ('---', '- foo', '---', '- bar', '---')); + $str .= "\n"; + eval { $yaml = $obj->read_string($str); }; + ok(! YAML::Tiny->errstr, "\$obj->read_str: No error, as expected"); + isa_ok( $yaml, 'YAML::Tiny' ); + $YAML::Tiny::errstr = ''; +} + +{ + my ($obj, $str, $yaml); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $str = "--\n"; + eval { $yaml = $obj->read_string($str); }; + like( + YAML::Tiny->errstr, + qr/YAML::Tiny found illegal characters in plain scalar/, + "Illegal characters in plain scalar" + ); + $YAML::Tiny::errstr = ''; +} + +{ + my ($obj, $str, $yaml); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $str = join("\n" => ('---', '- foo', '---', '- bar', '---')); + eval { $yaml = $obj->read_string($str); }; + like(YAML::Tiny->errstr, qr/Stream does not end with newline character/, + "Got expected error: stream did not end with newline"); + $YAML::Tiny::errstr = ''; +} + +{ + my ($obj, $str, $yaml); + $obj = YAML::Tiny->new(); + isa_ok( $obj, 'YAML::Tiny' ); + $str = join("\n" => ('# comment', '---', '- foo', '---', '- bar', '---')); + $str .= "\n"; + eval { $yaml = $obj->read_string($str); }; + ok(! YAML::Tiny->errstr, "\$obj->read_str: No error, as expected"); + isa_ok( $yaml, 'YAML::Tiny' ); + $YAML::Tiny::errstr = ''; +} + +{ + my $scalar = 'this is a string'; + my $arrayref = [ 1 .. 5 ]; + my $hashref = { alpha => 'beta', gamma => 'delta' }; + + my $yamldump = YAML::Tiny::Dump( $scalar, $arrayref, $hashref ); + my @yamldocsloaded = YAML::Tiny::Load($yamldump); + is_deeply( + [ @yamldocsloaded ], + [ $scalar, $arrayref, $hashref ], + "Functional interface: Dump to Load roundtrip works as expected" + ); +} + +{ + my $scalar = 'this is a string'; + my $arrayref = [ 1 .. 5 ]; + my $hashref = { alpha => 'beta', gamma => 'delta' }; + + my ($fh, $filename) = tempfile; + + my $rv = YAML::Tiny::DumpFile( + $filename, $scalar, $arrayref, $hashref); + ok($rv, "DumpFile returned true value"); + + my @yamldocsloaded = YAML::Tiny::LoadFile($filename); + is_deeply( + [ @yamldocsloaded ], + [ $scalar, $arrayref, $hashref ], + "Functional interface: DumpFile to LoadFile roundtrip works as expected" + ); +} + +{ + my $str = "This is not real YAML"; + my @yamldocsloaded; + eval { @yamldocsloaded = YAML::Tiny::Load("$str\n"); }; + like(YAML::Tiny->errstr, + qr/YAML::Tiny failed to classify line '$str'/, + "Correctly failed to load non-YAML string" + ); +} +
Awesome! Thank you. I've applied it (with your name on it) and pushed it up to PTG. I don't feel attached to a particular style. (I'd be happy to modernize it a bit as long as we work back to 5.8.1) It looks like you copied the style relatively faithfully.