1use strict;
2use warnings;
3use Test::More 0.88;
4
5sub dies_ok (&@) {
6  my ($code, $qr, $comment) = @_;
7
8  no warnings 'redefine';
9  local *Regexp::CARP_TRACE  = sub { "<regexp>" };
10  my $lived = eval { $code->(); 1 };
11
12  if ($lived) {
13    fail("$comment: did not die");
14  } else {
15    like($@, $qr, $comment);
16  }
17}
18
19use CPAN::Meta::Requirements;
20
21my $req = CPAN::Meta::Requirements->new;
22
23# Test ==
24$req->add_string_requirement('Foo::Bar', '== 1.3');
25ok($req->accepts_module('Foo::Bar' => '1.3'), 'exact version (==)');
26ok(!$req->accepts_module('Foo::Bar' => '1.2'), 'lower version (==)');
27ok(!$req->accepts_module('Foo::Bar' => '1.4'), 'higher version (==)');
28
29# Test !=
30$req->add_string_requirement('Foo::Baz', '!= 1.3');
31ok(!$req->accepts_module('Foo::Baz' => '1.3'), 'exact version (!=)');
32ok($req->accepts_module('Foo::Baz' => '1.2'), 'lower version (!=)');
33ok($req->accepts_module('Foo::Baz' => '1.4'), 'higher version (!=)');
34
35# Test >=
36$req->add_string_requirement('Foo::Gorch', '>= 1.3');
37ok($req->accepts_module('Foo::Gorch' => '1.3'), 'exact version (>=)');
38ok(!$req->accepts_module('Foo::Gorch' => '1.2'), 'lower version (>=)');
39ok($req->accepts_module('Foo::Gorch' => '1.4'), 'higher version (>=)');
40
41# Test <=
42$req->add_string_requirement('Foo::Graz', '<= 1.3');
43ok($req->accepts_module('Foo::Graz' => '1.3'), 'exact version (<=)');
44ok($req->accepts_module('Foo::Graz' => '1.2'), 'lower version (<=)');
45ok(!$req->accepts_module('Foo::Graz' => '1.4'), 'higher version (<=)');
46
47# Test ""
48$req->add_string_requirement('Foo::Blurb', '>= 1.3');
49ok($req->accepts_module('Foo::Blurb' => '1.3'), 'exact version (>=)');
50ok(!$req->accepts_module('Foo::Blurb' => '1.2'), 'lower version (>=)');
51ok($req->accepts_module('Foo::Blurb' => '1.4'), 'higher version (>=)');
52
53# Test multiple requirements
54$req->add_string_requirement('A::Tribe::Called', '>= 1.3, <= 2.0, != 1.6');
55ok($req->accepts_module('A::Tribe::Called' => '1.5'), 'middle version (>=, <=, !)');
56ok(!$req->accepts_module('A::Tribe::Called' => '1.2'), 'lower version (>=, <=, !)');
57ok(!$req->accepts_module('A::Tribe::Called' => '2.1'), 'higher version (>=, <=, !)');
58ok(!$req->accepts_module('A::Tribe::Called' => '1.6'), 'excluded version (>=, <=, !)');
59
60# Test precision
61{
62  my $req = CPAN::Meta::Requirements->new;
63
64  $req->add_string_requirement(Foo => "0.00");
65
66  is_deeply(
67    $req->as_string_hash,
68    {
69      Foo => '0.00'
70    },
71    "0.00 precision preserved",
72  );
73}
74
75# Test fatal errors
76dies_ok { $req->add_string_requirement('Foo::Bar', "not really a version") }
77  qr/Can't convert/,
78  "conversion failure caught";
79
80done_testing;
81