15aa621cb63d655173b04281744a122e4a0d0636
[perl.git] / cpan / CPAN-Meta / t / optional_feature-merge.t
1 use strict;
2 use warnings;
3 # vim: set ts=4 sw=4 noet nolist :
4
5 use Test::More;
6 use CPAN::Meta;
7 use CPAN::Meta::Merge;
8
9 my %base = (
10         abstract => 'This is a test',
11         author => ['A.U. Thor'],
12         generated_by => 'Myself',
13         license => [ 'perl_5' ],
14         resources => {
15                 license => [ 'http://dev.perl.org/licenses/' ],
16         },
17         prereqs => {
18                 runtime => {
19                         requires => {
20                                 Foo => '0',
21                         },
22                 },
23         },
24         dynamic_config => 0,
25         provides => {
26                 Baz => {
27                         file => 'lib/Baz.pm',
28                 },
29         },
30         'meta-spec' => {
31                 url => "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
32                 version => 2,
33         },
34 );
35
36 my $fragment1 = {
37         'optional_features' => {
38                 'FeatureName' => {
39                         'description' => 'desc',
40                         'x_default' => 1,
41                         'prereqs' => { 'runtime' => { 'requires' => { 'A' => '0' } } }
42                 }
43         }
44 };
45 my $fragment2 = {
46         'optional_features' => {
47                 'FeatureName' => {
48                         'description' => 'desc',
49                         'prereqs' => { 'test' => { 'requires' => { 'B' => '0' } } }
50                 }
51         }
52 };
53
54 my $merger = CPAN::Meta::Merge->new(default_version => "2");
55 my $meta1 = $merger->merge(\%base, $fragment1);
56
57 is_deeply(
58         $meta1,
59         {
60                 %base,
61                 %$fragment1,
62         },
63         'merged first optional_feature fragment into base',
64 );
65
66 my $meta2 = $merger->merge($meta1, $fragment2);
67
68 is_deeply(
69         $meta2,
70         {
71                 %base,
72                 'optional_features' => {
73                         'FeatureName' => {
74                                 'description' => 'desc',
75                                 'x_default' => 1,
76                                 'prereqs' => {
77                                         'runtime' => { 'requires' => { 'A' => '0' } },
78                                         'test' => { 'requires' => { 'B' => '0' } },
79                                 }
80                         }
81                 }
82         },
83         'merged second optional_feature fragment into the first',
84 );
85
86 my $fragment3 = {
87         'optional_features' => {
88                 'FeatureName' => {
89                         'description' => 'other desc',
90                         'prereqs' => { 'test' => { 'requires' => { 'B' => '0' } } }
91                 }
92         }
93 };
94
95 my $result = eval { $merger->merge($meta1, $fragment3) };
96 is($result, undef, 'Trying to merge optional_features with same feature name and different descriptions gives an exception');
97 like $@, qr/^Cannot merge two optional_features named 'FeatureName' with different 'description' values/, 'Exception looks right';
98
99 my $fragment4 = {
100         'optional_features' => {
101                 'FeatureName' => {
102                         'description' => 'desc',
103                         'x_default' => 0,
104                         'prereqs' => { 'test' => { 'requires' => { 'B' => '0' } } }
105                 }
106         }
107 };
108
109 $result = eval { $merger->merge($meta1, $fragment4) };
110 is($result, undef, 'Trying to merge optional_features with same feature name and differences in other keys gives an exception');
111 like $@, qr/^Cannot merge two optional_features named 'FeatureName' with different 'x_default' values/, 'Exception looks right';
112
113 my $fragment5 = {
114         'optional_features' => {
115                 'Another FeatureName' => {
116                         'description' => 'desc',
117                         'prereqs' => { 'test' => { 'requires' => { 'B' => '0' } } }
118                 }
119         }
120 };
121
122 my $meta5 = $merger->merge($meta1, $fragment5);
123 is_deeply(
124         $meta5,
125         {
126                 %base,
127                 'optional_features' => {
128                         'FeatureName' => {
129                                 'description' => 'desc',
130                                 'x_default' => 1,
131                                 'prereqs' => { 'runtime' => { 'requires' => { 'A' => '0' } } },
132                         },
133                         'Another FeatureName' => {
134                                 'description' => 'desc',
135                                 'prereqs' => { 'test' => { 'requires' => { 'B' => '0' } } },
136                         }
137                 }
138         },
139         'can merge optional_features with different names without collisions',
140 );
141
142 done_testing;