Test::YAML::Valid - Test for valid YAML


Test-YAML-Valid documentation Contained in the Test-YAML-Valid distribution.

Index


Code Index:

NAME

Top

Test::YAML::Valid - Test for valid YAML

VERSION

Top

Version 0.04

SYNOPSIS

Top

This module lets you easily test the validity of YAML:

    use Test::More tests => 3;
    use Test::YAML::Valid;

    yaml_string_ok(YAML::Dump({foo => 'bar'}), 'YAML generates good YAML?');
    yaml_string_ok('this is not YAML, is it?', 'This one will fail');
    yaml_file_ok('/path/to/some/YAML', '/path/to/some/YAML is YAML');
    yaml_files_ok('/path/to/YAML/files/*', 'all YAML files are valid');

You can also test with YAML::Syck instead of YAML by passing -Syck in the import list:

    use Test::YAML::Valid qw(-Syck);
    yaml_string_ok(...); # uses YAML::Syck::Load instead of YAML::Load

It's up to you to make sure you have YAML::Syck if you specify the -Syck option, since it's an optional prerequisite to this module. If it's requested but not found, a warning will be issued and YAML will be used instead.

As of version 0.04, you can use any module you want in the same way; -Tiny for YAML::Tiny and -XS for YAML::XS.

EXPORT

Top

* yaml_string_ok
* yaml_file_ok
* yaml_files_ok

FUNCTIONS

Top

yaml_string_ok($yaml, [$message])

Test will pass if $yaml contains valid YAML (according to YAML.pm) and fail otherwise. Returns the result of loading the YAML.

yaml_file_ok($filename, [$message])

Test will pass if $filename is a valid YAML file (according to YAML.pm) and fail otherwise. Returns the result of loading the YAML.

yaml_files_ok($file_glob_string, [$message])

Test will pass if all files matching the glob $file_glob_string contain valid YAML. If a file is not valid, the test will fail and no further files will be examined.

Returns a list of all loaded YAML;

AUTHOR

Top

Jonathan Rockway, <jrockway at cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-test-yaml-valid at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-YAML-Valid. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Test::YAML::Valid

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Test-YAML-Valid

* CPAN Ratings

http://cpanratings.perl.org/d/Test-YAML-Valid

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-YAML-Valid

* Search CPAN

http://search.cpan.org/dist/Test-YAML-Valid

ACKNOWLEDGEMENTS

Top

Stevan Little <stevan.little@iinteractive.com> contributed yaml_files_ok and some more tests.

COPYRIGHT & LICENSE

Top


Test-YAML-Valid documentation Contained in the Test-YAML-Valid distribution.
package Test::YAML::Valid;

use warnings;
use strict;
use Test::Builder;
use base 'Exporter';
use Carp qw(confess);

our @EXPORT_OK = qw(yaml_string_ok yaml_file_ok yaml_files_ok);
our @EXPORT = @EXPORT_OK;

sub import {
    my @_import = @_;

    # sort the import list into attempts to load alternate YAML
    # parsers ("requests"), and the actual import list to pass to
    # Exporter.
    my @requests;
    my @import;
    for my $elt (@_import){
        if( $elt =~ /^-([A-Za-z::]+)$/ ){
            push @requests, $1;
        }
        else {
            push @import, $elt;
        }
    }

    confess 'You can only specify one YAML module to use; you specified '. scalar @requests
      if @requests > 1;

    my $request_ok = 0;
    my $request = $requests[0];
    if($request){
        eval {
            eval "use YAML::$request qw(Load LoadFile); 1" or die;
            $request_ok = 1;
        };
    }

    if(!$request_ok){
	require YAML;
	eval "use YAML qw(Load LoadFile)";
	Test::Builder->new->diag("Falling back to YAML from YAML::$request")
	    if $request;
    }

    __PACKAGE__->export_to_level(1, @import);
}

our $VERSION = '0.04';

# workaround for YAML::Syck -- it doesn't parse report errors!!!!!
sub _is_undef_yaml($){
    my $yaml = shift;
    return if !defined $yaml;
    return 1 if $yaml =~ /^(?:---(?:\s+~?)?\s+)+$/m;
    # XXX: ... should be OK:
    #/^(?:---)?(?: ~)?\n+(?:[.][.][.]\n+)?$/;

    return 0;
}

sub _is_yaml($$){
    return (defined $_[0] || _is_undef_yaml($_[1]));
}

sub yaml_string_ok($;$) {
    my $yaml = shift;
    my $msg  = shift;
    my $result;

    eval {
	$result = Load($yaml);
    };

    my $test = Test::Builder->new();
    $test->ok(!$@ && _is_yaml($result,$yaml), $msg);
    return $result;
}

sub yaml_file_ok($;$) {
    my $file = shift;
    my $msg  = shift;
    my $result;
    my $yaml;
    eval {
	$result = LoadFile($file);
	if(!defined $result){ # special case for YAML::Syck
	    open my $fh, '<', $file or die "Can't open $file: $!";
	    $yaml = do {local $/; <$fh> };
	    close $fh;
	}
    };

    my $test = Test::Builder->new();
    $msg = "$file contains valid YAML" unless $msg;
    $test->ok(!$@ && _is_yaml($result,$yaml), $msg);
    return $result;
}

sub yaml_files_ok($;$) {
    my $file_glob = shift;
    my $msg       = shift;
    my @results;
    my $result;

    my $test = Test::Builder->new();
    $msg = "$file_glob contains valid YAML files" unless $msg;
    foreach my $file (glob($file_glob)) {
	my $yaml = "";
	next if -d $file; # skip directories
        eval {
	    $result = LoadFile($file);
	    if(!defined $result){ # special case for YAML::Syck
		open my $fh, '<', $file or die "Can't open $file: $!";
		$yaml = do {local $/; <$fh> };
		close $fh;
	    }
            push @results, $result;
        };
        if ($@ || !_is_yaml($result,$yaml)) {
            $test->ok(0, $msg);
            $test->diag("  Could not load file: $file.");
            return;
        }
    }

    $test->ok(1, $msg);
    return \@results;
}


1; # End of Test::YAML::Valid