]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - t/60-version.t
4b6b2a99ae2ab227a1e3cac7fdada86731427bc8
[perl/modules/Test-Valgrind.git] / t / 60-version.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6 + 6 + 2 * 5 + 2 * 5 + 2 * 21 + 2 * 14;
7
8 use Test::Valgrind::Version;
9
10 sub TVV () { 'Test::Valgrind::Version' }
11
12 sub sanitize {
13  my $str = shift;
14
15  $str = '(undef)' unless defined $str;
16  1 while chomp $str;
17  $str =~ s/\n/\\n/g;
18
19  $str;
20 }
21
22 my @command_failures = (
23  undef,
24  'valgrind',
25  '1.2.3',
26  'valgrin-1.2.3',
27  'VALGRIND-1.2.3',
28  "doo dah doo\nvalgrind-1.2.3",
29 );
30
31 for my $failure (@command_failures) {
32  my $desc = sanitize $failure;
33  local $@;
34  eval { TVV->new(command_output => $failure) };
35  like $@, qr/^Invalid argument/,
36           "\"$desc\" correctly failed to parse as command_output";
37 }
38
39 my @string_failures = (
40  undef,
41  'valgrind',
42  'valgrind-1.2.3',
43  '1.',
44  '1.2.',
45  '1.2.a',
46 );
47
48 for my $failure (@string_failures) {
49  my $desc = sanitize $failure;
50  local $@;
51  eval { TVV->new(string => $failure) };
52  like $@, qr/^Invalid argument/,
53           "\"$desc\" correctly failed to parse as string";
54 }
55
56 my @command_valid = (
57  'valgrind-1'         => '1.0.0',
58  'valgrind-1.2'       => '1.2.0',
59  'valgrind-1.2.3'     => '1.2.3',
60  'valgrind-1.2.4-rc5' => '1.2.4',
61  'valgrind-1.2.6a'    => '1.2.6',
62 );
63
64 my @string_valid = map { my $s = $_; $s =~ s/^valgrind-//; $s }
65                     @command_valid;
66
67 while (@command_valid) {
68  my ($output, $exp) = splice @command_valid, 0, 2;
69  my $desc = sanitize $output;
70  local $@;
71  my $res = eval { TVV->new(command_output => $output)->_stringify };
72  is $@,   '',   "\"$desc\" is parseable as command_output";
73  is $res, $exp, "\"$desc\" parses correctly as command_output";
74 }
75
76 while (@string_valid) {
77  my ($str, $exp) = splice @string_valid, 0, 2;
78  my $desc = sanitize $str;
79  local $@;
80  my $res = eval { TVV->new(string => $str)->_stringify };
81  is $@,   '',   "\"$desc\" is parseable as string";
82  is $res, $exp, "\"$desc\" parses correctly as string";
83 }
84
85 sub tvv_s {
86  my ($string) = @_;
87  local $@;
88  eval { TVV->new(string => $string) };
89 }
90
91 my @compare = (
92  '1',       '1',     0,
93  '1',       '1.0',   0,
94  '1',       '1.0.0', 0,
95  '1.1',     '1',     1,
96  '1.1',     '1.0',   1,
97  '1.1',     '1.0.0', 1,
98  '1',       '1.1',   -1,
99  '1.0',     '1.1',   -1,
100  '1.0.0',   '1.1',   -1,
101  '1.1',     '1.2',   -1,
102  '1.1.0',   '1.2',   -1,
103  '1.1',     '1.2.0', -1,
104  '1.1.0',   '1.2.0', -1,
105  '1',       '1',     0,
106  '1.0.1',   '1',     1,
107  '1.0.1.0', '1',     1,
108  '1.0.0.1', '1',     1,
109  '1.0.0.1', '1.0.1', -1,
110  '1.0.0.2', '1.0.1', -1,
111  '3.4.0',   '3.4.1', -1,
112  '3.5.2',   '3.5.1', 1,
113 );
114
115 while (@compare) {
116  my ($left, $right, $exp) = splice @compare, 0, 3;
117
118  my $desc = sanitize($left) . ' <=> ' . sanitize($right);
119
120  $left  = tvv_s($left);
121  $right = tvv_s($right);
122
123  my ($err, $res) = '';
124  if (defined $left and defined $right) {
125   local $@;
126   $res = eval { $left <=> $right };
127   $err = $@;
128  } elsif (defined $right) {
129   $res = -2;
130  } elsif (defined $left) {
131   $res = 2;
132  }
133
134  is $err, '',   "\"$desc\" compared without croaking";
135  is $res, $exp, "\"$desc\" compared correctly";
136 }
137
138 my @stringify = (
139  '1',         '1.0.0',
140  '1.0',       '1.0.0',
141  '1.0.0',     '1.0.0',
142  '1.0.0.0',   '1.0.0',
143  '1.2',       '1.2.0',
144  '1.2.0',     '1.2.0',
145  '1.2.0.0',   '1.2.0',
146  '1.2.3',     '1.2.3',
147  '1.2.3.0',   '1.2.3',
148  '1.2.3.4',   '1.2.3.4',
149  '1.2.3.4.0', '1.2.3.4',
150  '1.0.3',     '1.0.3',
151  '1.0.0.4',   '1.0.0.4',
152  '1.2.0.4',   '1.2.0.4',
153 );
154
155 while (@stringify) {
156  my ($str, $exp) = splice @stringify, 0, 2;
157  my $desc = sanitize($str);
158  local $@;
159  my $res = eval { my $v = TVV->new(string => $str); "$v" };
160  is $@,   '',   "\"$desc\" stringification did not croak";
161  is $res, $exp, "\"$desc\" stringified correctly";
162 }