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