Changeset 22751 for t

Show
Ignore:
Timestamp:
10/25/08 11:30:01 (3 months ago)
Author:
moritz
Message:

[t] some cleanup in instance.t

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • t/oo/attributes/instance.t

    r22000 r22751  
    1111=end pod 
    1212 
    13 eval 'has $.x;'; 
    14 ok $!, "'has' only works inside of class|role definitions"; 
     13eval_dies_ok 'has $.x;', "'has' only works inside of class|role definitions"; 
    1514 
    1615# L<S12/Attributes/the automatic generation of an accessor method of the same name> 
     
    3231# L<S12/Attributes/Pseudo-assignment to an attribute declaration specifies the default> 
    3332 
    34 eval 'class Foo2 { has $.bar = "baz"; }'; 
    35  
    36 { 
     33 
     34{ 
     35    class Foo2 { has $.bar = "baz"; }; 
    3736    my $foo = eval 'Foo2.new()'; 
    38     ok(eval('$foo ~~ Foo2'), '... our Foo2 instance was created'); 
    39     ok(eval('$foo.can("bar")'), '.. checking autogenerated accessor existence', :todo<feature>); 
    40     is(eval('$foo.bar()'), "baz", '.. autogenerated accessor works'); 
    41     is(eval('$foo.bar'), "baz", '.. autogenerated accessor works w/out parens'); 
     37    ok($foo ~~ Foo2, '... our Foo2 instance was created'); 
     38    ok($foo.can("bar"), '.. checking autogenerated accessor existence'); 
     39    is($foo.bar(), "baz", '.. autogenerated accessor works'); 
     40    is($foo.bar, "baz", '.. autogenerated accessor works w/out parens'); 
    4241    # what exactly will happen if we try to set bar() 
    4342} 
     
    4544# L<S12/Attributes/making it an lvalue method> 
    4645 
    47 class Foo3 { has $.bar is rw; }; 
    48  
    49 { 
     46 
     47#?pugs todo 'instance attributes' 
     48{ 
     49    class Foo3 { has $.bar is rw; }; 
    5050    my $foo = Foo3.new(); 
    5151    ok($foo ~~ Foo3, '... our Foo3 instance was created'); 
     
    5353    lives_ok { 
    5454        $val = $foo.can("bar"); 
    55     }, '.. checking autogenerated accessor existence', :todo<feature>; 
    56     ok($val, '... $foo.can("bar") should have returned true', :todo<feature>); 
     55    }, '.. checking autogenerated accessor existence'; 
     56    ok $val, '... $foo.can("bar") should have returned true'; 
    5757    is($foo.bar(), undef, '.. autogenerated accessor works'); 
    5858    lives_ok { 
     
    6262    lives_ok { 
    6363        $foo.bar("baz2"); 
    64     }, '.. autogenerated mutator works as method', :todo<feature>;     
    65     is($foo.bar, "baz2", '.. autogenerated mutator as method set the value correctly', :todo<feature>);         
     64    }, '.. autogenerated mutator works as method'; 
     65    is $foo.bar, "baz2", '.. autogenerated mutator as method set the value correctly'; 
    6666} 
    6767 
    6868# L<S12/Attributes/Private attributes use an exclamation to indicate that no public accessor is> 
    6969 
    70 class Foo4 { has $!bar; }; 
    71  
    72 { 
     70 
     71{ 
     72    class Foo4 { has $!bar; }; 
    7373    my $foo = Foo4.new(); 
    7474    ok($foo ~~ Foo4, '... our Foo4 instance was created'); 
     
    7777} 
    7878 
    79 class Foo4a { has $!bar = "baz"; }; 
    80  
    81 { 
     79 
     80{ 
     81    class Foo4a { has $!bar = "baz"; }; 
    8282    my $foo = eval 'Foo4a.new()'; 
    83     ok(eval('$foo ~~ Foo4a'), '... our Foo4a instance was created'); 
     83    ok($foo ~~ Foo4a, '... our Foo4a instance was created'); 
    8484    #?pugs eval 'todo' 
    8585    ok(!$foo.can("bar"), '.. checking autogenerated accessor existence'); 
     
    8989# L<S12/Attributes> 
    9090 
    91 class Foo5 { 
    92   has $.tail is rw; 
    93   has @.legs; 
    94   has $!brain; 
    95  
    96   method set_legs  (*@legs) { @.legs = @legs } 
    97   method inc_brain ()      { $!brain++ } 
    98   method get_brain ()      { $!brain } 
    99 }; 
    100  
    101 { 
     91 
     92{ 
     93    class Foo5 { 
     94        has $.tail is rw; 
     95        has @.legs; 
     96        has $!brain; 
     97 
     98        method set_legs  (*@legs) { @.legs = @legs } 
     99        method inc_brain ()      { $!brain++ } 
     100        method get_brain ()      { $!brain } 
     101    }; 
    102102    my $foo = Foo5.new(); 
    103103    ok($foo ~~ Foo5, '... our Foo5 instance was created'); 
     
    130130# L<S12/Construction and Initialization/If you name an attribute as a parameter, that attribute is initialized directly, so> 
    131131 
    132 class Foo6 { 
    133   has $.bar is rw; 
    134   has $.baz; 
    135   has $!hidden; 
    136  
    137   submethod BUILD($.bar, $.baz, $!hidden) {} 
    138   method get_hidden() { $!hidden } 
    139 } 
    140  
    141 { 
     132 
     133{ 
     134    class Foo6 { 
     135        has $.bar is rw; 
     136        has $.baz; 
     137        has $!hidden; 
     138 
     139        submethod BUILD($.bar, $.baz, $!hidden) {} 
     140        method get_hidden() { $!hidden } 
     141    } 
     142 
    142143    my $foo = Foo6.new(bar => 1, baz => 2, hidden => 3); 
    143144    ok($foo ~~ Foo6, '... our Foo6 instance was created'); 
     
    149150 
    150151# check that doing something in submethod BUILD works 
    151 class Foo6a { 
    152   has $.bar is rw; 
    153   has $.baz; 
    154   has $!hidden; 
    155  
    156   submethod BUILD ($!hidden, $.bar = 10, $.baz?) { 
    157     $.baz = 5; 
    158   } 
    159   method get_hidden() { $!hidden } 
    160 } 
    161  
    162 { 
     152 
     153{ 
     154    class Foo6a { 
     155        has $.bar is rw; 
     156        has $.baz; 
     157        has $!hidden; 
     158 
     159        submethod BUILD ($!hidden, $.bar = 10, $.baz?) { 
     160            $.baz = 5; 
     161        } 
     162        method get_hidden() { $!hidden } 
     163    } 
     164 
    163165    my $foo = Foo6a.new(bar => 1, hidden => 3); 
    164166    ok($foo ~~ Foo6a, '... our Foo6a instance was created'); 
     
    170172 
    171173# check that assignment in submethod BUILD works with a bare return, too 
    172 class Foo6b { 
    173   has $.bar is rw; 
    174   has $.baz; 
    175  
    176   submethod BUILD ($.bar = 10, $.baz?) { 
    177     $.baz = 9; 
    178     return; 
    179   } 
    180 } 
    181  
    182 { 
     174{ 
     175    class Foo6b { 
     176        has $.bar is rw; 
     177        has $.baz; 
     178 
     179        submethod BUILD ($.bar = 10, $.baz?) { 
     180            $.baz = 9; 
     181            return; 
     182        } 
     183    } 
     184 
    183185    my $foo = Foo6b.new(bar => 7); 
    184186    ok($foo ~~ Foo6b, '... our Foo6b instance was created');