Skip to content
This repository
tag: v12
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 212 lines (186 sloc) 7.744 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
# vim: ft=perl6
use Test;
use MONKEY_TYPING;

{
    my class Bt {
        has $!pie;
        method get_pie() { $!pie }
        submethod BUILD(:$x) { $!pie = $x }
    }
    is Bt.new(x => 5).get_pie, 5, "BUILD basically works";
    my class SubBT is Bt {
        has $!pie2;
        method get_pie2() { $!pie2 }
        submethod BUILD(:$y) { $!pie2 = $y }
    }
    is SubBT.new(x => 5, y => 2).get_pie, 5, "superclass' BUILD in subclass";
    is SubBT.new(x => 5, y => 2).get_pie2, 2, "subclass' BUILD in subclass";

    my @l;
    @l := [1,2,3];
    is +[@l], 3, 'binding to existing list vars works';
}

is [ 1,2,3 ... 10 ], [1..10];
is [ 1,2,4 ... 256 ], [map 2 ** *, 0..8];
is [ 1,1,*+* ...^ *>100 ], [1,1,2,3,5,8,13,21,34,55,89];

eval_lives_ok q[
class F2855::G7136 { ... }
class F2855::G7136 { }
], "can stub then define nested classes";

{
    my @l = gather for 1,2 { take $_ };
    is ~@l, "1 2", "gather for works";

    eval_dies_ok 'class { has $!foo; has $!foo; }',
        "double attribute declaration caught";

    eval_dies_ok 'class { method abar {}; method abar {}; }',
        "double method declaration caught";

    # <chain> isn't tested here. It's not possible to do the same AST
    # reconstruction tricks. However if <right> etc work, and chained
    # comparisons work, it's pretty likely to work combinationally.
    sub infix:<@a> { "a(@_.Str())" }
    sub infix:<@b> is assoc<right> { "b(@_.Str())" }
    sub infix:<@c> is assoc<list> { "c(@_.Str())" }
    sub infix:<@d> is assoc<list> { "d(@_.Str())" } #OK not used
    sub infix:<@e> is assoc<non> { "e(@_.Str())" }
    sub infix:<@f> is assoc<left> { "f(@_.Str())" }

    is (1 @a 2), 'a(1 2)', 'basic operator function';
    is (1 @a 2 @a 3), 'a(a(1 2) 3)', 'operators default to left assoc';
    is (1 @f 2 @f 3), 'f(f(1 2) 3)', 'explicit assoc<left> works too';
    is (1 @f 2 @a 3), 'a(f(1 2) 3)', 'mixed <left> at same prec works (1)';
    is (1 @a 2 @f 3), 'f(a(1 2) 3)', 'mixed <left> at same prec works (2)';
    is (1 @b 2 @b 3), 'b(1 b(2 3))', 'assoc<right> overrides';
    is (1 @c 2 @c 3), 'c(1 2 3)', 'assoc<list> takes all 3 at once';
    eval_dies_ok q[1 @c 2 @d 3], 'mixed <list> at same prec dies';
    eval_dies_ok q[1 @e 2 @e 3], '<non> dies with 3';
    is (1 @e 2), 'e(1 2)', '<non> with 2 works';

    sub infix:<@g> is tighter<@a> { "g(@_.Str())" } #OK not used
    sub infix:<@h> is looser<@a> { "h(@_.Str())" } #OK not used
    sub infix:<@i> is tighter(&infix:<@a>) { "i(@_.Str())" } #OK not used
    sub infix:<@j> is looser(&infix:<@a>) { "j(@_.Str())" } #OK not used
    sub infix:<@k> is tighter<@h> { "k(@_.Str())" } #OK not used
    sub infix:<@l> is looser<@g> { "l(@_.Str())" } #OK not used
    sub infix:<@m> is equiv<@a> { "m(@_.Str())" } #OK not used
    sub infix:<@n> is equiv(&infix:<@a>) { "n(@_.Str())" } #OK not used
    sub infix:<@o> is equiv<@g> { "o(@_.Str())" } #OK not used
    sub infix:<@p> is equiv<@h> { "p(@_.Str())" } #OK not used
    sub infix:<@q> is equiv<@b> { "q(@_.Str())" } #OK not used

    my @cmptests = (
        'a', 'g', 1, 0, 'tighter<> works',
        'h', 'a', 1, 0, 'looser<> works',
        'a', 'i', 1, 0, 'tighter<> works with code object',
        'j', 'a', 1, 0, 'looser<> works with code object',
        'h', 'k', 1, 0, 'tighter of a looser works',
        'l', 'g', 1, 0, 'looser of a tighter works',
        'k', 'a', 1, 0, 'tighter of a looser is still looser',
        'a', 'l', 1, 0, 'looser of a tighter is still tighter',
        'm', 'a', 0, 0, 'equiv works',
        'n', 'a', 0, 0, 'equiv works with code object',
        'o', 'g', 0, 0, 'equiv of tighter works',
        'p', 'h', 0, 0, 'equiv of looser works',
        'q', 'q', 1, 1, 'equiv also copies associativity',
    );
    sub ckb($res is copy) { #OK not used
        $res ~~ s:g /<.alpha>//; #::
        $res eq '((1 2) 3)' ?? 0 !! 1;
    }
    my @frags;
    for @cmptests -> $lt, $gt, $right_br_ord, $right_br_opp, $msg {
        push @frags, "is ckb(1 @$lt 2 @$gt 3), $right_br_ord, '$msg (1)';\n";
        push @frags, "is ckb(1 @$gt 2 @$lt 3), $right_br_opp, '$msg (2)';\n";
    }
    eval @frags.join;
}

{
    lives_ok { my $x; $x = Mu },
        "can assign Mu to default-typed variable (noninline)";
    lives_ok { if 1 { my $x; $x = Mu } },
        "can assign Mu to default-typed variable (inline)";
    dies_ok { my Any $x; $x = Mu },
        "cannot assign Mu to Any-typed variable (noninline)";
    dies_ok { if 1 { my Any $x; $x = Mu } },
        "cannot assign Mu to Any-typed variable (inline)";
    ok { my $x; $x }.() === Any,
        "default-typed variable receives Any (noninline)";
    ok { if 1 { my $x; $x } }.() === Any,
        "default-typed variable receives Any (inline)";

    lives_ok { my @x; push @x, Mu }, "can push Mu";
    lives_ok { my @x; push @x, 5; @x[0] = Mu }, "push creates Mu-ready vars";
    lives_ok { my @x; unshift @x, Mu }, "can unshift Mu";
    lives_ok { my @x; unshift @x, 5; @x[0] = Mu }, "unshift creates Mu-ready vars";
    lives_ok { my $x; $x[0] = Mu }, "array creation autoviv supports Mu";
    lives_ok { my @x; @x[0] = Mu }, "element creation autoviv supports Mu";
    lives_ok { my $x; $x<a> = Mu }, "hash creation autoviv supports Mu";
    lives_ok { my %x; %x<a> = Mu }, "hash element creation autoviv supports Mu";
}

# regression test from thou
{
    my $x = 'Bar';
    my $in = qq:to [A] ;
$x Foo
A
is $in.substr(0,8), 'Bar Foo', "spaces preserved after heredoc interpolation";
}

{
ok @*ARGS.flattens, '@*ARGS is a flatteny thing';
ok %*ENV.flattens, '%*ENV is a flatteny thing';
@Y8158::z := [1,2,3];
    ok @Y8158::z.flattens, 'binding to @foo::bar works';
}

# from colomon
{
    isa_ok 1 ** 2, Int, "1 squared is an Int";
    is 1 ** 2, 1, "1 squared is 1";

    isa_ok 2 ** 3, Int, "2 ** 3 is an Int";
    is 2 ** 3, 8, "2 ** 3 is 8";
    isa_ok (2/3) ** 3, Rat, "(2/3) ** 3 is a Rat";
is (2/3) ** 3, 8 / 27, "(2/3) ** 3 is 8 / 27";
isa_ok FatRat.new(2, 3) ** 3, FatRat, "FatRat.new(2, 3) ** 3 is a FatRat";
is FatRat.new(2, 3) ** 3, 8 / 27, "FatRat.new(2, 3) ** 3 is 8 / 27";
isa_ok 2.54e0 ** 3, Num, "2.54e0 ** 3 is an Num";
is 2.54e0 ** 3, 16.387064e0, "2.54e0 ** 3 is 16.387064e0";

isa_ok 2 ** -3, Rat, "2 ** -3 is an Rat"; # spec?
is 2 ** -3, 1/8, "2 ** -3 is 1/8";
isa_ok (2/3) ** -3, Rat, "(2/3) ** -3 is a Rat";
is (2/3) ** -3, 27 / 8, "(2/3) ** -3 is 27 / 8";
isa_ok FatRat.new(2, 3) ** -3, FatRat, "FatRat.new(2, 3) ** -3 is a FatRat";
is FatRat.new(2, 3) ** -3, 27 / 8, "FatRat.new(2, 3) ** -3 is 27 / 8";
isa_ok 2.54e0 ** -3, Num, "2.54e0 ** -3 is an Num";
is_approx (2.54e0 ** -3), 0.0610237440947323, "2.54e0 ** -3 is 0.0610237440947323, more or less";

is_approx 1i ** 2, -1, "1i ** 2 is -1";
is_approx 1i ** 3, -1i, "1i ** 3 is -i";
is_approx 1i ** 4, 1, "1i ** 4 is 1";
}

{
"x" ~~ /./;
    $/.perl; # regression; failure mode was infinite loop
    my $x = 1;
    2 R+= $x;
    is $x, 3, 'R+= works';

    sub foo(@y is copy) { +@y }
    is foo([1,2,4]), 3, '@y is copy works with non-flatteny values';
}

lives_ok { ^2 X ^2 }, 'X works on Ranges';

#is $?ORIG.substr(0,5), '# vim', '$?ORIG works';

# {
# {
# our $x = 5; #OK
# }
# ok $::x == 5, '$::x finds our variable';
#
# package Fao { our $y = 6; } #OK
# ok $::Fao::y == 6, '$::Fao::y works as $Fao::y';
#
# { class Mao { } }
# ok ::Mao.new.defined, 'can use classes via ::Mao';
# }
#
# {
# my $x = 7; #OK
# ok $::x == 7, '$::x can find lexicals';
# class A3 {
# method moo { 42 }
# class B4 {
# ok ::A3.moo, '::A3 can find outer classes';
# }
# }
# }

done;
Something went wrong with that request. Please try again.