forked from rakudo-p5/roast5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fudge
executable file
·270 lines (241 loc) · 7.35 KB
/
fudge
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
267
268
269
270
#!/usr/bin/perl
use strict;
use warnings;
my %OPTS;
while( $_ = $ARGV[0], /^-/ ) {
shift;
last if /^--$/;
$OPTS{$_} = $_;
}
my $ME; # implementation
my $IN; # test file
my $OUT; # fudged file
if (-f ".spec_config") {
open my $cf, "<", ".spec_config";
while (<$cf>) {
if (m/^\bname\b\s*=\s*(\w+)/) {
$ME = $1;
}
}
}
if (@ARGV == 3) {
# impl test fudged
$ME = shift;
$IN = shift;
$OUT = shift;
} elsif (@ARGV == 1) {
# test
$IN = shift;
} elsif (@ARGV == 2) {
my $arg = shift;
if ($arg =~ /\.t$/) {
# test fudged
$IN = $arg;
$OUT = shift;
} else {
# impl test
$ME = $arg;
$IN = shift;
}
}
if (!$OUT and $IN) {
($OUT = $IN) =~ s/\.t$/.$ME/ or $OUT .= ".$ME";
}
unless ($ME and $IN and $OUT) {
die <<"USAGE";
Usage: $0 [options] [implname] testfilename [fudgedtestfilename]
implname, if not specified on the command line, is pulled from the
.spec_config file in your compiler's directory.
Options:
--keep-exit-code
by default, fudge modifies the exit code for fudged test files to 1.
supplying this option will suppress that behavior.
Verbs:
#?implname [num] skip 'reason'
comment out num tests or blocks and call skip(num)
#?implname [num] eval 'reason'
eval num tests or blocks and skip(num) on parsefail
#?implname [num] try 'reason'
try num tests or blocks and fail on exception
#?implname [num] todo 'reason', :by<1.2.3>
run num tests or blocks with todo() preset
#?implname emit your_ad_here();
just pass through your_ad_here();
#?DOES count
for all implementations, the following thing does count tests
(disables any attempt to autocount tests within the construct)
when construct is a sub, registers the sub name as tester
(and multiplies calls to tester sub by count tests)
where
implname is the lc name of your implementation, e.g. pugs or rakudo
num is the number of statements or blocks to preprocess, defaults to 1
count is how many tests the following construct counts as
USAGE
}
unless (-e $IN) {
die "$0: No such test file '$IN'\n";
}
unlink $OUT; # old fudged version, may or may not regenerate...
my $REALLY_FUDGED = 0;
my $OUTPUT = "";
my $FUDGE = "";
our $PENDING = 0;
my $ARGS = '';
my $IS = '\\b(?:is|ok|nok|is_deeply|is_approx|isnt|like|unlike|eval_dies_ok|cmp_ok|isa_ok|use_ok|throws_ok|dies_ok|lives_ok|eval_lives_ok|pass|flunk|throws_like)(?:\\b|_)';
my %DOES;
my $DOES = 0;
my $EXIT = $OPTS{'--keep-exit-code'} ? '' : 'exit(1);';
@ARGV = ($IN);
fudgeblock();
if ($OPTS{'--add_use_v5'}) {
$REALLY_FUDGED = 1;
$OUTPUT = "use v5;\n$OUTPUT";
}
if ($REALLY_FUDGED) {
open OUT, ">", $OUT or die "Can't create $OUT: $!";
print OUT $OUTPUT;
print OUT <<"END";
say "# FUDGED!";
$EXIT
END
close OUT;
print "$OUT\n"; # pick the output file to run
}
else {
print "$IN\n"; # pick the input file to run
}
sub fudgeblock {
while (<>) {
if (/^\s*\#\?DOES[:\s] \s* (.*)/x) {
$DOES = $1;
next;
}
if (/^\s*\#\? (\S+?)[:\s] \s* ((\S*).*)/x) {
if ($1 eq $ME) {
$REALLY_FUDGED = 1;
$ARGS = $2;
if ($ARGS =~ s/^emit\s*//) {
$_ = $ARGS;
next;
}
if ($ARGS =~ s/^(\d+)\s*//) {
$PENDING = $1;
}
else {
$PENDING = 1;
}
$ARGS =~ s/^(\w+)\s*//;
$FUDGE = $1;
} elsif ($3 eq 'emit') {
$_ = '';
next;
}
}
next if /^\s*#/;
next if /^\s*$/;
if ($DOES) {
if (/^\s*(sub|multi|proto)\b/) {
my $tmp = $_;
$tmp =~ s/^\s*proto\s+//;
$tmp =~ s/^\s*multi\s+//;
$tmp =~ s/^\s*sub\s+//;
$tmp =~ /^(\w+)/;
$DOES{$1} = $DOES;
$DOES = 0;
next;
}
}
next unless $PENDING > 0;
if (/^\{/) {
$PENDING--;
if ($FUDGE eq 'todo') {
local $PENDING = 999999; # do all in block as one action
$OUTPUT .= $_;
$DOES = 0; # XXX ignore?
fudgeblock();
$_ = '';
}
else {
my $more;
while (defined($more = <>)) {
$_ .= $more;
last if $more =~ /^\}/;
}
my $numtests = $DOES || do {
my $tmp = $_;
my $nt = 0;
$nt += $1 while $tmp =~ s/^#\?DOES[:\s]\s*(\d+).*\n.*\n//m;
if (%DOES) {
my $does = join('|',keys(%DOES));
$nt += $DOES{$1} while $tmp =~ s/^\s*($does)\b//mx;
}
$nt += () = $tmp =~ m/^(\s*$IS)/mgx;
$nt;
};
if ($FUDGE eq 'skip') {
s/^/# /mg;
$_ = "skip($ARGS, $numtests);" . $_;
}
elsif ($FUDGE eq 'try') {
chomp;
$_ = "(try $_) // flunk($ARGS);\n";
}
elsif ($FUDGE eq 'eval') {
chomp;
s/(['\\])/\\$1/g;
$_ = "eval('$_') // skip($ARGS, $numtests);\n";
}
else {
warn "Don't know how to mark block for $FUDGE!\n";
}
}
}
else {
if ($FUDGE eq 'todo') {
$DOES = 0; # XXX ignore?
my $does = join '|', keys %DOES;
$PENDING -= s/^(\s*)/${1}todo($ARGS); / if /^\s*(?:$IS|$does)\b/;
}
else {
while ($_ !~ /;[ \t]*(#.*)?$/) {
my $more = <>;
last unless $more;
$_ .= $more;
}
my ($keyword) = /^\s*(\w+)/ || '';
my $numtests;
if ($DOES{$keyword}) {
$numtests = $DOES{$keyword};
}
elsif ($DOES) {
$numtests = $DOES;
}
else {
my $does = join '|', keys %DOES;
next unless /^\s*($IS|$does)/;
$numtests = defined $DOES{$1}? $DOES{$1} : 1;
}
$PENDING--;
$_ = "{ " . $_ . " }";
if ($FUDGE eq 'skip') {
s/^/# /mg;
$_ = "skip($ARGS, $numtests); $_\n";
}
elsif ($FUDGE eq 'try') {
$_ = "(try $_) // flunk($ARGS);\n";
}
elsif ($FUDGE eq 'eval') {
s/(['\\])/\\$1/g;
$_ = "eval('$_') // skip($ARGS, $numtests);\n";
}
else {
warn "Don't know how to mark statement for $FUDGE!\n";
}
}
}
}
continue {
$OUTPUT .= $_;
return if /^\}/ and $PENDING > 0;
}
}