]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - t/60-version.t
More lax valgrind version parsing
[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 + 5 + 4 * 8 + 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  'abc',
44  'd.e.f',
45 );
46
47 for my $failure (@string_failures) {
48  my $desc = sanitize $failure;
49  local $@;
50  eval { TVV->new(string => $failure) };
51  like $@, qr/^Invalid argument/,
52           "\"$desc\" correctly failed to parse as string";
53 }
54
55 my @command_valid = (
56  'valgrind-1'         => '1.0.0',
57  'valgrind-1.2'       => '1.2.0',
58  'valgrind-1.2.3'     => '1.2.3',
59  'valgrind-1.2.4-rc5' => '1.2.4',
60  'valgrind-1.2.6a'    => '1.2.6',
61  'valgrind-1.2.7.'    => '1.2.7',
62  'valgrind-1.2.x.8'   => '1.2.0',
63  'valgrind-1.10.'     => '1.10.0',
64 );
65
66 my @string_valid = map { my $s = $_; $s =~ s/^valgrind-//; $s }
67                     @command_valid;
68
69 while (@command_valid) {
70  my ($output, $exp) = splice @command_valid, 0, 2;
71  my $desc = sanitize $output;
72  local $@;
73  my $res = eval { TVV->new(command_output => $output)->_stringify };
74  is $@,   '',   "\"$desc\" is parseable as command_output";
75  is $res, $exp, "\"$desc\" parses correctly as command_output";
76 }
77
78 while (@string_valid) {
79  my ($str, $exp) = splice @string_valid, 0, 2;
80  my $desc = sanitize $str;
81  local $@;
82  my $res = eval { TVV->new(string => $str)->_stringify };
83  is $@,   '',   "\"$desc\" is parseable as string";
84  is $res, $exp, "\"$desc\" parses correctly as string";
85 }
86
87 sub tvv_s {
88  my ($string) = @_;
89  local $@;
90  eval { TVV->new(string => $string) };
91 }
92
93 my @compare = (
94  '1',       '1',     0,
95  '1',       '1.0',   0,
96  '1',       '1.0.0', 0,
97  '1.1',     '1',     1,
98  '1.1',     '1.0',   1,
99  '1.1',     '1.0.0', 1,
100  '1',       '1.1',   -1,
101  '1.0',     '1.1',   -1,
102  '1.0.0',   '1.1',   -1,
103  '1.1',     '1.2',   -1,
104  '1.1.0',   '1.2',   -1,
105  '1.1',     '1.2.0', -1,
106  '1.1.0',   '1.2.0', -1,
107  '1',       '1',     0,
108  '1.0.1',   '1',     1,
109  '1.0.1.0', '1',     1,
110  '1.0.0.1', '1',     1,
111  '1.0.0.1', '1.0.1', -1,
112  '1.0.0.2', '1.0.1', -1,
113  '3.4.0',   '3.4.1', -1,
114  '3.5.2',   '3.5.1', 1,
115 );
116
117 while (@compare) {
118  my ($left, $right, $exp) = splice @compare, 0, 3;
119
120  my $desc = sanitize($left) . ' <=> ' . sanitize($right);
121
122  $left  = tvv_s($left);
123  $right = tvv_s($right);
124
125  my ($err, $res) = '';
126  if (defined $left and defined $right) {
127   local $@;
128   $res = eval { $left <=> $right };
129   $err = $@;
130  } elsif (defined $right) {
131   $res = -2;
132  } elsif (defined $left) {
133   $res = 2;
134  }
135
136  is $err, '',   "\"$desc\" compared without croaking";
137  is $res, $exp, "\"$desc\" compared correctly";
138 }
139
140 my @stringify = (
141  '1',         '1.0.0',
142  '1.0',       '1.0.0',
143  '1.0.0',     '1.0.0',
144  '1.0.0.0',   '1.0.0',
145  '1.2',       '1.2.0',
146  '1.2.0',     '1.2.0',
147  '1.2.0.0',   '1.2.0',
148  '1.2.3',     '1.2.3',
149  '1.2.3.0',   '1.2.3',
150  '1.2.3.4',   '1.2.3.4',
151  '1.2.3.4.0', '1.2.3.4',
152  '1.0.3',     '1.0.3',
153  '1.0.0.4',   '1.0.0.4',
154  '1.2.0.4',   '1.2.0.4',
155 );
156
157 while (@stringify) {
158  my ($str, $exp) = splice @stringify, 0, 2;
159  my $desc = sanitize($str);
160  local $@;
161  my $res = eval { my $v = TVV->new(string => $str); "$v" };
162  is $@,   '',   "\"$desc\" stringification did not croak";
163  is $res, $exp, "\"$desc\" stringified correctly";
164 }