Skip to content
This repository
tree: 56b506ae1f
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 185 lines (164 sloc) 5.373 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
my module Test;

constant $?TRANSPARENT = 1;

class Builder {
    has $!current-test;
    has $!set-plan;
    has $!todo-up-to = 0;
    has $!todo-reason;

    method new() {
        $*TEST-BUILDER;
    }

    method blame() {
        my $frame = caller;
        while $frame.hints('$?TRANSPARENT') {
            $frame = $frame.caller;
        }
        $frame.file ~ " line " ~ $frame.line;
    }

    method !output($text) {
        say $text;
    }

    method reset() {
        $!current-test = 1;
    }

    method note($m) {
        self!output("# " ~ $m);
        0;
    }

    method ok($bool, $tag) {
        my $not = $bool ?? "" !! "not ";
        my $desc;
        if $tag {
            $desc = " - " ~ $tag.subst('#', '\#').split("\n").join("\n#");
        } else {
            $desc = '';
        }
        if $!todo-up-to >= $!current-test {
            $desc ~= " # TODO $!todo-reason";
        }
        self!output($not ~ "ok " ~ $!current-test++ ~ $desc);
        if !$bool { self.note(self.blame); }
        $bool;
    }

    method todo($reason, $count) {
        $!todo-reason = $reason;
        $!todo-up-to = $!current-test + $count - 1; # todo(1) should stop after cur
    }

    method skip($reason) {
        self!output("ok {$!current-test++} # SKIP $reason");
    }

    method expected-tests($num) {
        self!output("1.." ~ $num);
    }

    method plan($x) {
        $!set-plan = 1;
        if $x ~~ Cool {
            self.expected-tests(+$x);
        } else {
            die "Invalid argument to plan";
        }
    }

    method done {
        if !($!set-plan) {
            self!output("1.." ~ ($!current-test - 1));
        }
    }
}

INIT {
    $GLOBAL::TEST-BUILDER = Builder.bless(*);
    $GLOBAL::TEST-BUILDER.reset;
}

sub cmp_ok(\a, $fn, \b, $tag?) is export { ok($fn(a, b), $tag); }
sub ok(\bool, $tag?) is export { $*TEST-BUILDER.ok(?bool, $tag) }
sub nok(\bool, $tag?) is export { $*TEST-BUILDER.ok(!bool, $tag) }
sub skip_rest($tag?) is export { } #OK
sub pass($tag?) is export { $*TEST-BUILDER.ok(1, $tag); True }
sub flunk($tag?) is export { $*TEST-BUILDER.ok(0, $tag) }
sub isa_ok(Mu $obj, Mu $type, $tag?) is export { $*TEST-BUILDER.ok($obj.^isa($type), $tag) }
sub is_deeply($a,$b,$c) is export { is $a.perl, $b.perl, $c }
sub is(\got, \expected, $tag?) is export {

    # avoid comparing twice
    my $sgot = ~(got // '');
    my $sexpexted = ~(expected // '');
    my $equal = $sgot eq $sexpexted;

    $*TEST-BUILDER.ok($equal, $tag);
    if !$equal {
        $*TEST-BUILDER.note(' Failed test');
        $*TEST-BUILDER.note(" got: $sgot");
        $*TEST-BUILDER.note(" expected: $sexpexted");
    }
}
sub isnt(Mu $got, Mu $expected, $tag?) is export { $*TEST-BUILDER.ok($got ne $expected, $tag) }
# Runs $code, trapping various failure modes and returning applicable.
sub no-control($code, :$diag) {
    my ($died, $warned);
    {
        CATCH {
            $*TEST-BUILDER.note("Exception: $_") if $diag;
            default { $died = True }
        }
        CONTROL {
            if .[0] == 11 {
                $warned = True;
                return; # NIECZA - causes &warn to return
            }
            when .[0] != 11 { $died = True } # exits block
        }
        $code.();
    }
    $died ?? "die" !! $warned ?? "warn" !! "";
}
sub lives_ok($code,$why?) is export {
    $*TEST-BUILDER.ok(no-control($code, :diag) ne "die", $why);
}
sub dies_ok($code,$why?) is export {
    $*TEST-BUILDER.ok(no-control($code) eq "die", $why);
}
sub succeeds_ok($code,$why?,:$ignore = ()) is export {
    $*TEST-BUILDER.ok(?(no-control($code, :diag) eq any("", @$ignore)), $why);
}
sub fails_ok($code,$why?,:$expect = <die warn fail>) is export {
    $*TEST-BUILDER.ok(?(no-control($code) eq any(@$expect)), $why);
}
sub eval_lives_ok($code,$why?) is export {
    $*TEST-BUILDER.ok(no-control({ eval $code }, :diag) ne "die", $why);
}
sub eval_dies_ok($code,$why?) is export {
    $*TEST-BUILDER.ok(no-control({ eval $code }) eq "die", $why);
}
sub eval_succeeds_ok($code,$why?,:$ignore = ()) is export {
    $*TEST-BUILDER.ok(?(no-control({ eval $code }) eq any("", @$ignore)), $why);
}
sub eval_fails_ok($code,$why?,:$expect = <die warn fail>) is export {
    $*TEST-BUILDER.ok(?(no-control({ eval $code }) eq any(@$expect)), $why);
}
sub diag($str) is export { $*TEST-BUILDER.note($str) }
sub is_approx(Mu $got, Mu $expected, $desc = '') is export {
    my $tol = $expected.abs < 1e-6 ?? 1e-5 !! $expected.abs * 1e-6;
    my $test = ($got - $expected).abs <= $tol;
    $*TEST-BUILDER.ok(?$test, $desc);
    unless $test {
        $*TEST-BUILDER.note("got: $got");
        $*TEST-BUILDER.note("expected: $expected");
    }
    ?$test;
}
sub todo($reason="", $count = 1) is export { $*TEST-BUILDER.todo($reason, $count) }
sub plan($num) is export { $*TEST-BUILDER.plan($num) }
sub done() is export { $*TEST-BUILDER.done }
sub skip($reason,$number) is export {
    $*TEST-BUILDER.skip($reason) for ^$number;
}

# TODO standardize me
sub rxtest($rgx, $rgxname, @y, @n) is export {
    for @y {
        my $k = $_ ~~ Pair ?? $_.key !! $_;
        my $v = $_ ~~ Pair ?? $_.value !! $_;
        ok $k ~~ $rgx, "$rgxname ~~ $v";
    }
    for @n {
        my $k = $_ ~~ Pair ?? $_.key !! $_;
        my $v = $_ ~~ Pair ?? $_.value !! $_;
        ok !($k ~~ $rgx), "$rgxname !~~ $v";
    }
}


Something went wrong with that request. Please try again.