Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for let/temp preserving holes and containerization #768

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions S04-blocks-and-statements/let.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 15;
plan 17;

# L<S04/The Relationship of Blocks and Declarations/There is also a let function>
# L<S04/Definition of Success>
Expand Down Expand Up @@ -74,7 +74,7 @@ plan 15;
}

# https://github.com/Raku/old-issue-tracker/issues/5057
{
{
my %h{Pair}; %h{a => 1} = 2;
my %c{Pair}; %c{a => 1} = 2;
{
Expand All @@ -97,4 +97,82 @@ plan 15;
is-deeply %h, %c, '`let` keeps Nils around in Hashes when they exist';
}

# https://github.com/rakudo/rakudo/issues/1433
# Make sure that holes and containerization are preserved.
{
subtest "let with Array" => {
plan 12;
my @a;
@a[3] = 42;
@a[2] := 12;

# Control tests
nok @a[1]:exists, "control: there is an expected hole";
isa-ok @a[3].VAR.WHAT, Scalar, "control: there is a container at the assigned position";
isa-ok @a[2].VAR.WHAT, Int, "control: there is no container at the bound position";

{
let @a;

#?rakudo todo "NYI"
nok @a[1]:exists, "let doesn't vivifies containers in holes";
@a[1] = 13;
ok @a[1]:exists, "assignment closes a hole on localized array";

#?rakudo todo "NYI"
isa-ok @a[2].VAR.WHAT, Int, "let doesn't containerize bound elements";
isa-ok @a[3].VAR.WHAT, Scalar, "let doesn't decontainerize assigned elements";

@a[2] = "12";
@a[3] := "42";

isa-ok @a[2].VAR.WHAT, Scalar, "control: containerized a bound element";
isa-ok @a[3].VAR.WHAT, Str, "control: decontainerized by binding";

Nil
}

nok @a[1]:exists, "hole is back after restoration";
isa-ok @a[2].VAR.WHAT, Int, "non-container element is correctly restored";
isa-ok @a[3].VAR.WHAT, Scalar, "containerized element is correctly restored";
}

subtest "let with Hash" => {
plan 4;
my sub let-hash(\h, $msg) is test-assertion {
subtest $msg => {
plan 8;
h<a> = 1;
h<c> := 3;
isa-ok h<a>.VAR.WHAT, Scalar, "control: assigned value is containerized";
isa-ok h<c>.VAR.WHAT, Int, "control: bound value is not containerized";

{
let h;

isa-ok h<a>.VAR.WHAT, Scalar, "let doesn't decontainerize assigned elements";
isa-ok h<c>.VAR.WHAT, Int, "let doesn't containerize bound elements";

h<a> := 42;
h<c>:delete;
h<c> = 13;

isa-ok h<a>.VAR.WHAT, Int, "control: decontainerized by binding";
isa-ok h<c>.VAR.WHAT, Scalar, "control: containerized by re-assignment";

Nil
}

isa-ok h<a>.VAR.WHAT, Scalar, "containerized element is correctly restored";
isa-ok h<c>.VAR.WHAT, Int, "non-container element is correctly restored";
}
}

let-hash my %h, "plain hash";
let-hash my Int %h, "typed hash";
let-hash my %h{Str}, "object hash";
let-hash my Int %h{Str}, "typed object hash";
}
}

# vim: expandtab shiftwidth=4
82 changes: 80 additions & 2 deletions S04-blocks-and-statements/temp.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 46;
plan 48;

# L<S04/The Relationship of Blocks and Declarations/function has been renamed>
{
Expand Down Expand Up @@ -232,7 +232,7 @@ throws-like { temp $*foo = 42 }, X::Dynamic::NotFound,
'Useful error conveyed when trying to temp a non-existing dynamic';

# https://github.com/Raku/old-issue-tracker/issues/5057
{
{
my %h{Pair}; %h{a => 1} = 2;
my %c{Pair}; %c{a => 1} = 2;
{
Expand All @@ -254,4 +254,82 @@ throws-like { temp $*foo = 42 }, X::Dynamic::NotFound,
is-deeply %h, %c, '`temp` keeps Nils around in Hashes when they exist';
}

# https://github.com/rakudo/rakudo/issues/1433
# Make sure that holes and containerization are preserved.
{
subtest "temp with Array" => {
plan 12;
my @a;
@a[3] = 42;
@a[2] := 12;

# Control tests
nok @a[1]:exists, "control: there is an expected hole";
isa-ok @a[3].VAR.WHAT, Scalar, "control: there is a container at the assigned position";
isa-ok @a[2].VAR.WHAT, Int, "control: there is no container at the bound position";

{
temp @a;

#?rakudo todo "NYI"
nok @a[1]:exists, "temp doesn't vivifies containers in holes";
@a[1] = 13;
ok @a[1]:exists, "assignment closes a hole on localized array";

#?rakudo todo "NYI"
isa-ok @a[2].VAR.WHAT, Int, "temp doesn't containerize bound elements";
isa-ok @a[3].VAR.WHAT, Scalar, "temp doesn't decontainerize assigned elements";

@a[2] = "12";
@a[3] := "42";

isa-ok @a[2].VAR.WHAT, Scalar, "control: containerized a bound element";
isa-ok @a[3].VAR.WHAT, Str, "control: decontainerized by binding";

Nil
}

nok @a[1]:exists, "hole is back after restoration";
isa-ok @a[2].VAR.WHAT, Int, "non-container element is correctly restored";
isa-ok @a[3].VAR.WHAT, Scalar, "containerized element is correctly restored";
}

subtest "temp with Hash" => {
plan 4;
my sub temp-hash(\h, $msg) is test-assertion {
subtest $msg => {
plan 8;
h<a> = 1;
h<c> := 3;
isa-ok h<a>.VAR.WHAT, Scalar, "control: assigned value is containerized";
isa-ok h<c>.VAR.WHAT, Int, "control: bound value is not containerized";

{
temp h;

isa-ok h<a>.VAR.WHAT, Scalar, "temp doesn't decontainerize assigned elements";
isa-ok h<c>.VAR.WHAT, Int, "temp doesn't containerize bound elements";

h<a> := 42;
h<c>:delete;
h<c> = 13;

isa-ok h<a>.VAR.WHAT, Int, "control: decontainerized by binding";
isa-ok h<c>.VAR.WHAT, Scalar, "control: containerized by re-assignment";

Nil
}

isa-ok h<a>.VAR.WHAT, Scalar, "containerized element is correctly restored";
isa-ok h<c>.VAR.WHAT, Int, "non-container element is correctly restored";
}
}

temp-hash my %h, "plain hash";
temp-hash my Int %h, "typed hash";
temp-hash my %h{Str}, "object hash";
temp-hash my Int %h{Str}, "typed object hash";
}
}

# vim: expandtab shiftwidth=4