Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 186 lines (161 sloc) 3.721 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
use v6;

use Test;
# Test of PRE and POST traits
#
# L<S04/Phasers/"assert precondition at every block ">
# L<S06/Subroutine traits/PRE/POST>

plan 22;

sub foo(Int $i) {
    PRE {
        $i < 5
    }
    return 1;
}

sub bar(Int $i) {
    return 1;
    POST {
        $i < 5;
    }
}

lives_ok { foo(2) }, 'sub with PRE compiles and runs';
lives_ok { bar(3) }, 'sub with POST compiles and runs';

#?pugs todo
dies_ok { foo(10) }, 'Violated PRE throws (catchable) exception';
dies_ok { bar(10) }, 'Violated POST throws (catchable) exception';

# multiple PREs und POSTs

sub baz (Int $i) {
PRE {
$i > 0
}
PRE {
$i < 23
}
return 1;
}
lives_ok { baz(2) }, 'sub with two PREs compiles and runs';

#?pugs todo
dies_ok { baz(-1)}, 'sub with two PREs fails when first is violated';
#?pugs todo
dies_ok { baz(42)}, 'sub with two PREs fails when second is violated';


sub qox (Int $i) {
return 1;
POST {
$i > 0
}
POST {
$i < 42
}
}

lives_ok({ qox(23) }, "sub with two POSTs compiles and runs");
#?pugs todo
dies_ok( { qox(-1) }, "sub with two POSTs fails if first POST is violated");
dies_ok( { qox(123)}, "sub with two POSTs fails if second POST is violated");


class Another {
    method test(Int $x) {
        return 3 * $x;
        POST {
            $_ > 4
        }
    }
}

my $pt = Another.new;
#?pugs todo
lives_ok { $pt.test(2) }, 'POST receives return value as $_ (succeess)';
dies_ok { $pt.test(1) }, 'POST receives return value as $_ (failure)';

{
    my $str;
    {
        PRE { $str ~= '('; 1 }
        POST { $str ~= ')'; 1 }
        $str ~= 'x';
    }
    #?pugs todo
    is $str, '(x)', 'PRE and POST run on ordinary blocks';
}

{
    my $str;
    {
        POST { $str ~= ')'; 1 }
        LEAVE { $str ~= ']' }
        ENTER { $str ~= '[' }
        PRE { $str ~= '('; 1 }
        $str ~= 'x';
    }
    #?pugs todo
    is $str, '([x])', 'PRE/POST run outside ENTER/LEAVE';
}

{
    my $str;
    try {
        {
            PRE { $str ~= '('; 0 }
            PRE { $str ~= '*'; 1 }
            ENTER { $str ~= '[' }
            $str ~= 'x';
            LEAVE { $str ~= ']' }
            POST { $str ~= ')'; 1 }
        }
    }
    #?pugs todo
    #?rakudo.moar todo 'failing PRE block should not run any other phasers, RT 121531'
    is $str, '(', 'failing PRE runs nothing else';
}

#?rakudo.jvm todo "nigh"
{
    my $str;
    try {
        {
            POST { $str ~= 'z'; 1 }
            POST { $str ~= 'x'; 0 }
            LEAVE { $str ~= 'y' }
        }
    }
    #?pugs todo
    is $str, 'yx', 'failing POST runs LEAVE but not more POSTs';
}

#?niecza skip 'unspecced'
{
    my $str;
    try {
        POST { $str ~= $! // '<undef>'; 1 }
        die 'foo';
    }
    #?pugs todo
    ok $str ~~ /foo/, 'POST runs on exception, with correct $!';
}

#?niecza skip 'unspecced'
#?rakudo todo 'POST and exceptions'
{
    my $str;
    try {
        POST { $str ~= (defined $! ?? 'yes' !! 'no'); 1 }
        try { die 'foo' }
        $str ~= (defined $! ?? 'aye' !! 'nay');
    }
    #?pugs todo
    is $str, 'ayeno', 'POST has undefined $! on no exception';
}

#?niecza skip 'unspecced'
#?rakudo skip "POST and exceptions"
{
    try {
        POST { 0 }
        die 'foo';
    }
    #?pugs todo
    ok $! ~~ /foo/, 'failing POST on exception doesn\'t replace $!';
    # XXX
    # is $!.pending.[-1], 'a POST exception', 'does push onto $!.pending';
}

{
    my sub blockless($x) {
        PRE $x >= 0;
        POST $_ == 4;
        return $x;
    }
    #?pugs todo
    lives_ok { blockless(4) }, 'blockless PRE/POST (+)';
    dies_ok { blockless -4 }, 'blockless PRE/POST (-, 1)';
    dies_ok { blockless 14 }, 'blockless PRE/POST (-, 2)';
}

# vim: ft=perl6
Something went wrong with that request. Please try again.