Changeset 2912

Show
Ignore:
Timestamp:
05/10/05 03:01:51 (4 years ago)
Author:
Stevan
svk:copy_cache_prev:
4437
Message:

Perl::MetaClass? - more visibility tweaks; subclasses cannot be the superclass of the invocant anymore in MetaClass?; more tests, and comments as well

Location:
ext/Perl-MetaClass
Files:
8 modified

Legend:

Unmodified
Added
Removed
  • ext/Perl-MetaClass/lib/Hack/Instances.pm

    r2903 r2912  
    2525 
    2626sub instance_isa(Str $inst: Str $class) is export { 
    27     (%INSTANCES.exists($inst)) 
    28         || die "The instance '$inst' is not a valid instance (key not found)"; 
     27    return 0 unless %INSTANCES.exists($inst); 
    2928    my (undef, $inv_class, undef) = split(';', $inst); 
    3029    return ($inv_class eq $class); 
  • ext/Perl-MetaClass/lib/Perl/MetaAssoc.pm

    r2911 r2912  
    8989  # property end. 
    9090 
    91   my $Property_mc = Perl::MetaClass->new("Property"); 
    92   my $Class_mc    = Perl::MetaClass->new("Class"); 
     91  my $Property_mc = Perl::MetaClass::new("Property"); 
     92  my $Class_mc    = Perl::MetaClass::new("Class"); 
    9393 
    94   $Class_mc->clsAssocs 
    95        (properties => Perl::MetaAssoc->new 
     94  $Class_mc.clsAssocs 
     95       (properties => Perl::MetaAssoc::new 
    9696           ( 
    9797             assocOrdered => false, 
    98              assocRange => [0, inf], 
     98             assocRange => [0, Inf], 
    9999             assocCompanion => "class", 
    100              assocIsComposite => true, 
    101              assocPair => Perl::MetaAssoc->new 
     100             assocIsComposite => 1, 
     101             assocPair => Perl::MetaAssoc::new 
    102102                             ( assocRange => [1, 1], 
    103103                               assocClass => $Property_mc ) 
  • ext/Perl-MetaClass/lib/Perl/MetaClass.pm

    r2911 r2912  
    1313        'name'       => $name, 
    1414        'super'      => undef, 
    15         'subclasses' => [], 
     15        'subclasses' => hash(), 
    1616        'properties' => hash(), 
    1717        'methods'    => hash(), 
     
    5454    my %self := get_instance($inv, "Perl::MetaClass"); 
    5555    if @subclasses { 
     56        my %inv_subclasses = %self<subclasses>; 
    5657        # NOTE: 
    5758        # enforce the following rules on all @subclasses: 
    58         # - they are instances of Perl::MetaClass 
    59         # - they're superclass is our invocant 
     59        # - the subclass is an instance of Perl::MetaClass 
     60        # - if the subclass has a superclass, it's superclass is our invocant 
     61        # - if the invocant has a superclass, the subclass is not the superclass of our invocant 
    6062        for @subclasses -> $subclass { 
    6163            ($subclass.instance_isa('Perl::MetaClass')) 
    6264                || die "Sub class must be a Perl::MetaClass instance (got: '$subclass')";  
    6365            ($subclass.clsSuper() && $subclass.clsSuper().clsName() eq $inv.clsName()) 
    64                 || die "Sub class's superclass must be the invocant (got: '{ $subclass.clsSuper() }')";              
     66                || die "Sub class's superclass must be the invocant (got: '{ $subclass.clsSuper() }')";     
     67            ($subclass.clsName() ne $inv.clsSuper().clsName()) 
     68                || die "Subclass cannot be the superclass of the invocant" 
     69                    if $inv.clsSuper(); 
     70            %inv_subclasses{$subclass} = undef; 
    6571        }  
    66         # NOTE:  
    67         # this is kind of ugly, but I get a  
    68         # "can't modify constant" error otherwise 
    69         my @inv_subclasses = %self<subclasses>; 
    70         %self<subclasses> = [ @inv_subclasses, @subclasses ];     
     72        %self<subclasses> = \%inv_subclasses;     
    7173    } 
    72     return %self<subclasses>; 
     74    return keys(%self<subclasses>); 
    7375} 
    7476 
  • ext/Perl-MetaClass/lib/Perl/MetaMethod.pm

    r2904 r2912  
    55use Hack::Instances; 
    66 
    7 sub Perl::MetaMethod::new(Code $sub) returns Str is export { 
     7sub Perl::MetaMethod::new(Code $sub, Str +$visibility) returns Str is export { 
    88    my $id = make_instance("Perl::MetaMethod", {  
    9         'sub'    => $sub, 
    10         'params' => [], 
     9        'sub'        => $sub, 
     10        'params'     => [], 
     11        'visibility' => 'public', 
    1112    }); 
     13    $id.methodVisibility($visibility) if $visibility.defined; 
    1214    return $id; 
    1315} 
     
    2729    my %self := get_instance($inv, "Perl::MetaMethod"); 
    2830    return %self<sub>(@args); 
     31} 
     32 
     33sub methodVisibility(Str $inv: Str ?$visibility) returns Str { 
     34    my %self := get_instance($inv, "Perl::MetaMethod"); 
     35    if $visibility.defined { 
     36        ($visibility ~~ rx:perl5:i/(private|public)/) 
     37            || die "Visibility must be either 'private' or 'public' (got: '$visibility')"; 
     38        %self<visibility> = lc($visibility); 
     39    } 
     40    return %self<visibility>; 
    2941} 
    3042 
  • ext/Perl-MetaClass/lib/Perl/MetaProperty.pm

    r2910 r2912  
    99        'type'       => $type, 
    1010        'default'    => undef, 
    11         'visibility' => undef, 
     11        'visibility' => 'public', 
    1212    }); 
    1313    $id.propDefault($default)       if $default.defined; 
     
    5454    if $visibility.defined { 
    5555        ($visibility ~~ rx:perl5:i/(private|public)/) 
    56             || die "Visibility must be either 'Private' of 'Public' (got: '$visibility')"; 
    57         %self<visibility> = $visibility; 
     56            || die "Visibility must be either 'private' or 'public' (got: '$visibility')"; 
     57        %self<visibility> = lc($visibility); 
    5858    } 
    5959    return %self<visibility>; 
  • ext/Perl-MetaClass/t/10_MetaClass.t

    r2909 r2912  
    2323# -------------------------------------------------------------- 
    2424 
    25 my $class = Perl::MetaClass::new('Role'); 
     25my $role = Perl::MetaClass::new('Role'); 
    2626 
    27 is($class.clsName(), 'Role', '... we got the right class name'); 
     27is($role.clsName(), 'Role', '... we got the right class name'); 
    2828 
    2929# Super Class 
    3030 
    31 my $superclass = Perl::MetaClass::new('Package'); 
     31my $package = Perl::MetaClass::new('Package'); 
    3232 
    33 is($class.clsSuper(), undef, '... we do not have a superclass'); 
    34 $class.clsSuper($superclass); 
    35 is($class.clsSuper().clsName(), 'Package', '... we now have a superclass'); 
     33is($role.clsSuper(), undef, '... we do not have a superclass'); 
     34$role.clsSuper($package); 
     35is($role.clsSuper().clsName(), 'Package', '... we now have a superclass'); 
    3636 
    3737# Sub Classes 
    3838 
    3939{ 
    40     my @subclasses = $class.clsSubClasses(); 
     40    my @subclasses = $role.clsSubClasses(); 
    4141    is(+@subclasses, 0, '... no subclasses yet'); 
    4242} 
    4343 
    44 my $subclass1 = Perl::MetaClass::new('Class'); 
    45 $subclass1.clsSuper($class); 
     44my $class = Perl::MetaClass::new('Class'); 
     45$class.clsSuper($role); 
    4646 
    4747{ 
    48     my @subclasses = $class.clsSubClasses(); 
     48    my @subclasses = $role.clsSubClasses(); 
    4949    is(+@subclasses, 1, '... we have 1 subclasses now'); 
    50     is(@subclasses[0].clsName(), 'Foo::Bar', '... this is our first subclass'); 
     50    is(@subclasses[0].clsName(), 'Class', '... this is our first subclass'); 
    5151} 
     52 
     53dies_ok { 
     54    $class.clsSubClasses($role); 
     55}, '... subclass cannot be the superclass of the invocant'; 
     56like($!, rx:perl5/^Sub class\'s superclass must be the invocant/, '... got the right error'); 
    5257 
    5358# Properties 
    5459 
    5560{ 
    56     my %props = $class.clsProperties(); 
     61    my %props = $role.clsProperties(); 
    5762    is(+keys(%props), 0, '... we have no properties yet'); 
    5863} 
     
    6267 
    6368{ 
    64     my %props = $class.clsProperties('.prop1', $prop1, '.prop2', $prop2); 
     69    my %props = $role.clsProperties('.prop1', $prop1, '.prop2', $prop2); 
    6570    my @keys = keys(%props); 
    6671    is(+@keys, 2, '... we have 2 properties now'); 
  • ext/Perl-MetaClass/t/11_MetaProperty.t

    r2903 r2912  
    1111is($prop.propType(), 'Str', '... our property type is "Str"'); 
    1212is($prop.propDefault(), undef, '... our property default is not defined'); 
     13is($prop.propVisibility(), 'public', '... our property by default is public'); 
    1314 
    1415lives_ok { 
     
    2021 
    2122is($prop.propDefault(), undef, '... our property default is now undefined since we changed types'); 
     23is($prop.propVisibility(), 'public', '... our property is still public'); 
    2224 
    2325dies_ok { 
    2426    $prop.propDefault('Testing default'); 
    2527}, '...  property default successfully'; 
     28like($!, rx:perl5/^Incorrect Type value for property default/, '... got the right error'); 
    2629 
    2730lives_ok { 
     
    3841}, '... we set the property default successfully'; 
    3942 
     43dies_ok { 
     44    $prop.propVisibility('invisible'); 
     45}, '...  property must be either public or private'; 
     46like($!, rx:perl5/^Visibility must be either \'private\' or \'public\'/, '... got the right error'); 
     47 
    4048my $prop2; 
    4149lives_ok { 
    42     $prop2 = Perl::MetaProperty::new('Str', :default("Hello World")); 
    43 }, '... set our default in the constructor successfully' 
     50    $prop2 = Perl::MetaProperty::new('Str', :default("Hello World"), :visibility<private>); 
     51}, '... set our default in the constructor successfully'; 
    4452 
    4553is($prop2.propType(), 'Str', '... our property type is "Str"'); 
    4654is($prop2.propDefault(), "Hello World", '... our property default is defined'); 
     55is($prop2.propVisibility(), 'private', '... our property is private'); 
     56 
     57 
  • ext/Perl-MetaClass/t/12_MetaMethod.t

    r2904 r2912  
    88my $method = Perl::MetaMethod::new(sub { return "Hello Meta-World" }); 
    99is($method.methodInvoke(), 'Hello Meta-World', '... got the expected value from our method'); 
     10is($method.methodVisibility(), 'public', '... by default it is public'); 
    1011 
    1112{ 
     
    1920    is(~@params, '1 2 3', '... get have the right params'); 
    2021} 
     22 
     23my $method2 = Perl::MetaMethod::new(sub { return "Hello (Private) Meta-World" }, :visibility<private>); 
     24is($method2.methodVisibility(), 'private', '... this method is private'); 
     25 
     26dies_ok { 
     27    $method2.methodVisibility('invisible'); 
     28}, '...  method must be either public or private'; 
     29like($!, rx:perl5/^Visibility must be either \'private\' or \'public\'/, '... got the right error');