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 = CPAN::Meta::Requirements->new;
24
25  $req->add_minimum('Foo::Bar' => 10);
26  $req->add_minimum('Foo::Bar' => 0);
27  $req->add_minimum('Foo::Bar' => 2);
28
29  $req->add_minimum('Foo::Baz' => version->declare('v1.2.3'));
30
31  $req->add_minimum('Foo::Undef' => undef);
32
33  my $want = {
34    'Foo::Bar'   => 10,
35    'Foo::Baz'   => 'v1.2.3',
36    'Foo::Undef' => 0,
37  };
38
39  is_deeply(
40    $req->as_string_hash,
41    $want,
42    "some basic minimums",
43  );
44
45  $req->finalize;
46
47  $req->add_minimum('Foo::Bar', 2);
48
49  pass('we can add a Foo::Bar requirement with no effect post finalization');
50
51  dies_ok { $req->add_minimum('Foo::Bar', 12) }
52    qr{finalized req},
53    "can't add a higher Foo::Bar after finalization";
54
55  dies_ok { $req->add_minimum('Foo::New', 0) }
56    qr{finalized req},
57    "can't add a new module prereq after finalization";
58
59  dies_ok { $req->clear_requirement('Foo::Bar') }
60    qr{finalized req},
61    "can't clear an existing prereq after finalization";
62
63  $req->clear_requirement('Bogus::Req');
64
65  pass('we can clear a prereq that was not set to begin with');
66
67  is_deeply(
68    $req->as_string_hash,
69    $want,
70    "none of our attempts to alter the object post-finalization worked",
71  );
72
73  my $cloned = $req->clone;
74
75  $cloned->add_minimum('Foo::Bar', 12);
76
77  is_deeply(
78    $cloned->as_string_hash,
79    {
80      %$want,
81      'Foo::Bar' => 12,
82    },
83    "we can alter a cloned V:R (finalization does not survive cloning)",
84  );
85
86  is_deeply(
87    $req->as_string_hash,
88    $want,
89    "...and original requirements are untouched",
90  );
91}
92
93done_testing;
94