1use strict;
2use warnings;
3
4use CPAN::Meta::Requirements;
5
6use Test::More 0.88;
7
8sub dies_ok (&@) {
9  my ($code, $qr, $comment) = @_;
10
11  no warnings 'redefine';
12  local *Regexp::CARP_TRACE  = sub { "<regexp>" };
13  my $lived = eval { $code->(); 1 };
14
15  if ($lived) {
16    fail("$comment: did not die");
17  } else {
18    like($@, $qr, $comment);
19  }
20}
21
22{
23  my $req_1 = CPAN::Meta::Requirements->new;
24  $req_1->add_minimum(Left   => 10);
25  $req_1->add_minimum(Shared => 2);
26  $req_1->add_exclusion(Shared => 7);
27
28  my $req_2 = CPAN::Meta::Requirements->new;
29  $req_2->add_minimum(Shared => 1);
30  $req_2->add_maximum(Shared => 9);
31  $req_2->add_minimum(Right  => 18);
32
33  $req_1->add_requirements($req_2);
34
35  is_deeply(
36    $req_1->as_string_hash,
37    {
38      Left   => 10,
39      Shared => '>= 2, <= 9, != 7',
40      Right  => 18,
41    },
42    "add requirements to an existing set of requirements",
43  );
44}
45
46{
47  my $req_1 = CPAN::Meta::Requirements->new;
48  $req_1->add_minimum(Left   => 10);
49  $req_1->add_minimum(Shared => 2);
50  $req_1->add_exclusion(Shared => 7);
51  $req_1->exact_version(Exact => 8);
52
53  my $req_2 = CPAN::Meta::Requirements->new;
54  $req_2->add_minimum(Shared => 1);
55  $req_2->add_maximum(Shared => 9);
56  $req_2->add_minimum(Right  => 18);
57  $req_2->exact_version(Exact => 8);
58
59  my $clone = $req_1->clone->add_requirements($req_2);
60
61  is_deeply(
62    $req_1->as_string_hash,
63    {
64      Left   => 10,
65      Shared => '>= 2, != 7',
66      Exact  => '== 8',
67    },
68    "clone/add_requirements does not affect lhs",
69  );
70
71  is_deeply(
72    $req_2->as_string_hash,
73    {
74      Shared => '>= 1, <= 9',
75      Right  => 18,
76      Exact  => '== 8',
77    },
78    "clone/add_requirements does not affect rhs",
79  );
80
81  is_deeply(
82    $clone->as_string_hash,
83    {
84      Left   => 10,
85      Shared => '>= 2, <= 9, != 7',
86      Right  => 18,
87      Exact  => '== 8',
88    },
89    "clone and add_requirements",
90  );
91
92  $clone->clear_requirement('Shared');
93
94  is_deeply(
95    $clone->as_string_hash,
96    {
97      Left   => 10,
98      Right  => 18,
99      Exact  => '== 8',
100    },
101    "cleared the shared requirement",
102  );
103}
104
105{
106  my $req_1 = CPAN::Meta::Requirements->new;
107  $req_1->add_maximum(Foo => 1);
108
109  my $req_2 = $req_1->clone;
110
111  is_deeply(
112    $req_2->as_string_hash,
113    {
114      'Foo' => '<= 1',
115    },
116    'clone with only max',
117  );
118}
119
120{
121  my $left = CPAN::Meta::Requirements->new;
122  $left->add_minimum(Foo => 0);
123  $left->add_minimum(Bar => 1);
124
125  my $right = CPAN::Meta::Requirements->new;
126  $right->add_requirements($left);
127
128  is_deeply(
129    $right->as_string_hash,
130    {
131      Foo => 0,
132      Bar => 1,
133    },
134    "we do not lose 0-min reqs on merge",
135  );
136}
137
138done_testing;
139