1use strict;
2use warnings;
3use Test::More 0.88;
4
5use CPAN::Meta::Prereqs;
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 $prereq_struct_1 = {
13  runtime => {
14    requires => {
15      'Config' => 0,
16      'Cwd'    => 0,
17      'perl'   => '5.005_03',
18    },
19    recommends => {
20      'Pod::Text' => 0,
21      'YAML'      => 0.35,
22    },
23  },
24  build => {
25    requires => {
26      'Test' => 0,
27    },
28    x_type => {
29      'Config' => 1,
30    },
31  },
32  x_phase => {
33    x_type => {
34      'POSIX' => '1.23',
35    },
36  },
37};
38
39my $prereq_1 = CPAN::Meta::Prereqs->new($prereq_struct_1);
40
41isa_ok($prereq_1, 'CPAN::Meta::Prereqs', 'first prereq');
42
43is_deeply($prereq_1->as_string_hash, $prereq_struct_1, '...and it round trips');
44
45my $prereq_struct_2 = {
46  develop => {
47    requires => {
48      'Dist::Mothra' => '1.230',
49    },
50    suggests => {
51      'Blort::Blortex' => '== 10.20',
52    },
53  },
54  runtime => {
55    requires => {
56      'Config' => 1,
57      'perl'       => '< 6',
58    },
59  },
60  build => {
61    suggests => {
62      'Module::Build::Bob' => '20100101',
63    },
64  },
65  x_phase => {
66    requires => {
67      'JSON::PP' => '2.34',
68    },
69  },
70};
71
72my $prereq_2 = CPAN::Meta::Prereqs->new($prereq_struct_2);
73
74isa_ok($prereq_2, 'CPAN::Meta::Prereqs', 'second prereq');
75
76is_deeply($prereq_1->as_string_hash, $prereq_struct_1, '...and it round trips');
77
78my $merged = $prereq_1->with_merged_prereqs($prereq_2);
79
80my $want = {
81  develop => {
82    requires => {
83      'Dist::Mothra' => '1.230',
84    },
85    suggests => {
86      'Blort::Blortex' => '== 10.20',
87    },
88  },
89  runtime => {
90    requires => {
91      'Config' => 1,
92      'Cwd'    => 0,
93      'perl'   => '>= 5.005_03, < 6',
94    },
95    recommends => {
96      'Pod::Text' => 0,
97      'YAML'      => 0.35,
98    },
99  },
100  build => {
101    requires => {
102      'Test' => 0,
103    },
104    suggests => {
105      'Module::Build::Bob' => '20100101',
106    },
107    x_type => {
108      'Config' => 1,
109    },
110  },
111  x_phase => {
112    requires => {
113      'JSON::PP' => '2.34',
114    },
115    x_type => {
116      'POSIX' => '1.23',
117    },
118  },
119};
120
121is_deeply(
122  $merged->as_string_hash,
123  $want,
124  "we get the right result of merging two prereqs",
125);
126
127is_deeply(
128  $prereq_2->with_merged_prereqs($prereq_1)->as_string_hash,
129  $want,
130  "...and the merge works the same in reverse",
131);
132
133done_testing;
134# vim: ts=2 sts=2 sw=2 et :
135