1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
use Moo::_strictures;
use Test::More;
{
package Qux;
use Moo;
has bar => ( is => "rw" );
has baz => ( is => "rw" );
package Quux;
use Moo;
extends qw(Qux);
}
{
package NonMooClass;
sub new {
my ($class, $arg) = @_;
bless { attr => $arg }, $class;
}
sub attr { shift->{attr} }
package Extends::NonMooClass::WithAttr;
use Moo;
extends qw( NonMooClass );
has 'attr2' => ( is => 'ro' );
sub BUILDARGS {
my ( $class, @args ) = @_;
shift @args if @args % 2 == 1;
return { @args };
}
}
{
package Foo;
use Moo;
has bar => ( is => "rw" );
has baz => ( is => "rw" );
sub BUILDARGS {
my ( $class, @args ) = @_;
unshift @args, "bar" if @args % 2 == 1;
return $class->SUPER::BUILDARGS(@args);
}
package Bar;
use Moo;
extends qw(Foo);
}
{
package Baz;
use Moo;
has bar => ( is => "rw" );
has baz => ( is => "rw" );
around BUILDARGS => sub {
my $orig = shift;
my ( $class, @args ) = @_;
unshift @args, "bar" if @args % 2 == 1;
return $class->$orig(@args);
};
package Biff;
use Moo;
extends qw(Baz);
}
foreach my $class (qw(Foo Bar Baz Biff)) {
is( $class->new->bar, undef, "no args" );
is( $class->new( bar => 42 )->bar, 42, "normal args" );
is( $class->new( 37 )->bar, 37, "single arg" );
{
my $o = $class->new(bar => 42, baz => 47);
is($o->bar, 42, '... got the right bar');
is($o->baz, 47, '... got the right baz');
}
{
my $o = $class->new(42, baz => 47);
is($o->bar, 42, '... got the right bar');
is($o->baz, 47, '... got the right baz');
}
}
foreach my $class (qw(Qux Quux)) {
my $o = $class->new(bar => 42, baz => 47);
is($o->bar, 42, '... got the right bar');
is($o->baz, 47, '... got the right baz');
eval {
$class->new( 37 );
};
like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
"new() requires a list or a HASH ref"
);
eval {
$class->new( [ 37 ] );
};
like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
"new() requires a list or a HASH ref"
);
eval {
$class->new( bar => 42, baz => 47, 'quux' );
};
like( $@, qr/You passed an odd number of arguments/,
"new() requires a list or a HASH ref"
);
}
my $non_moo = NonMooClass->new( 'bar' );
my $ext_non_moo = Extends::NonMooClass::WithAttr->new( 'bar', attr2 => 'baz' );
is $non_moo->attr, 'bar',
"non-moo accepts params";
is $ext_non_moo->attr, 'bar',
"extended non-moo passes params";
is $ext_non_moo->attr2, 'baz',
"extended non-moo has own attributes";
{
package NoAttr;
use Moo;
before BUILDARGS => sub {
our $buildargs_called++;
};
}
eval {
NoAttr->BUILDARGS( 37 );
};
like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
"default BUILDARGS requires a list or a HASH ref"
);
$NoAttr::buildargs_called = 0;
my $noattr = NoAttr->new({ foo => 'bar' });
is $noattr->{foo}, undef, 'without attributes, no params are stored';
is $NoAttr::buildargs_called, 1, 'BUILDARGS called even without attributes';
done_testing;
|