Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: rakudo-master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 234 lines (208 sloc) 6.598 kb
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
#!/usr/bin/perl

use strict;
use warnings;

my %OPTS;
while( $_ = $ARGV[0], /^-/ ) {
    shift;
    last if /^--$/;
    $OPTS{$_} = $_;
}

my $ME = shift;
my $IN = shift;
my $OUT = shift;

if (!$OUT and $IN) {
    ($OUT = $IN) =~ s/\.t$/.$ME/ or $OUT .= ".$ME";
}
unless (-e $IN) {
    die "$0: No such test file '$IN'\n";
}
unless ($ME and $IN and $OUT) {

    die <<"USAGE";
Usage: $0 [options] implname testfilename [fudgedtestfilename]

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
}

if (-e $OUT) {
    if (-M $IN >= -M $OUT and -M $0 >= -M $OUT) {
        print "$OUT\n"; # unchanged, so no need to refudge
        exit(0);
    }
    else {
        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)(?:\\b|_)';
my %DOES;
my $DOES = 0;
my $EXIT = $OPTS{'--keep-exit-code'} ? '' : 'exit(1);';

@ARGV = ($IN);
fudgeblock();

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* (.*)/x and $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;
        }

        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?
                $PENDING -= s/^(\s*)/${1}todo($ARGS); / if /^\s*$IS/;
            }
            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 {
                    next unless /^\s*$IS/;
                    $numtests = 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;
    }
}
Something went wrong with that request. Please try again.