This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v6; | |
| sub with-attributes ($obj, *&callback) { | |
| my %slot-values; | |
| for &callback.signature.params -> $param { | |
| my $slot = $param.name.substr(1); | |
| %slot-values{$slot} = $obj."$slot"(); | |
| } | |
| callback |%slot-values; | |
| } | |
| use Test; | |
| class Thing { | |
| has $.x is rw; | |
| has $.y is rw; | |
| } | |
| my Thing $thing .= new(x => 0, y => 1); | |
| is $thing.x, 0, 'Correct initial $thing.x.'; | |
| is $thing.y, 1, 'Correct initial $thing.y.'; | |
| with-attributes $thing, -> $x, $y { | |
| is $x, 0, 'with-attributes works for reading $thing.x.'; | |
| is $y, 1, 'with-attributes works for reading $thing.y.'; | |
| } | |
| with-attributes $thing, -> $x is rw, $y is rw { | |
| $x++; | |
| $y++; | |
| is $x, 1, 'with-attributes allows modifying local $x value with is rw.'; | |
| is $y, 2, 'with-attributes allows modifying local $y value with is rw.'; | |
| } | |
| is $thing.x, 1, 'Changes to $thing.x in with-attributes persist outside of the block.'; | |
| is $thing.y, 2, 'Changes to $thing.y in with-attributes persist outside of the block.'; | |
| done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current output is:
ok 1 - Correct initial $thing.x.
ok 2 - Correct initial $thing.y.
ok 3 - with-attributes works for reading $thing.x.
ok 4 - with-attributes works for reading $thing.y.
ok 5 - with-attributes allows modifying local $x value with is rw.
ok 6 - with-attributes allows modifying local $y value with is rw.
not ok 7 - Changes to $thing.x in with-attributes persist outside of the block.
got: '0'
expected: '1'
not ok 8 - Changes to $thing.y in with-attributes persist outside of the block.
got: '1'
expected: '2'
1..8
Looks like you failed 2 tests of 8