Created
October 25, 2010 20:18
-
-
Save EvanCarroll/645659 to your computer and use it in GitHub Desktop.
This file demonstrates the counter-intuitive behavior of Moose in regards to strict-defaults, and constructor provided arguments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Class; | |
use Moose; | |
has "foo" => ( | |
isa => 'Str' | |
, is => 'ro' | |
, predicate => '_has_foo' | |
); | |
has "bar" => ( | |
is => 'ro' | |
, isa => 'Str' | |
, default => sub { | |
my $self = shift; | |
die "E100 Attribute foo was not set with implicit Moose-provided constructor, before attribute bar's default" | |
unless $self->_has_foo | |
; | |
"DOOG"; | |
} | |
); | |
package main; | |
use Test::More tests => 2; | |
my $o; | |
eval { | |
$o = Class->new({foo=>"GOOD"})->bar | |
}; | |
SKIP: { | |
unlike ( $@, qr/E100/, "constructor provided arguments filled before non-lazy defaults" ) | |
|| skip "we've got an error", 1 | |
; | |
like ( $o->bar, qr/DOOG/, "non-lazy default dependant on constructor provided argument set properly" ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment