| 1 | package YAML::Tests; | ||
| 2 | use 5.005003; | ||
| 3 | use strict; | ||
| 4 | $YAML::Tests::VERSION = '0.06'; | ||
| 5 | |||
| 6 | sub yt_process { | ||
| 7 | my $class = shift; | ||
| 8 | my @ARGS = @_; | ||
| 9 | my $base_env_name = 'PERL_YAML_TESTS_BASE'; | ||
| 10 | my $base = $ENV{$base_env_name} || '.'; | ||
| 11 | |||
| 12 | die YAML::Tests->env_error_msg($base, $base_env_name) | ||
| 13 | unless | ||
| 14 | -d "$base/yaml-tests" and | ||
| 15 | -d "$base/t" and | ||
| 16 | -f "$base/lib/YAML/Tests.pm" and | ||
| 17 | -f "$base/Makefile.PL"; | ||
| 18 | |||
| 19 | chdir($base) or die "Can't chdir to '$base'"; | ||
| 20 | |||
| 21 | my ($commands, $modules, $options, $tests) = ([], [], [], []); | ||
| 22 | for (@ARGS) { | ||
| 23 | push @{( | ||
| 24 | /^--/ ? $commands : | ||
| 25 | /^-M/ ? $modules : | ||
| 26 | /^-/ ? $options : $tests | ||
| 27 | )}, $_; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $command; | ||
| 31 | if (@$commands) { | ||
| 32 | $command = $commands->[0]; | ||
| 33 | if ($command =~ /^--(list|benchmark|version)$/) { | ||
| 34 | my $method = "handle_$1"; | ||
| 35 | $class->$method($options, $tests); | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | die "Unknown command: '$command'"; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $module; | ||
| 42 | if (@$modules and $modules->[0] =~ /^-M([\w\:]+)$/) { | ||
| 43 | $module = $1; | ||
| 44 | } | ||
| 45 | $module ||= $ENV{PERL_YAML_TESTS_MODULE}; | ||
| 46 | die YAML::Tests->usage_msg($base, $base_env_name) | ||
| 47 | unless $module; | ||
| 48 | eval "require $module; 1" | ||
| 49 | or die $@; | ||
| 50 | die "Error: $module does not support the YAML Dump/Load API\n" | ||
| 51 | unless | ||
| 52 | $module->can('Load') and | ||
| 53 | $module->can('Dump'); | ||
| 54 | $ENV{PERL_YAML_TESTS_MODULE} = $module; | ||
| 55 | |||
| 56 | my $file_list = join ' ', | ||
| 57 | (@$tests) ? do { | ||
| 58 | map { | ||
| 59 | s/.*yaml-tests[\/\\]//; | ||
| 60 | "yaml-tests/$_"; | ||
| 61 | } @$tests; | ||
| 62 | } : "yaml-tests"; | ||
| 63 | |||
| 64 | my $option_list = join ' ', | ||
| 65 | (@$options) ? (@$options, '') : (); | ||
| 66 | |||
| 67 | my $command = "prove $option_list$file_list"; | ||
| 68 | system($command) == 0 | ||
| 69 | or die("Command failed:\n $command\n"); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub handle_list { | ||
| 73 | opendir DIR, "yaml-tests" | ||
| 74 | or die "Can't open yaml-tests directory"; | ||
| 75 | while (my $file = readdir(DIR)) { | ||
| 76 | next unless $file =~ /\.t$/; | ||
| 77 | print "$file\n"; | ||
| 78 | } | ||
| 79 | closedir DIR; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub handle_benchmark { | ||
| 83 | my $class = shift; | ||
| 84 | require YAML::Tests::Benchmark; | ||
| 85 | YAML::Tests::Benchmark->run(@_); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub handle_version { | ||
| 89 | my @modules; | ||
| 90 | for my $module (qw(YAML YAML::LibYAML YAML::Syck YAML::Tiny)) { | ||
| 91 | eval "require $module; 1" or next; | ||
| 92 | no strict 'refs'; | ||
| 93 | push @modules, { | ||
| 94 | name => $module, | ||
| 95 | version => ${$module . "::VERSION"}, | ||
| 96 | }; | ||
| 97 | } | ||
| 98 | print <<"..."; | ||
| 99 | This is the "yt" program, version $main::VERSION. | ||
| 100 | |||
| 101 | It uses the Perl module YAML::Tests, version $YAML::Tests::VERSION. | ||
| 102 | |||
| 103 | The following known YAML Perl modules are installed on your system: | ||
| 104 | |||
| 105 | ... | ||
| 106 | |||
| 107 | for my $module (@modules) { | ||
| 108 | print " - $module->{name}, version $module->{version}\n"; | ||
| 109 | } | ||
| 110 | |||
| 111 | print "\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub env_error_msg { | ||
| 115 | my ($class, $base, $base_env_name) = @_; | ||
| 116 | return <<"..."; | ||
| 117 | '$base' is not a YAML-Tests directory. | ||
| 118 | |||
| 119 | You need to get YAML-Tests from CPAN or its SVN repository. If you get a | ||
| 120 | tarball you need to untar it into a directory. | ||
| 121 | |||
| 122 | Next you should cd to that directory or set the $base_env_name | ||
| 123 | environment variable to point to the directory. | ||
| 124 | |||
| 125 | The SVN location is: http://svn.kwiki.org/ingy/YAML-Tests/ | ||
| 126 | |||
| 127 | ... | ||
| 128 | } | ||
| 129 | |||
| 130 | sub usage_msg { | ||
| 131 | return <<"..."; | ||
| 132 | Usage: | ||
| 133 | yt [prove-options] [-MYAML::Module] [list-of-tests] | ||
| 134 | yt --list | ||
| 135 | yt --benchmark | ||
| 136 | yt --version | ||
| 137 | |||
| 138 | Examples: | ||
| 139 | yt | ||
| 140 | yt -MYAML::LibYAML | ||
| 141 | yt -v -MYAML::Tiny dump.t load.t | ||
| 142 | PERL_YAML_TESTS_MODULE=YAML::Syck yt | ||
| 143 | |||
| 144 | See Also: | ||
| 145 | perldoc yt | ||
| 146 | |||
| 147 | ... | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | =head1 NAME | ||
| 153 | |||
| 154 | YAML::Tests - Common Test Suite for Perl YAML Implementations | ||
| 155 | |||
| 156 | =head1 SYNOPSIS | ||
| 157 | |||
| 158 | > yt -MYAML::Foo # Run all YAML Tests against YAML::Foo implementation | ||
| 159 | |||
| 160 | or: | ||
| 161 | |||
| 162 | > export PERL_YAML_TESTS_MODULE=YAML::Foo | ||
| 163 | > yt | ||
| 164 | |||
| 165 | =head1 DESCRIPTION | ||
| 166 | |||
| 167 | YAML-Tests defines a number of implementation independent tests that can | ||
| 168 | be used to test various YAML modules. | ||
| 169 | |||
| 170 | There are two ways to use YAML-Tests. If you are the author of a Perl | ||
| 171 | YAML module, you can add the line: | ||
| 172 | |||
| 173 | use_yaml_tests; | ||
| 174 | |||
| 175 | to your C<Makefile.PL>. This will copy the tests from YAML::Tests into | ||
| 176 | your module's test area. | ||
| 177 | |||
| 178 | If you are Just Another Perl Hacker, YAML-Tests installs a command line tool | ||
| 179 | called C<yt> to run the YAML tests against a specific module. Like this: | ||
| 180 | |||
| 181 | > yt -MYAML::Syck | ||
| 182 | |||
| 183 | YAML::Tests provides a common test suite against which to test Perl YAML | ||
| 184 | modules. It also provides a Module::Install component | ||
| 185 | (C<use_yaml_tests>) to make it simple for YAML module authors to include | ||
| 186 | the tests in their distributions. See L<Module::Install::YAML::Tests> | ||
| 187 | for more information about this feature. | ||
| 188 | |||
| 189 | This module installs a command line tool called C<yt> which can be used | ||
| 190 | to run the YAML tests against various implementations. See L<yt> for more | ||
| 191 | information. | ||
| 192 | |||
| 193 | =head1 TYPES OF TESTS | ||
| 194 | |||
| 195 | YAML::Tests provides tests that should pass on any YAML implementation | ||
| 196 | that provides a C<Dump> and C<Load> function interface. These are likely | ||
| 197 | not the only tests that an implementation should have. They are intended | ||
| 198 | to be a common subset. | ||
| 199 | |||
| 200 | This section describes the types of tests that are provided. | ||
| 201 | |||
| 202 | =head2 NYN Roundtripping | ||
| 203 | |||
| 204 | "NYN Roundtripping" is a YAML term that means Native->YAML->Native. In | ||
| 205 | our case "Native" means "Perl". These tests take various Perl objects, | ||
| 206 | Dump them to YAML and then Load them back into Perl. The original and | ||
| 207 | the clone Perl objects are compared for equivalence. | ||
| 208 | |||
| 209 | This is a very common type of test. | ||
| 210 | |||
| 211 | =head2 YNY Roundtripping | ||
| 212 | |||
| 213 | "YNY" means YAML->Native->YAML. Load a YAML stream to Perl and Dump it | ||
| 214 | back to YAML. Test if the YAML streams match. | ||
| 215 | |||
| 216 | There are fewer of these tests because there are usually variations in | ||
| 217 | how a Dumper implementation will actually Dump a given object. Still we | ||
| 218 | can cover the simple basics. | ||
| 219 | |||
| 220 | =head2 Y2N Testing | ||
| 221 | |||
| 222 | Load a given YAML stream and see if it produces the expected Perl | ||
| 223 | objects. | ||
| 224 | |||
| 225 | This is different from NYN because we are testing YAML streams that are | ||
| 226 | not produced by a YAML Dumper. This is where we can test the edge cases | ||
| 227 | that might be produced by a human editing YAML. | ||
| 228 | |||
| 229 | =head2 YAML Loader Errors | ||
| 230 | |||
| 231 | Invalid YAML should cause a Loader to throw an error. These tests Load | ||
| 232 | various invalid YAML streams and make sure that an error is thrown. | ||
| 233 | |||
| 234 | =head2 API Testing | ||
| 235 | |||
| 236 | Some tests make sure that all the C<Dump> and C<Load> functions follow | ||
| 237 | the same API. | ||
| 238 | |||
| 239 | =head1 YAML IMPLEMENTATIONS | ||
| 240 | |||
| 241 | Currently there are 4 YAML Implementations on CPAN: | ||
| 242 | |||
| 243 | =head2 YAML.pm | ||
| 244 | |||
| 245 | This is the original YAML module written in 2001 by Ingy döt Net. It is | ||
| 246 | pure Perl. | ||
| 247 | |||
| 248 | =head2 YAML::Syck | ||
| 249 | |||
| 250 | This wrapper of Why The Lucky Stiff's libsyck, was written by Audrey | ||
| 251 | Tang in 2005. YAML::Syck is almost entirely written in C, so it is | ||
| 252 | fast. The libsyck library was written in 2003 and targeted at the | ||
| 253 | YAML 1.0 spec. | ||
| 254 | |||
| 255 | =head2 YAML::Tiny | ||
| 256 | |||
| 257 | YAML::Tiny is a pure Perl module written by Adam Kennedy in 2006. It is | ||
| 258 | an attempt to write a YAML implementation that is as small as possible. | ||
| 259 | It does this by choosing to only deal with a subset of the YAML | ||
| 260 | language. It attempts to support the subset of YAML that is used by | ||
| 261 | popular Perl projects like CPAN and SVK. | ||
| 262 | |||
| 263 | =head2 YAML::LibYAML | ||
| 264 | |||
| 265 | This wrapper of Kirill Siminov's libyaml (2005) is a pure C module | ||
| 266 | written by Ingy döt Net in 2007. The libyaml library was targeted at | ||
| 267 | the current YAML 1.1 spec. It was written to match the spec exactly. At | ||
| 268 | this point it has no known bugs. It is meant to eventually become the | ||
| 269 | new YAML.pm codebase. | ||
| 270 | |||
| 271 | =head1 AUTHOR | ||
| 272 | |||
| 273 | Ingy döt Net <ingy@cpan.org> | ||
| 274 | |||
| 275 | =head1 COPYRIGHT | ||
| 276 | |||
| 277 | Copyright (c) 2007. Ingy döt Net. All rights reserved. | ||
| 278 | |||
| 279 | This program is free software; you can redistribute it and/or modify it | ||
| 280 | under the same terms as Perl itself. | ||
| 281 | |||
| 282 | See http://www.perl.com/perl/misc/Artistic.html | ||
| 283 | |||
| 284 | =cut |