Skip to content

Instantly share code, notes, and snippets.

@ajs
Created November 7, 2016 17:27
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 ajs/e3a9fb6caf76f23de0940a676ef7dd2b to your computer and use it in GitHub Desktop.
Save ajs/e3a9fb6caf76f23de0940a676ef7dd2b to your computer and use it in GitHub Desktop.
Example of the default setting bug in rakudo perl6
#!/usr/bin/env perl6
#
# Demonstrate a bug in defualt handling. One failure:
#
# ok 1 - My something isa Something
# ok 2 - My something does Pingable
# ok 3 - Something starts at zero
# ok 4 - Ping the something
# ok 5 - Something incremented to 1
# not ok 6 - Starts at zero
#
# # Failed test 'Starts at zero'
# # at /home/ajs/self/perl/default-bug.p6 line 46
# # expected: '0'
# # got: (Any)
# ok 7 - Ping the hash
# ok 8 - Incremented to 1
use v6;
use Test;
# Imagine that this class is in a library that my code uses...
role Pingable {
has $.ping-count = 0;
method ping() { $!ping-count++ }
}
sub pingable(::type) { ::type but Pingable }
# First, in my code I use this on my own type:
class Something {}
my $ping-something = (pingable Something).new;
# And verifiy expectations
isa-ok $ping-something, Something, "My something isa Something";
does-ok $ping-something, Pingable, "My something does Pingable";
is $ping-something.ping-count, 0, "Something starts at zero";
lives-ok {$ping-something.ping}, "Ping the something";
is $ping-something.ping-count, 1, "Something incremented to 1";
# Now in my code, I innocently:
my $ping-hash = (pingable Hash).new;
# now let's verify expectations:
is $ping-hash.ping-count, 0, "Starts at zero";
lives-ok {$ping-hash.ping}, "Ping the hash";
is $ping-hash.ping-count, 1, "Incremented to 1";
# vim: sw=4 sts=4 et ai ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment