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
23for my $string (10, '>= 2, <= 9, != 7') {
24  my $range = CPAN::Meta::Requirements::Range->with_string_requirement($string);
25  is ($range->as_string, $string, "'$string' roundtrips");
26}
27
28{
29  my $string_hash = {
30    Left   => 10,
31    Shared => '>= 2, <= 9, != 7',
32    Right  => 18,
33  };
34
35  my $req = CPAN::Meta::Requirements->from_string_hash($string_hash);
36
37  is_deeply(
38    $req->as_string_hash,
39    $string_hash,
40    "we can load from a string hash",
41  );
42}
43
44SKIP: {
45  skip "Can't tell v-strings from strings until 5.8.1", 1
46    unless $] gt '5.008';
47  my $string_hash = {
48    Left   => 10,
49    Shared => '= 2',
50    Right  => 18,
51  };
52
53  dies_ok { CPAN::Meta::Requirements->from_string_hash($string_hash) }
54    qr/Can't convert/,
55    "we die when we can't understand a version spec";
56}
57
58{
59  my $warning;
60  local $SIG{__WARN__} = sub { $warning = join("\n",@_) };
61
62  my $range = CPAN::Meta::Requirements::Range->with_string_requirement(undef);
63  like ($warning, qr/Undefined requirement.*treated as '0'/, "undef requirement warns");
64  $range->with_string_requirement('');
65  like ($warning, qr/Undefined requirement.*treated as '0'/, "'' requirement warns");
66
67}
68
69{
70  my $undef_hash = { Undef => undef };
71  my $z_hash = { ZeroLength => '' };
72
73  my $warning;
74  local $SIG{__WARN__} = sub { $warning = join("\n",@_) };
75
76  my $req = CPAN::Meta::Requirements->from_string_hash($undef_hash);
77  like ($warning, qr/Undefined requirement.*treated as '0'/, "undef requirement warns");
78  $req->add_string_requirement(%$z_hash);
79  like ($warning, qr/Undefined requirement.*treated as '0'/, "'' requirement warns");
80
81  is_deeply(
82    $req->as_string_hash,
83    { map { ($_ => 0) } keys(%$undef_hash), keys(%$z_hash) },
84    "undef/'' requirements treated as '0'",
85  );
86}
87
88SKIP: {
89  skip "Can't tell v-strings from strings until 5.8.1", 2
90    unless $] gt '5.008';
91  my $string_hash = {
92    Left   => 10,
93    Shared => v50.44.60,
94    Right  => 18,
95  };
96
97  my $warning;
98  local $SIG{__WARN__} = sub { $warning = join("\n",@_) };
99
100  my $req = eval { CPAN::Meta::Requirements->from_string_hash($string_hash); };
101  is( $@, '', "vstring in string hash lives" );
102
103  ok(
104    $req->accepts_module(Shared => 'v50.44.60'),
105    "vstring treated as if string",
106  );
107}
108
109
110{
111  my $req = CPAN::Meta::Requirements->from_string_hash(
112    { Bad => 'invalid', },
113    { bad_version_hook => sub { version->new(42) } },
114  );
115
116  ok(
117    $req->accepts_module(Bad => 42),
118    "options work 2nd arg to f_s_h",
119  );
120}
121
122done_testing;
123