Skip to content

Commit

Permalink
Added tests for late applied also is rw
Browse files Browse the repository at this point in the history
No matter when `is rw` is applied, attributes without explicit `is
readonly` trait have to repsect it:

```
class C {
    has $.a;
    also is rw;
}
C.new(a=>12).a++; # 13
```

Grouped some older tests into semantically linked subtests.
  • Loading branch information
vrurg committed Feb 19, 2020
1 parent b12064d commit f63ef08
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
46 changes: 33 additions & 13 deletions S12-class/rw.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ use v6;

use Test;

plan 5;
plan 3;

# L<S12/Attributes/If you declare the class as>

class Foo {
has $.readonly_attr;
}

{
subtest "Basic sanity" => {
plan 1;
my class Foo {
has $.readonly_attr;
}
my Foo $foo .= new;
dies-ok { $foo.readonly_attr++ }, "basic sanity";
dies-ok { $foo.readonly_attr++ }, "the default is readonly";
}

subtest "'is rw' on a class" => {
plan 4;

class Bar is rw {
has $.readwrite_attr;
has $.but_not_this is readonly;
}
my class Bar is rw {
has $.readwrite_attr;
has $.but_not_this is readonly;
}

{
my Bar $bar .= new(but_not_this => 42);

lives-ok { $bar.readwrite_attr++ },
"'is rw' on the class declaration applies to all attributes (1)";
is $bar.readwrite_attr, 1,
Expand All @@ -35,4 +36,23 @@ class Bar is rw {
"'is readonly' on a specific attribute can overrule the is rw on the class (2)";
}

subtest "With 'also is rw'" => {
plan 6;

my class C {
has $.a;
has $.a-ro is readonly;
also is rw;
has $.b;
}

my $obj = C.new( a => 12, a-ro => 42, b => 1 );
lives-ok { $obj.a++ }, "'also is rw' is being respected as class default";
is $obj.a, 13, "attribute value has been changed";
lives-ok { $obj.b-- }, "post-'also is rw' attribute is writable";
is $obj.b, 0, "post-'also is rw' attribute value changed";
dies-ok { $obj.a-ro++ }, "'is readonly' is not altered";
is $obj.a-ro, 42, "readonly attribute value unchanged";
}

# vim: ft=perl6
27 changes: 20 additions & 7 deletions S14-roles/rw.t
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use v6;
use Test;

plan 5;
plan 6;

role R1 is rw {
has $.r1;
has $.r1-ro is readonly;
}

role R2 {
has $.r2;
}

sub test-R1(Mu \type) {
subtest "rw/ro" => {
sub test-R1(Mu \type, Str $msg?) {
my $title = $msg // "rw/ro";
subtest $title => {
plan 4;
my $obj = type.new(r1 => 41, r1-ro => 12);

Expand All @@ -26,6 +23,11 @@ sub test-R1(Mu \type) {

subtest "Default is readonly" => {
plan 4;

my role R2 {
has $.r2;
}

dies-ok { R2.new.r2 = 0 }, "pun of a role preserves default readonly";
my role R3 does R2 { }
dies-ok { R3.new.r2 = 0 }, "pun of a role doing role preserves default readonly";
Expand Down Expand Up @@ -58,3 +60,14 @@ subtest "Class consuming role doing a role" => {
my class C2 does R3 { }
test-R1 C2;
}

subtest "use 'also is rw'" => {
my role R3 {
has $.r1;
also is rw;
has $.r1-ro is readonly;
}
test-R1 R3, "role puning";
my role R4 does R3 { }
test-R1 R4, "role doing role puning";
}

0 comments on commit f63ef08

Please sign in to comment.