Changeset 3373
- Timestamp:
- 05/18/05 03:51:03 (4 years ago)
- svk:copy_cache_prev:
- 4945
- Location:
- ext/Perl-MetaModel
- Files:
-
- 2 modified
-
lib/Perl/Meta/Class.pm (modified) (1 diff)
-
t/12_Perl_Meta_MetaClass_methods.t (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ext/Perl-MetaModel/lib/Perl/Meta/Class.pm
r3361 r3373 158 158 } 159 159 160 # can we remove these?161 160 method methods ($self:) returns Hash { %:methods } 162 161 method methodLabels ($self:) returns Array { %:methods.keys() } 163 162 164 163 method findMethod ($self: $label) returns Perl::Meta::Method { 165 166 } 167 168 our &Perl::Meta::Class::isMethodSupported ::= &Perl::Meta::Class::findMethod; 169 170 method invokeMethod ($self: Str $label, @args) returns Any { 164 return %:methods{$label} if %:methods.exists($label); 165 return $:parent.findMethod($label) if $:parent.defined; 166 return undef; 167 } 168 169 method isMethodSupported ($self: $label) returns Bool { 170 $self.findMethod($label) ?? 1 :: 0; 171 } 172 173 method invokeMethod ($self: Str $label, *@args) returns Any { 171 174 my $method = $self.findMethod($label); 172 175 ($method.defined) 173 176 || die "Method not found"; 174 177 my $impl = $method.code(); 178 ($impl.defined) 179 || die "Method has no code"; 175 180 return $impl($self, @args); 176 181 } -
ext/Perl-MetaModel/t/12_Perl_Meta_MetaClass_methods.t
r3356 r3373 4 4 use Test; 5 5 6 plan 10;6 plan 25; 7 7 8 8 use Perl::Meta::Class; … … 23 23 24 24 my $method1 = Perl::Meta::Method.new(); 25 my $method2 = Perl::Meta::Method.new( );25 my $method2 = Perl::Meta::Method.new(code => sub { 'Hello World' }); 26 26 27 27 $mmc.addMethod('method1', $method1); 28 28 $mmc.addMethod('method2', $method2); 29 30 ok($mmc.findMethod('method1') =:= $method1, '... found the right method'); 31 ok($mmc.findMethod('method2') =:= $method2, '... found the right method'); 32 33 ok(!$mmc.isMethodSupported('method3'), '... did not find the method (as expected)'); 29 34 30 35 { … … 50 55 ok(%methods{'method1'} =:= $method1, '... the first is $method1'); 51 56 } 57 58 my $sub_mmc = Perl::Meta::Class::new('SubClass'); 59 $sub_mmc.superclass($mmc); 60 61 $sub_mmc.addMethod('method2', $method2); 62 63 ok($sub_mmc.isMethodSupported('method1'), '... did find the method in parent class'); 64 ok($sub_mmc.findMethod('method1') =:= $method1, '... found the right method (in parent class)'); 65 66 ok(!$mmc.isMethodSupported('method2'), '... did not find the method (as expected) in parent class'); 67 ok($sub_mmc.isMethodSupported('method2'), '... did find the method (as expected) in class'); 68 69 my $sub_sub_mmc = Perl::Meta::Class::new('SubSubClass'); 70 $sub_sub_mmc.superclass($sub_mmc); 71 72 ok($sub_sub_mmc.isMethodSupported('method1'), '... did find the method in parents parent class'); 73 ok($sub_sub_mmc.findMethod('method1') =:= $method1, '... found the right method (in parents parent class)'); 74 ok($sub_sub_mmc.findMethod('method2'), '... did find the method (as expected) in parents class'); 75 76 # check some errors 77 78 $!= undef; 79 dies_ok { 80 $mmc.invokeMethod('method2'); 81 }, '... this dies as expected'; 82 like($!, rx:perl5/^Method not found/, '... got the right error'); 83 84 $!= undef; 85 dies_ok { 86 $sub_sub_mmc.invokeMethod('method1'); 87 }, '... this dies as expected'; 88 like($!, rx:perl5/^Method has no code/, '... got the right error'); 89 90 is($sub_mmc.invokeMethod('method2'), 'Hello World', '... the method returned what we expected');
