1use strict;
2use warnings;
3use Test::More 0.88;
4
5use CPAN::Meta::Converter;
6
7delete $ENV{PERL_YAML_BACKEND};
8delete $ENV{PERL_JSON_BACKEND};
9delete $ENV{CPAN_META_JSON_BACKEND};
10delete $ENV{CPAN_META_JSON_DECODER};
11
12my $spec2 = {
13    version => '2',
14    url => 'http://search.cpan.org/perldoc?CPAN::Meta::Spec',
15};
16
17my @cases = (
18    #<<< No perltidy
19    {
20        label  => "v1.4 requires -> v2 prereqs",
21        from   => "1.4",
22        to     => "2",
23        input  => {
24            requires => {
25                'File::Spec' => "0.80",
26            },
27        },
28        expect => {
29            'meta-spec' => $spec2,
30            prereqs => {
31                runtime => {
32                    requires => {
33                        'File::Spec' => "0.80",
34                    },
35                }
36            }
37        },
38    },
39    {
40        label  => "v1.4 x_custom -> v2 x_custom",
41        from   => "1.4",
42        to     => "2",
43        input  => {
44            x_authority => 'DAGOLDEN',
45        },
46        expect => {
47            'meta-spec' => $spec2,
48            x_authority => 'DAGOLDEN',
49        },
50    },
51    {
52        label  => "meta-spec included",
53        to     => "2",
54        input  => {
55            'meta-spec' => { version => '1.0' },
56            requires => {
57                'File::Spec' => "0.80",
58            },
59        },
60        expect => {
61            'meta-spec' => $spec2,
62            prereqs => {
63                runtime => {
64                    requires => {
65                        'File::Spec' => "0.80",
66                    },
67                }
68            }
69        },
70    },
71    {
72        # this is a test of default version and intentionally gives bad
73        # data that will get dropped by the conversion
74        label  => "default version",
75        from   => "2",
76        to     => "2",
77        input  => {
78            requires => {
79                'File::Spec' => "0.80",
80            },
81        },
82        expect => {
83            'meta-spec' => $spec2,
84        },
85    },
86    {
87        # fields deprecated from older versions
88        label  => "v1.4 prereq stuff -> v2 prereqs",
89        from   => "1.4",
90        to     => "2",
91        input  => {
92            configure_requires => {
93                'File::Spec' => "0.80",
94            },
95            build_requires => {
96                'Scalar::Util' => '1.0',
97            },
98            requires => {
99                'B' => '3.1',
100            },
101            recommends => {
102                'Config' => '4.0',
103            },
104            conflicts => {
105                'File::Temp' => "0.2",
106            },
107        },
108        expect => {
109            'meta-spec' => $spec2,
110            prereqs => {
111                configure => {
112                    requires => {
113                        'File::Spec' => "0.80",
114                    },
115                },
116                build => {
117                    requires => {
118                        'Scalar::Util' => '1.0',
119                    },
120                },
121                runtime => {
122                    conflicts => {
123                        'File::Temp' => "0.2",
124                    },
125                    requires => {
126                        'B' => '3.1',
127                    },
128                    recommends => {
129                        'Config' => '4.0',
130                    },
131                },
132            },
133        },
134    },
135    {
136        label  => "v1.1 license_url: -> v2 license",
137        from   => "1.1",
138        to     => "2",
139        input  => {
140            license_url => 'http://opensource.org/licenses/Artistic-1.0',
141            license => 'perl',
142        },
143        expect => {
144            'meta-spec' => $spec2,
145            license => [ 'perl_5' ],
146        },
147    },
148);
149
150for my $c (@cases) {
151    my $cmc = CPAN::Meta::Converter->new(
152        $c->{input}, $c->{from} ? (default_version => $c->{from} ) : ()
153    );
154    my $got = $cmc->upgrade_fragment;
155    my $exp = $c->{expect};
156    is_deeply( $got, $exp, $c->{label} )
157      or diag "GOT:\n", explain($got), "EXPECTED:\n", explain($exp);
158}
159
160done_testing;
161# vim: ts=8 sts=4 sw=4 et :
162