use v6;
use Test;

plan 16;

# L<S12/Methods/may be declared as lvalues with is rw.>

class T {
    has $.a;
    has $.b;
    method l1 is rw { 
        return $.a;
    }

    method l2 is rw { 
        $.b;
    }
}

my $o = T.new(:a<x>, :b<y>);

is $o.l1,       'x',    'lvalue method as rvalue with explicit return';
is $o.l2,       'y',    'lvalue method as rvalue with implicit return';

lives_ok { $o.l1 = 3 }, 'can assign to the lvalue method (explicit return)';
is $o.l1,       3,      '... and the assignment worked';
is $o.a,        3,      '...also to the attribute';

lives_ok { $o.l2 = 4 }, 'can assign to the lvalue method (implicit return)';
is $o.l2,       4,      '... and the assignment worked';
is $o.b,        4,      '...also to the attribute';

my ($a, $b);
lives_ok { temp $o.l1 = 8; $a = $o.a },
         'can use lvalue method in temp() statement (explicit return)';
is $o.l1,       3,      '... and the value was reset';
is $o.a,        3,      '... also on the attribute';
is $a,          8,      'but the temp assignment had worked';

lives_ok { temp $o.l2 = 9; $b = $o.b },
         'can use lvalue method in temp() statement (explicit return)';
is $o.l2,       4,      '... and the value was reset';
is $o.b,        3,      '... also on the attribute';
is $a,          9,      'but the temp assignment had worked';

# vim: ft=perl6