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 89 lines (61 sloc) 1.612 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
use v6;

use Test;

=begin pod

Tests for precedence of self defined operators

=end pod

# L<S06/Subroutine traits/"relative to an existing">

plan 10;

do {
    sub prefix:<!> (Int $x) is tighter(&infix:<**>) {
        return 3 * $x;
    }
    #?rakudo todo 'changing precedence of already declared op'
    is !1**2, 9, "'is tighter' on prefix works";
}

do {
    sub prefix:<foo> (Int $x) is looser(&infix:<+>) {
        return 2 * $x;
    }

    is foo 2 + 3, 10, "'is looser' on prefix works";
}


{
    sub postfix:<!> (Int $x) is tighter(&infix:<**>) {
        return 2 * $x;
    }

    is 3**1!, 9, "'is tighter' on postfix works";
}


{
    sub infix:<mul> ($a, $b) is looser(&infix:<+>) {
        return $a * $b;
    }

    is 2 mul 3 + 4, 14, "'is looser' infix works 1";
    is 4 + 3 mul 2 , 14, "'is looser' infix works 2";
}

{
    sub infix:<div> ($a, $b) is equiv(&infix:<*>) {
        return $a / $b;
    }

    ok((4 div 2 * 3) == 6, "'is equiv' works");
}

# prefix/postfix precedence

{
    sub prefix:<'foo1'> (Int $x) {
        return 2 * $x;
    }

    sub postfix:<'bar1'> (Int $x) is looser(&prefix:<'foo1'>){
        return 1 + $x;
    }

    is('foo1'3'bar1', 7, "Postifix declared looser than prefix");

    sub postfix:<'bar2'> (Int $x) is tighter(&prefix:<'foo1'>){
        return 1 + $x;
    }

    is('foo1'3'bar2', 8, "Postfix declared tighter than prefix");
}

{
    sub postfix:<!> ($n) {
        return [*] 1..$n;
    }

    is( -1!, -1 , "Should be parsed as '-(1!)'");
    dies_ok( { EVAL '4 !' }, "Whitespace not allowed before user-defined postfix");
}


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