Permalink
Browse files

[t/spec] more cleanup of instance.t

git-svn-id: http://svn.pugscode.org/pugs@24193 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information...
1 parent eb858da commit b8116e0183e5c5bcdb1884c2ef465d07e21a1b6a moritz committed Dec 7, 2008
Showing with 79 additions and 65 deletions.
  1. +79 −65 S12-attributes/instance.t
View
@@ -33,7 +33,7 @@ class Foo1 { has $.bar; };
{
class Foo2 { has $.bar = "baz"; };
- my $foo = eval 'Foo2.new()';
+ my $foo = Foo2.new();
ok($foo ~~ Foo2, '... our Foo2 instance was created');
ok($foo.can("bar"), '.. checking autogenerated accessor existence');
is($foo.bar(), "baz", '.. autogenerated accessor works');
@@ -79,7 +79,7 @@ class Foo1 { has $.bar; };
{
class Foo4a { has $!bar = "baz"; };
- my $foo = eval 'Foo4a.new()';
+ my $foo = Foo4a.new();
ok($foo ~~ Foo4a, '... our Foo4a instance was created');
#?pugs eval 'todo'
ok(!$foo.can("bar"), '.. checking autogenerated accessor existence');
@@ -130,6 +130,7 @@ class Foo1 { has $.bar; };
# L<S12/Construction and Initialization/If you name an attribute as a parameter, that attribute is initialized directly, so>
+#?rakudo skip 'parse fail'
{
class Foo6 {
has $.bar is rw;
@@ -150,6 +151,7 @@ class Foo1 { has $.bar; };
# check that doing something in submethod BUILD works
+#?rakudo skip 'parse fail'
{
class Foo6a {
has $.bar is rw;
@@ -171,6 +173,7 @@ class Foo1 { has $.bar; };
}
# check that assignment in submethod BUILD works with a bare return, too
+#?rakudo skip 'parse fail'
{
class Foo6b {
has $.bar is rw;
@@ -190,75 +193,78 @@ class Foo1 { has $.bar; };
}
# L<A12/Default Values>
-ok eval('class Foo7 { has $.attr = 42 }'), "class definition worked";
+ok eval('class Foo7 { has $.attr = 42 }; 1'), "class definition worked";
is eval('Foo7.new.attr'), 42, "default attribute value (1)";
# L<A12/Default Values/is equivalent to this:>
-ok eval('class Foo8 { has $.attr is build(42) }'),
+ok eval('class Foo8 { has $.attr is build(42) }; 1'),
"class definition using 'is build' worked";
is eval('Foo8.new.attr'), 42, "default attribute value (2)";
# L<A12/Default Values/is equivalent to this:>
-ok eval('class Foo9 { has $.attr will build(42) }'),
+ok eval('class Foo9 { has $.attr will build(42) }; 1'),
"class definition using 'will build' worked";
is eval('Foo9.new.attr'), 42, "default attribute value (3)";
my $was_in_supplier = 0;
sub forty_two_supplier() { $was_in_supplier++; 42 }
-# XXX: Currently hard parsefail!
-ok eval('class Foo10 { has $.attr = { forty_two_supplier() } }'),
- "class definition using '= {...}' worked";
+ok eval('class Foo10 { has $.attr = { forty_two_supplier() } }; 1'),
+ 'class definition using "= {...}" worked';
is eval('Foo10.new.attr'), 42, "default attribute value (4)";
is $was_in_supplier, 1, "forty_two_supplier() was actually executed (1)";
# The same, but using 'is build {...}'
# XXX: Currently hard parsefail!
-ok eval('class Foo11 { has $.attr is build { forty_two_supplier() } }'),
- "class definition using 'is build {...}' worked";
+ok eval('class Foo11 { has $.attr is build { forty_two_supplier() } }; 1'),
+ 'class definition using "is build {...}" worked';
is eval('Foo11.new.attr'), 42, "default attribute value (5)";
is $was_in_supplier, 2, "forty_two_supplier() was actually executed (2)";
# The same, but using 'will build {...}'
# XXX: Currently hard parsefail!
-ok eval('class Foo12 { has $.attr will build { forty_two_supplier() } }'),
+ok eval('class Foo12 { has $.attr will build { forty_two_supplier() } }; 1'),
"class definition using 'will build {...}' worked";
is eval('Foo11.new.attr'), 42, "default attribute value (6)";
is $was_in_supplier, 3, "forty_two_supplier() was actually executed (3)";
# check that doing something in submethod BUILD works
-class Foo7 {
- has $.bar;
- has $.baz;
+#?rakudo skip 'parse failure'
+{
+ class Foo7 {
+ has $.bar;
+ has $.baz;
- submethod BUILD ($.bar = 5, $baz = 10 ) {
- $.baz = 2 * $baz;
- }
-}
+ submethod BUILD ($.bar = 5, $baz = 10 ) {
+ $.baz = 2 * $baz;
+ }
+ }
-my $foo7 = Foo7.new();
-is( $foo7.bar, 5,
- 'optional attribute should take default value without passed-in value' );
-is( $foo7.baz, 20,
- '... optional non-attribute should too' );
-$foo7 = Foo7.new( :bar(4), :baz(5) );
-is( $foo7.bar, 4,
- 'optional attribute should take passed-in value over default' );
-is( $foo7.baz, 10,
- '... optional non-attribute should too' );
+ my $foo7 = Foo7.new();
+ is( $foo7.bar, 5,
+ 'optional attribute should take default value without passed-in value' );
+ is( $foo7.baz, 20,
+ '... optional non-attribute should too' );
+ $foo7 = Foo7.new( :bar(4), :baz(5) );
+ is( $foo7.bar, 4,
+ 'optional attribute should take passed-in value over default' );
+ is( $foo7.baz, 10,
+ '... optional non-attribute should too' );
+}
# check that args are passed to BUILD
-class Foo8 {
- has $.a;
- has $.b;
-
- submethod BUILD(:$foo, :$bar) {
- $.a = $foo;
- $.b = $bar;
- }
-}
-
+#?rakudo skip 'submethod parsing'
{
+ class Foo8 {
+ has $.a;
+ has $.b;
+
+ submethod BUILD(:$foo, :$bar) {
+ $.a = $foo;
+ $.b = $bar;
+ }
+ }
+
my $foo = Foo8.new(foo => 'c', bar => 'd');
ok($foo.isa(Foo8), '... our Foo8 instance was created');
@@ -268,36 +274,44 @@ class Foo8 {
# check mixture of positional/named args to BUILD
-class Foo9 {
- has $.a;
- has $.b;
-
- submethod BUILD($foo, :$bar) {
- $.a = $foo;
- $.b = $bar;
- }
-}
-
-dies_ok({ Foo9.new('pos', bar => 'd') }, 'cannot pass positional to .new');
+#?rakudo skip 'submethod parsing'
+{
+ class Foo9 {
+ has $.a;
+ has $.b;
+
+ submethod BUILD($foo, :$bar) {
+ $.a = $foo;
+ $.b = $bar;
+ }
+ }
-# check $self is passed to BUILD
-class Foo10 {
- has $.a;
- has $.b;
- has $.c;
-
- submethod BUILD(Class $self: :$foo, :$bar) {
- $.a = $foo;
- $.b = $bar;
- $.c = 'y' if $self.isa(Foo10);
- }
+ dies_ok({ Foo9.new('pos', bar => 'd') }, 'cannot pass positional to .new');
}
+# check $self is passed to BUILD
+#?rakudo skip 'submethod parsing'
{
- my $foo = Foo10.new(foo => 'c', bar => 'd');
- ok($foo.isa(Foo10), '... our Foo10 instance was created');
+ class Foo10 {
+ has $.a;
+ has $.b;
+ has $.c;
- is($foo.a, 'c', 'BUILD received $foo');
- is($foo.b, 'd', 'BUILD received $bar');
- is($foo.c, 'y', 'BUILD received $self');
+ submethod BUILD(Class $self: :$foo, :$bar) {
+ $.a = $foo;
+ $.b = $bar;
+ $.c = 'y' if $self.isa(Foo10);
+ }
+ }
+
+ {
+ my $foo = Foo10.new(foo => 'c', bar => 'd');
+ ok($foo.isa(Foo10), '... our Foo10 instance was created');
+
+ is($foo.a, 'c', 'BUILD received $foo');
+ is($foo.b, 'd', 'BUILD received $bar');
+ is($foo.c, 'y', 'BUILD received $self');
+ }
}
+
+# vim: ft=perl6

0 comments on commit b8116e0

Please sign in to comment.