File: strings.t

package info (click to toggle)
libcpan-meta-requirements-perl 2.143-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: perl: 611; makefile: 2
file content (143 lines) | stat: -rw-r--r-- 4,771 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use strict;
use warnings;
use Test::More 0.88;

sub dies_ok (&@) {
  my ($code, $qr, $comment) = @_;

  no warnings 'redefine';
  local *Regexp::CARP_TRACE  = sub { "<regexp>" };
  my $lived = eval { $code->(); 1 };

  if ($lived) {
    fail("$comment: did not die");
  } else {
    like($@, $qr, $comment);
  }
}

use CPAN::Meta::Requirements;


my $base = 'CPAN::Meta::Requirements::Range';

# Test ==
my $range = $base->with_string_requirement('== 1.3');
ok($range->accepts('1.3'), 'exact version (==)');
ok(!$range->accepts('1.2'), 'lower version (==)');
ok(!$range->accepts('1.4'), 'higher version (==)');

# Test !=
$range = $base->with_string_requirement('!= 1.3');
ok(!$range->accepts('1.3'), 'exact version (!=)');
ok($range->accepts('1.2'), 'lower version (!=)');
ok($range->accepts('1.4'), 'higher version (!=)');

# Test >=
$range = $base->with_string_requirement('>= 1.3');
ok($range->accepts('1.3'), 'exact version (>=)');
ok(!$range->accepts('1.2'), 'lower version (>=)');
ok($range->accepts('1.4'), 'higher version (>=)');

# Test <=
$range = $range = $base->with_string_requirement('<= 1.3');
ok($range->accepts('1.3'), 'exact version (<=)');
ok($range->accepts('1.2'), 'lower version (<=)');
ok(!$range->accepts('1.4'), 'higher version (<=)');

# Test ""
$range = $base->with_string_requirement('1.3');
ok($range->accepts('1.3'), 'exact version ("")');
ok(!$range->accepts('1.2'), 'lower version ("")');
ok($range->accepts('1.4'), 'higher version ("")');

# Test multiple requirements
$range = $base->with_string_requirement('>= 1.3, <= 2.0, != 1.6');
ok($range->accepts('1.5'), 'middle version (>=, <=, !)');
ok(!$range->accepts('1.2'), 'lower version (>=, <=, !)');
ok(!$range->accepts('2.1'), 'higher version (>=, <=, !)');
ok(!$range->accepts('1.6'), 'excluded version (>=, <=, !)');

# Test multiple requirements with implicit minimum
$range = $base->with_string_requirement('0.90, != 0.91');
ok(!$range->accepts('0.88'), 'lower version ("", !)');
ok($range->accepts('0.90'), 'exact version ("", !)');
ok($range->accepts('0.901'), 'middle version ("", !)');
ok(!$range->accepts('0.91'), 'excluded version ("", !)');
ok($range->accepts('0.92'), 'higher version ("", !)');

my $req = CPAN::Meta::Requirements->new;

# Test ==
$req->add_string_requirement('Foo::Bar', '== 1.3');
ok($req->accepts_module('Foo::Bar' => '1.3'), 'exact version (==)');
ok(!$req->accepts_module('Foo::Bar' => '1.2'), 'lower version (==)');
ok(!$req->accepts_module('Foo::Bar' => '1.4'), 'higher version (==)');

# Test !=
$req->add_string_requirement('Foo::Baz', '!= 1.3');
ok(!$req->accepts_module('Foo::Baz' => '1.3'), 'exact version (!=)');
ok($req->accepts_module('Foo::Baz' => '1.2'), 'lower version (!=)');
ok($req->accepts_module('Foo::Baz' => '1.4'), 'higher version (!=)');

# Test >=
$req->add_string_requirement('Foo::Gorch', '>= 1.3');
ok($req->accepts_module('Foo::Gorch' => '1.3'), 'exact version (>=)');
ok(!$req->accepts_module('Foo::Gorch' => '1.2'), 'lower version (>=)');
ok($req->accepts_module('Foo::Gorch' => '1.4'), 'higher version (>=)');

# Test <=
$req->add_string_requirement('Foo::Graz', '<= 1.3');
ok($req->accepts_module('Foo::Graz' => '1.3'), 'exact version (<=)');
ok($req->accepts_module('Foo::Graz' => '1.2'), 'lower version (<=)');
ok(!$req->accepts_module('Foo::Graz' => '1.4'), 'higher version (<=)');

# Test ""
$req->add_string_requirement('Foo::Blurb', '1.3');
ok($req->accepts_module('Foo::Blurb' => '1.3'), 'exact version ("")');
ok(!$req->accepts_module('Foo::Blurb' => '1.2'), 'lower version ("")');
ok($req->accepts_module('Foo::Blurb' => '1.4'), 'higher version ("")');

# Test multiple requirements
$req->add_string_requirement('A::Tribe::Called', '>= 1.3, <= 2.0, != 1.6');
ok($req->accepts_module('A::Tribe::Called' => '1.5'), 'middle version (>=, <=, !)');
ok(!$req->accepts_module('A::Tribe::Called' => '1.2'), 'lower version (>=, <=, !)');
ok(!$req->accepts_module('A::Tribe::Called' => '2.1'), 'higher version (>=, <=, !)');
ok(!$req->accepts_module('A::Tribe::Called' => '1.6'), 'excluded version (>=, <=, !)');

# Test multiple requirements with implicit minimum
{
  my $req = CPAN::Meta::Requirements->new;

  $req->add_string_requirement('Foo::MyModule', '0.90, != 0.91');

  is_deeply(
    $req->as_string_hash,
    {
      'Foo::MyModule' => '>= 0.90, != 0.91'
    },
    "multiple requirements with implicit minimum",
  );
}

# Test precision
{
  my $req = CPAN::Meta::Requirements->new;

  $req->add_string_requirement(Foo => "0.00");

  is_deeply(
    $req->as_string_hash,
    {
      Foo => '0.00'
    },
    "0.00 precision preserved",
  );
}

# Test fatal errors
dies_ok { $req->add_string_requirement('Foo::Bar', "not really a version") }
  qr/Can't convert/,
  "conversion failure caught";

done_testing;