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