-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.pl
266 lines (215 loc) · 6.24 KB
/
test.pl
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'
######################### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)
my $loaded;
BEGIN { $| = 1; print "1..9\n"; }
END {print "not ok 1\n" unless $loaded;}
use strict;
use Getopt::Tabular;
$loaded = 1;
print "ok 1\n";
my $test_count = 1;
######################### End of black magic.
# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):
my $warning;
my $num_warnings = 0;
sub catch_warn
{
$warning = $_[0];
$num_warnings++;
}
sub warning
{
my $w = $warning;
undef $warning;
$w;
}
sub list_equal
{
my ($eq, $a, $b) = @_;
die "lequal: \$a and \$b not lists"
unless ref $a eq 'ARRAY' && ref $b eq 'ARRAY';
return 0 unless @$a == @$b; # compare lengths
my @eq = map { &$eq ($a->[$_], $b->[$_]) } (0 .. $#$a);
return 0 unless (grep ($_ == 1, @eq)) == @eq;
}
sub slist_equal
{
my ($a, $b) = @_;
list_equal (sub { $_[0] eq $_[1] }, $a, $b);
}
sub nlist_equal
{
my ($a, $b) = @_;
list_equal (sub { $_[0] == $_[1] }, $a, $b);
}
sub clear_values
{
my ($types, $vals) = @_;
my ($k, $t);
foreach $k (keys %$types)
{
$t = $types->{$k};
($t =~ /^[bns]$/) and undef $vals->{$k};
($t =~ /^[ns]l$/) and @{$vals->{$k}} = ();
}
}
sub values_equal
{
my ($types, $a, $b) = @_;
my ($k, $t);
# return 0
# unless slist_equal ([keys %$types], [keys %$a]) &&
# slist_equal ([keys %$a], [keys %$b]);
foreach $k (keys %$types)
{
$t = $types->{$k};
# next unless exists $a->{$k} && exists $b->{$k};
# first make sure that the defined-ness of $a->{$k} and $b->{$k}
# are the same
!(defined $a->{$k} xor defined $b->{$k}) || return 0;
# now the type-dependent comparison
($t eq 'b') && (( !($a->{$k} xor $b->{$k}) || return 0), next);
($t eq 'n') && (($a->{$k} == $b->{$k} || return 0), next);
($t eq 's') && (($a->{$k} eq $b->{$k} || return 0), next);
($t eq 'nl') && ((nlist_equal ($a->{$k}, $b->{$k}) || return 0), next);
($t eq 'sl') && ((slist_equal ($a->{$k}, $b->{$k}) || return 0), next);
die "unknown type \"$t\"";
}
return 1;
}
sub test
{
my ($ok) = @_;
printf "%s %d\n", ($ok ? "ok" : "not ok"), ++$test_count;
}
sub test_parse
{
my ($opt_table, $args, $types, $values,
$exp_leftovers, $exp_values, $exp_output, $exp_error) = @_;
my ($k, $leftovers, $ok);
$SIG{'__WARN__'} = \&catch_warn;
clear_values ($types, $values);
$leftovers = [];
GetOptions ($opt_table, $args, $leftovers);
delete $SIG{'__WARN__'};
$ok = 1;
unless (slist_equal ($leftovers, $exp_leftovers))
{
warn "leftovers don't match\n";
$ok = 0;
}
unless (values_equal ($types, $values, $exp_values))
{
warn "values don't match\n";
$ok = 0;
}
if ($exp_error && warning !~ /$exp_error/)
{
warn "warning message doesn't match\n";
$ok = 0;
}
test ($ok);
} # &test
my @foo = ();
sub get_foo
{
my ($arg, $args) = @_;
my $next;
# print "Hello, you have used the $arg option\n";
unless (@$args)
{
&Getopt::Tabular::SetError
("bad_foo", "no arguments found for $arg option");
return 0;
}
while ($next = shift @$args)
{
last if $next =~ /^-/;
push (@foo, $next);
# print "Got $next from \@\$args\n";
}
if (defined $next) # not the last option?
{
# print "Putting $next back on \@\$args\n";
unshift (@$args, $next);
}
1;
}
my %vals =
(ints => [],
float => undef,
string => undef,
flag => undef);
my %types =
(ints => 'nl',
float => 'n',
string => 's',
flag => 'b');
my @opt_table =
(['-int', 'integer', 2, $vals{ints},
'two integers', 'i1 i2'],
['-float', 'float', 1, \$vals{float},
'a floating-point number' ],
['-string', 'string', 1, \$vals{string},
'a string' ],
['-flag', 'boolean', 0, \$vals{flag},
'a boolean flag' ],
['-foo', 'call', 0, \&get_foo,
'do nothing important'],
['-show', 'eval', 0, 'print "Ints = @Ints\n";',
'print the current values of -int option']
);
# command line with no options: leftovers should be same as whole arg list
test_parse (\@opt_table,
[qw(hello there)],
\%types, \%vals,
[qw(hello there)],
{ ints => [] }, '', '');
# with options, but no leftovers:
test_parse (\@opt_table,
[qw(-int 3 4 -string FOO!)],
\%types, \%vals,
[],
{ ints => [3, 4], string => 'FOO!' },
'', '');
# options and leftovers mixed up together
test_parse (\@opt_table,
[qw(hello -int 2 -5 there -string barf)],
\%types, \%vals,
[qw(hello there)],
{ ints => [2, -5], string => 'barf' },
'', '');
# similar, but add boolean option
test_parse (\@opt_table,
[qw(-flag how -int 2 -5 are you -string frab)],
\%types, \%vals,
[qw(how are you)],
{ ints => [2, -5], string => 'frab', flag => 1 },
'', '');
# now add callback option
test_parse (\@opt_table,
[qw(-flag how -int 2 -5 are you -foo x1 x2 -string frab)],
\%types, \%vals,
[qw(how are you)],
{ ints => [2, -5], string => 'frab', flag => 1 },
'', '');
test (slist_equal (\@foo, [qw(x1 x2)]));
# same, but with a negation of the boolean option later in the arg list
# and a different way of using the callback
test_parse (\@opt_table,
[qw(-flag bang -int 2 -5 pow! -noflag -foo bing bong bang)],
\%types, \%vals,
[qw(bang pow!)],
{ ints => [2, -5], flag => 0 },
'', '');
test (slist_equal (\@foo, [qw(x1 x2 bing bong bang)]));
# still need to test:
# argument errors (ie. warnings)
# table errors (catch `die')
# custom patterns (eg. uppercase string)
# spoof parsing