Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (127 sloc) 5.581 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
use v6;

use Test;

plan 50;

# L<S06/Required parameters/method:>
sub a_zero () { };
sub a_one ($a) { }; #OK not used
sub a_two ($a, $b) { }; #OK not used
sub a_three ($a, $b, @c) { }; #OK not used
sub a_four ($a, $b, @c, %d) { }; #OK not used

sub o_zero ($x?, $y?) { }; #OK not used
sub o_one ($x, :$y) { }; #OK not used
sub o_two ($x, :$y!, :$z) { }; #OK not used

is &a_zero.arity, 0, '0 arity &sub';
is &a_one.arity, 1, '1 arity &sub';
is &a_two.arity, 2, '2 arity &sub';
is &a_three.arity, 3, '3 arity &sub';
is &a_four.arity, 4, '4 arity &foo';

#?pugs 5 skip '.count'
is &a_zero.count, 0, '0 count &sub';
is &a_one.count, 1, '1 count &sub';
is &a_two.count, 2, '2 count &sub';
is &a_three.count, 3, '3 count &sub';
is &a_four.count, 4, '4 count &foo';

#?pugs 3 todo
is &o_zero.arity, 0, 'arity 0 sub with optional params';
is &o_one.arity, 1, 'arity 1 sub with optional params';
is &o_two.arity, 1, 'arity with optional and required named params';

#?pugs 3 skip '.count'
is &o_zero.count, 2, 'count on sub with optional params';
is &o_one.count, 1, 'count on sub with optional params';
is &o_two.count, 1, 'count on sub with optional and required named params';

#?pugs skip 'parsefail'
{
    sub b_zero () { };
    sub b_one ($) { };
    sub b_two ($, $) { };
    sub b_three ($, $, @) { };
    sub b_four ($, $, @, %) { };
    is &b_zero.arity, 0, '0 arity &sub (sigils only)';
    is &b_one.arity, 1, '1 arity &sub (sigils only)';
    is &b_two.arity, 2, '2 arity &sub (sigils only)';
    is &b_three.arity, 3, '3 arity &sub (sigils only)';
    is &b_four.arity, 4, '4 arity &foo (sigils only)';

}

# It's not really specced in what way (*@slurpy_params) should influence
# .arity. Also it's unclear what the result of &multisub.arity is.
# See the thread "&multisub.arity?" on p6l started by Ingo Blechschmidt for
# details:
# L<http://thread.gmane.org/gmane.comp.lang.perl.perl6.language/4915>

{
    is ({ $^a }.arity), 1,
        "block with one placeholder var has .arity == 1";
    #?pugs 4 skip "is multi"
    is (-> $a { $a }.arity), 1,
        "pointy block with one placeholder var has .arity == 1";
    is { $^a,$^b }.arity, 2,
        "block with two placeholder vars has .arity == 2";
    is (-> $a, $b { $a,$b }).arity, 2,
        "pointy block with two placeholder vars has .arity == 2";
    is { $^a,$^b,$^c }.arity, 3,
        "block with three placeholder vars has .arity == 3";
    is (-> $a, $b, $c { $a,$b,$c }).arity, 3,
        "pointy block with three placeholder vars has .arity == 3";
    
    #?pugs 6 skip ".count"
    is ({ $^a }.count), 1,
        "block with one placeholder var has .count == 1";
    is (-> $a { $a }.count), 1,
        "pointy block with one placeholder var has .count == 1";
    is { $^a,$^b }.count, 2,
        "block with two placeholder vars has .count == 2";
    is (-> $a, $b { $a,$b }).count, 2,
        "pointy block with two placeholder vars has .count == 2";
    is { $^a,$^b,$^c }.count, 3,
        "block with three placeholder vars has .count == 3";
    is (-> $a, $b, $c { $a,$b,$c }).count, 3,
        "pointy block with three placeholder vars has .count == 3";
}

#?pugs skip "is multi"
{
    is { my $k; $^a }.arity, 1, #OK not used
        "additional my() vars don't influence .arity calculation (1-1)";
    is { my $k; $^a,$^b }.arity, 2, #OK not used
        "additional my() vars don't influence .arity calculation (1-2)";
    is { my $k; $^a,$^b,$^c }.arity, 3, #OK not used
        "additional my() vars don't influence .arity calculation (1-3)";

    is { my $k; $^a }.count, 1, #OK not used
        "additional my() vars don't influence .count calculation (1-1)";
    is { my $k; $^a,$^b }.count, 2, #OK not used
        "additional my() vars don't influence .count calculation (1-2)";
    is { my $k; $^a,$^b,$^c }.count, 3, #OK not used
        "additional my() vars don't influence .count calculation (1-3)";
}

#?pugs skip 'is multi'
{
    is { $^a; my $k }.arity, 1, #OK not used
        "additional my() vars don't influence .arity calculation (2-1)";
    is { $^a,$^b; my $k }.arity, 2, #OK not used
        "additional my() vars don't influence .arity calculation (2-2)";
    is { $^a,$^b,$^c; my $k }.arity, 3, #OK not used
        "additional my() vars don't influence .arity calculation (2-3)";

    is { $^a; my $k }.count, 1, #OK not used
        "additional my() vars don't influence .count calculation (2-1)";
    is { $^a,$^b; my $k }.count, 2, #OK not used
        "additional my() vars don't influence .count calculation (2-2)";
    is { $^a,$^b,$^c; my $k }.count, 3, #OK not used
        "additional my() vars don't influence .count calculation (2-3)";
}

# used to be a bug in Rakudo, RT #63744
#?pugs skip 'parsefail'
{
    sub indirect-count(Code $c) { +$c.signature.params; }
    my $tester = -> $a, $b, $c? { ... }; #OK not used
    is +$tester.signature.params, 3, '+$obj.signature.params work';
    is +$tester.signature.params, indirect-count($tester),
       '... also when passed to a sub first';
}

#?pugs todo
dies_ok { eval("a_zero( 'hello', 'world' )") }, 'no matching sub signature';

#?pugs skip 'parsefail'
{
    my proto sub a($, $?) { * }
    my multi sub a($) { 1 }
    my multi sub a($, $) { 2 }
    is &a.count, 2, '&multi.count';
    is &a.arity, 1, '&multi.arity';
}

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