Skip to content

Instantly share code, notes, and snippets.

@ekiru
Created August 30, 2010 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekiru/557661 to your computer and use it in GitHub Desktop.
Save ekiru/557661 to your computer and use it in GitHub Desktop.
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;
@ekiru
Copy link
Author

ekiru commented Aug 30, 2010

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment