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