lundi 12 mars 2018

How can I test if a method is defined in a Perl 6 class?

I often want to test that I've defined a method in a particular class. This has caught many problems where I've renamed a method or otherwise rearranged things in the architecture.

I know I can use .^lookup but that still feels weird to me like I'm eventually going to run into a case where it returns things in a different order than I expect (ignore signatures for now). This is what I came up with:

use Test;

class Foo is Str {}
class Bar is Str { method Str { 'Hello' } }

can-ok Str, 'Str';
can-ok Foo, 'Str';
can-ok Bar, 'Str';

is Foo.^lookup( 'Str' ).package.^name, 'Foo', 'Foo defines Str';
is Bar.^lookup( 'Str' ).package.^name, 'Bar', 'Bar defines Str';

done-testing;

It does what I want in this simple case and I haven't made it fail so far:

ok 1 - The type 'Str' can do the method 'Str'
ok 2 - The type 'Foo' can do the method 'Str'
ok 3 - The type 'Bar' can do the method 'Str'
not ok 4 -
ok 5 -
1..5
# Failed test at /Users/brian/Desktop/hello.p6 line 12
# expected: 'Foo'
#      got: 'Mu'
# Looks like you failed 1 test of 5

Aucun commentaire:

Enregistrer un commentaire