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 121 lines (103 sloc) 2.708 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
use v6;

use Test;

plan 25;

# L<S09/Autovivification/In Perl 6 these read-only operations are indeed non-destructive:>
#?pugs todo
{
    my %h;
    my $b = %h<a><b>;
    is %h.keys.elems, 0, "fetching doesn't autovivify.";
    ok $b === Any, 'and the return value is not defined';
}

#?pugs skip ':exists'
{
    my %h;
    my $exists = %h<a><b>:exists;
    is %h.keys.elems, 0, "exists doesn't autovivify.";
    ok $exists === False, '... and it returns the right value';
}

# L<S09/Autovivification/But these bindings do autovivify:>
#?pugs todo
{
    my %h;
    bar(%h<a><b>);
    is %h.keys.elems, 0, "in ro arguments doesn't autovivify.";
}

{
    my %h;
    my $b := %h<a><b>;
    #?pugs todo
    #?niecza todo "https://github.com/sorear/niecza/issues/176"
    is %h.keys.elems, 0, 'binding does not immediately autovivify';
    #?pugs todo
    ok $b === Any, '... to an undefined value';
    $b = 42;
    is %h.keys.elems, 1, '.. but autovivifies after assignment';
    is %h<a><b>, 42, 'having it in there';
    ok %h<a><b> =:= $b, 'check binding';
}

#?pugs todo
{
    my %h;
    my $b = \(%h<a><b>);
    is %h.keys.elems, 0, 'capturing does not autovivify';
}

{
    my %h;
    foo(%h<a><b>);
    #?pugs todo
    #?niecza todo "https://github.com/sorear/niecza/issues/176"
    is %h.keys.elems, 0, 'in rw arguments does not autovivify';
    foo(%h<a><b>,42);
    is %h.keys.elems, 1, 'storing from within the sub does autovivify';
    is %h<a><b>, 42, 'got the right value';
}

{
    my %h;
    %h<a><b> = 42;
    is %h.keys.elems, 1, 'store autovivify.';
    is %h<a><b>, 42, 'got the right value';
}

# helper subs
sub foo ($baz is rw, $assign? ) { $baz = $assign if $assign }
sub bar ($baz is readonly) { }

# RT #77038
#?niecza skip "Unable to resolve method push in type Any"
{
    my %h;
    push %h<a>, 4, 2;
    is %h<a>.join, '42', 'can autovivify in sub form of push';
    unshift %h<b>, 5, 3;
    is %h<b>.join, '53', 'can autovivify in sub form of unshift';
    %h<c><d>.push( 7, 8 );
    is %h<c><d>.join, '78', 'can autovivify in method form of push';
    %h<e><f>.unshift( 9, 10 );
    is %h<e><f>.join, '910', 'can autovivify in method form of unshift';
    is %h.keys.elems, 4, 'successfully autovivified lower level';
}

{
    my $a;
    $a[0] = '4';
    $a[1] = '2';
    is $a.join, '42', 'Can autovivify Array';
}

# RT #77048
{
    my Array $a;
    $a[0] = '4';
    $a[1] = '2';
    is $a.join, '42', 'Can autovivify Array-typed scalar';
}

{
    my $h;
    $h<a> = '4';
    $h<b> = '2';
    is $h<a b>.join, '42', 'Can autovivify Hash';
}

{
    my Hash $h;
    $h<a> = '4';
    $h<b> = '2';
    is $h<a b>.join, '42', 'Can autovivify Hash-typed scalar';
}


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