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