1#!/usr/bin/perl -w 2 3# This is a test of the fake YAML dumper implemented by EUMM: 4# ExtUtils::MM_Any::metafile_file 5 6BEGIN { 7 unshift @INC, 't/lib'; 8} 9 10use strict; 11use warnings; 12use Test::More tests => 16; 13 14require ExtUtils::MM_Any; 15 16my $mm = bless {}, 'ExtUtils::MM_Any'; 17 18{ 19 my @meta = ( a => 1, b => 2 ); 20 my $expected = <<YAML; 21--- #YAML:1.0 22a: 1 23b: 2 24YAML 25 26 is($mm->metafile_file(@meta), $expected, "dump for flat hashes works ok"); 27} 28 29{ 30 my @meta = ( k1 => 'some key and value', k2 => undef, k3 => 1 ); 31 my $expected = <<YAML; 32--- #YAML:1.0 33k1: some key and value 34k2: ~ 35k3: 1 36YAML 37 38 is($mm->metafile_file(@meta), $expected, "dumping strings and undefs is ok"); 39} 40 41{ 42 my @meta = ( a => 1, b => 2, h => { hh => 1 } ); 43 my $expected = <<YAML; 44--- #YAML:1.0 45a: 1 46b: 2 47h: 48 hh: 1 49YAML 50 51 is($mm->metafile_file(@meta), $expected, "dump for nested hashes works ok"); 52} 53 54{ 55 my @meta = ( a => 1, b => 2, h => { h1 => 'x', h2 => 'z' } ); 56 my $expected = <<YAML; 57--- #YAML:1.0 58a: 1 59b: 2 60h: 61 h1: x 62 h2: z 63YAML 64 65 is($mm->metafile_file(@meta), $expected, "nested hashes sort ascii-betically"); 66 # to tell the truth, they sort case-insensitively 67 # that's hard to test for Perl with unstable sort's 68} 69 70{ 71 my @meta = ( a => 1, b => 2, h => { hh => { hhh => 1 } } ); 72 my $expected = <<YAML; 73--- #YAML:1.0 74a: 1 75b: 2 76h: 77 hh: 78 hhh: 1 79YAML 80 81 is($mm->metafile_file(@meta), $expected, "dump for hashes (with more nesting) works ok"); 82} 83 84{ 85 my @meta = ( a => 1, k => [ qw(w y z) ] ); 86 my $expected = <<YAML; 87--- #YAML:1.0 88a: 1 89k: 90 - w 91 - y 92 - z 93YAML 94 95 is($mm->metafile_file(@meta), $expected, "array of strings are handled ok"); 96} 97 98is($mm->metafile_file( a => {}, b => [], c => undef ), <<'YAML', 'empty hashes and arrays'); 99--- #YAML:1.0 100a: {} 101b: [] 102c: ~ 103YAML 104 105 106{ 107 my @meta = ( 108 name => 'My-Module', 109 version => '0.1', 110 version_from => 'lib/My/Module.pm', 111 installdirs => 'site', 112 abstract => 'A does-it-all module', 113 license => 'perl', 114 generated_by => 'myself', 115 author => 'John Doe <doe@doeland.org>', 116 distribution_type => 'module', 117 requires => { 118 'My::Module::Helper' => 0, 119 'Your::Module' => '1.5', 120 }, 121 'meta-spec' => { 122 version => '1.1', 123 url => 'http://module-build.sourceforge.net/META-spec-new.html', 124 }, 125 ); 126 my $expected = <<'YAML'; 127--- #YAML:1.0 128name: My-Module 129version: 0.1 130version_from: lib/My/Module.pm 131installdirs: site 132abstract: A does-it-all module 133license: perl 134generated_by: myself 135author: John Doe <doe@doeland.org> 136distribution_type: module 137requires: 138 My::Module::Helper: 0 139 Your::Module: 1.5 140meta-spec: 141 url: http://module-build.sourceforge.net/META-spec-new.html 142 version: 1.1 143YAML 144 145 is($mm->metafile_file(@meta), $expected, "dump for something like META.yml works"); 146} 147 148{ 149 my @meta = ( 150 name => 'My-Module', 151 version => '0.1', 152 version_from => 'lib/My/Module.pm', 153 installdirs => 'site', 154 abstract => 'A does-it-all module', 155 license => 'perl', 156 generated_by => 'myself', 157 author => 'John Doe <doe@doeland.org>', 158 distribution_type => 'module', 159 requires => { 160 'My::Module::Helper' => 0, 161 'Your::Module' => '1.5', 162 }, 163 recommends => { 164 'Test::More' => 0, 165 'Test::Pod' => 1.18, 166 'Test::Pod::Coverage' => 1 167 }, 168 'meta-spec' => { 169 version => '1.1', 170 url => 'http://module-build.sourceforge.net/META-spec-new.html', 171 }, 172 ); 173 my $expected = <<'YAML'; 174--- #YAML:1.0 175name: My-Module 176version: 0.1 177version_from: lib/My/Module.pm 178installdirs: site 179abstract: A does-it-all module 180license: perl 181generated_by: myself 182author: John Doe <doe@doeland.org> 183distribution_type: module 184requires: 185 My::Module::Helper: 0 186 Your::Module: 1.5 187recommends: 188 Test::More: 0 189 Test::Pod: 1.18 190 Test::Pod::Coverage: 1 191meta-spec: 192 url: http://module-build.sourceforge.net/META-spec-new.html 193 version: 1.1 194YAML 195 196 is($mm->metafile_file(@meta), $expected, "META.yml with extra 'recommends' works"); 197} 198 199{ 200 my @meta = ( 201 name => 'My-Module', 202 version => '0.1', 203 version_from => 'lib/My/Module.pm', 204 installdirs => 'site', 205 abstract => 'A does-it-all module', 206 license => 'perl', 207 generated_by => 'myself', 208 author => 'John Doe <doe@doeland.org>', 209 distribution_type => 'module', 210 requires => { 211 'My::Module::Helper' => 0, 212 'Your::Module' => '1.5', 213 }, 214 recommends => { 215 'Test::More' => 0, 216 'Test::Pod' => 1.18, 217 'Test::Pod::Coverage' => 1 218 }, 219 no_index => { 220 dir => [ qw(inc) ], 221 file => [ qw(TODO NOTES) ], 222 }, 223 'meta-spec' => { 224 version => '1.1', 225 url => 'http://module-build.sourceforge.net/META-spec-new.html', 226 }, 227 ); 228 my $expected = <<'YAML'; 229--- #YAML:1.0 230name: My-Module 231version: 0.1 232version_from: lib/My/Module.pm 233installdirs: site 234abstract: A does-it-all module 235license: perl 236generated_by: myself 237author: John Doe <doe@doeland.org> 238distribution_type: module 239requires: 240 My::Module::Helper: 0 241 Your::Module: 1.5 242recommends: 243 Test::More: 0 244 Test::Pod: 1.18 245 Test::Pod::Coverage: 1 246no_index: 247 dir: 248 - inc 249 file: 250 - TODO 251 - NOTES 252meta-spec: 253 url: http://module-build.sourceforge.net/META-spec-new.html 254 version: 1.1 255YAML 256 257 is($mm->metafile_file(@meta), $expected, "META.yml with extra 'no_index' works"); 258 259 260 # Make sure YAML.pm can ready our output 261 SKIP: { 262 skip "Need YAML.pm to test if it can load META.yml", 1 263 unless eval { require YAML }; 264 265 my $yaml_load = YAML::Load($mm->metafile_file(@meta)); 266 is_deeply( $yaml_load, {@meta}, "META.yml can be read by YAML.pm" ); 267 } 268 269 270 SKIP: { 271 # Load() behaves diffrently in versions prior to 1.06 272 skip "Need YAML::Tiny to test if it can load META.yml", 2 273 unless eval { require YAML::Tiny } and $YAML::Tiny::VERSION >= 1.06; 274 275 my @yaml_load = YAML::Tiny::Load($mm->metafile_file(@meta)); 276 is @yaml_load, 1, "YAML::Tiny saw one document in META.yml"; 277 is_deeply( $yaml_load[0], {@meta}, "META.yml can be read by YAML::Tiny" ); 278 } 279} 280 281 282{ 283 my @meta = ( k => 'a : b', 'x : y' => 1 ); 284 my $expected = <<YAML; 285--- #YAML:1.0 286k: a : b 287x : y: 1 288YAML 289 # NOTE: the output is not YAML-equivalent to the input 290 291 is($mm->metafile_file(@meta), $expected, "no quoting is done"); 292} 293 294{ 295 my @meta = ( k => \*STDOUT ); 296 eval { $mm->metafile_file(@meta) }; 297 298 like($@, qr/^only nested hashes, arrays and objects are supported/, 299 "we don't like but hash/array refs"); 300} 301 302{ 303 my @meta = ( k => [ [] ] ); 304 eval { $mm->metafile_file(@meta) }; 305 306 like($@, qr/^only nested arrays of non-refs are supported/, 307 "we also don't like but array of strings"); 308} 309 310# recursive data structures: don't even think about it - endless recursion 311