Changeset 3373

Show
Ignore:
Timestamp:
05/18/05 03:51:03 (4 years ago)
Author:
Stevan
svk:copy_cache_prev:
4945
Message:

Perl::MetaModel? - findMethod and invokeMethod methods coded and tested (and an isMethodSupported too); Smalltalk Meta-Model++

Location:
ext/Perl-MetaModel
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • ext/Perl-MetaModel/lib/Perl/Meta/Class.pm

    r3361 r3373  
    158158} 
    159159 
    160 # can we remove these? 
    161160method methods      ($self:) returns Hash  { %:methods        } 
    162161method methodLabels ($self:) returns Array { %:methods.keys() } 
    163162 
    164163method 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 
     169method isMethodSupported ($self: $label) returns Bool { 
     170    $self.findMethod($label) ?? 1 :: 0; 
     171} 
     172 
     173method invokeMethod ($self: Str $label, *@args) returns Any {   
    171174    my $method = $self.findMethod($label); 
    172175    ($method.defined) 
    173176        || die "Method not found"; 
    174177    my $impl = $method.code(); 
     178    ($impl.defined) 
     179        || die "Method has no code";     
    175180    return $impl($self, @args); 
    176181} 
  • ext/Perl-MetaModel/t/12_Perl_Meta_MetaClass_methods.t

    r3356 r3373  
    44use Test; 
    55 
    6 plan 10; 
     6plan 25; 
    77 
    88use Perl::Meta::Class; 
     
    2323 
    2424my $method1 = Perl::Meta::Method.new(); 
    25 my $method2 = Perl::Meta::Method.new(); 
     25my $method2 = Perl::Meta::Method.new(code => sub { 'Hello World' }); 
    2626 
    2727$mmc.addMethod('method1', $method1); 
    2828$mmc.addMethod('method2', $method2); 
     29 
     30ok($mmc.findMethod('method1') =:= $method1, '... found the right method'); 
     31ok($mmc.findMethod('method2') =:= $method2, '... found the right method'); 
     32 
     33ok(!$mmc.isMethodSupported('method3'), '... did not find the method (as expected)'); 
    2934 
    3035{ 
     
    5055    ok(%methods{'method1'} =:= $method1, '... the first is $method1');      
    5156} 
     57 
     58my $sub_mmc = Perl::Meta::Class::new('SubClass'); 
     59$sub_mmc.superclass($mmc); 
     60 
     61$sub_mmc.addMethod('method2', $method2); 
     62 
     63ok($sub_mmc.isMethodSupported('method1'), '... did find the method in parent class'); 
     64ok($sub_mmc.findMethod('method1') =:= $method1, '... found the right method (in parent class)'); 
     65 
     66ok(!$mmc.isMethodSupported('method2'), '... did not find the method (as expected) in parent class'); 
     67ok($sub_mmc.isMethodSupported('method2'), '... did find the method (as expected) in class'); 
     68 
     69my $sub_sub_mmc = Perl::Meta::Class::new('SubSubClass'); 
     70$sub_sub_mmc.superclass($sub_mmc); 
     71 
     72ok($sub_sub_mmc.isMethodSupported('method1'), '... did find the method in parents parent class'); 
     73ok($sub_sub_mmc.findMethod('method1') =:= $method1, '... found the right method (in parents parent class)'); 
     74ok($sub_sub_mmc.findMethod('method2'), '... did find the method (as expected) in parents class'); 
     75 
     76# check some errors 
     77 
     78$!= undef;  
     79dies_ok { 
     80    $mmc.invokeMethod('method2'); 
     81}, '... this dies as expected'; 
     82like($!, rx:perl5/^Method not found/, '... got the right error'); 
     83 
     84$!= undef;  
     85dies_ok { 
     86    $sub_sub_mmc.invokeMethod('method1'); 
     87}, '... this dies as expected'; 
     88like($!, rx:perl5/^Method has no code/, '... got the right error'); 
     89 
     90is($sub_mmc.invokeMethod('method2'), 'Hello World', '... the method returned what we expected');