- Timestamp:
- 10/25/08 11:30:01 (3 months ago)
- Files:
-
- 1 modified
-
t/oo/attributes/instance.t (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
t/oo/attributes/instance.t
r22000 r22751 11 11 =end pod 12 12 13 eval 'has $.x;'; 14 ok $!, "'has' only works inside of class|role definitions"; 13 eval_dies_ok 'has $.x;', "'has' only works inside of class|role definitions"; 15 14 16 15 # L<S12/Attributes/the automatic generation of an accessor method of the same name> … … 32 31 # L<S12/Attributes/Pseudo-assignment to an attribute declaration specifies the default> 33 32 34 eval 'class Foo2 { has $.bar = "baz"; }'; 35 36 { 33 34 { 35 class Foo2 { has $.bar = "baz"; }; 37 36 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'); 42 41 # what exactly will happen if we try to set bar() 43 42 } … … 45 44 # L<S12/Attributes/making it an lvalue method> 46 45 47 class Foo3 { has $.bar is rw; }; 48 49 { 46 47 #?pugs todo 'instance attributes' 48 { 49 class Foo3 { has $.bar is rw; }; 50 50 my $foo = Foo3.new(); 51 51 ok($foo ~~ Foo3, '... our Foo3 instance was created'); … … 53 53 lives_ok { 54 54 $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'; 57 57 is($foo.bar(), undef, '.. autogenerated accessor works'); 58 58 lives_ok { … … 62 62 lives_ok { 63 63 $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'; 66 66 } 67 67 68 68 # L<S12/Attributes/Private attributes use an exclamation to indicate that no public accessor is> 69 69 70 class Foo4 { has $!bar; }; 71 72 { 70 71 { 72 class Foo4 { has $!bar; }; 73 73 my $foo = Foo4.new(); 74 74 ok($foo ~~ Foo4, '... our Foo4 instance was created'); … … 77 77 } 78 78 79 class Foo4a { has $!bar = "baz"; }; 80 81 { 79 80 { 81 class Foo4a { has $!bar = "baz"; }; 82 82 my $foo = eval 'Foo4a.new()'; 83 ok( eval('$foo ~~ Foo4a'), '... our Foo4a instance was created');83 ok($foo ~~ Foo4a, '... our Foo4a instance was created'); 84 84 #?pugs eval 'todo' 85 85 ok(!$foo.can("bar"), '.. checking autogenerated accessor existence'); … … 89 89 # L<S12/Attributes> 90 90 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 }; 102 102 my $foo = Foo5.new(); 103 103 ok($foo ~~ Foo5, '... our Foo5 instance was created'); … … 130 130 # L<S12/Construction and Initialization/If you name an attribute as a parameter, that attribute is initialized directly, so> 131 131 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 142 143 my $foo = Foo6.new(bar => 1, baz => 2, hidden => 3); 143 144 ok($foo ~~ Foo6, '... our Foo6 instance was created'); … … 149 150 150 151 # 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 163 165 my $foo = Foo6a.new(bar => 1, hidden => 3); 164 166 ok($foo ~~ Foo6a, '... our Foo6a instance was created'); … … 170 172 171 173 # 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 183 185 my $foo = Foo6b.new(bar => 7); 184 186 ok($foo ~~ Foo6b, '... our Foo6b instance was created');
