diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-06-06 17:50:16 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2015-06-06 17:50:16 +0000 |
commit | 5ac2026f7eed78958d69d051e7a8e993dcf51205 (patch) | |
tree | 298c3d2f08bdfe5689998b11892d72a897985be1 /t/immutable/definition_context.t | |
download | Moose-tarball-5ac2026f7eed78958d69d051e7a8e993dcf51205.tar.gz |
Moose-2.1405HEADMoose-2.1405master
Diffstat (limited to 't/immutable/definition_context.t')
-rw-r--r-- | t/immutable/definition_context.t | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/t/immutable/definition_context.t b/t/immutable/definition_context.t new file mode 100644 index 0000000..71482df --- /dev/null +++ b/t/immutable/definition_context.t @@ -0,0 +1,82 @@ +use strict; +use warnings; +use Test::More; +use Test::Fatal; + +{ + use Moose::Util::TypeConstraints; + use Carp 'confess'; + subtype 'Death', as 'Int', where { $_ == 1 }; + coerce 'Death', from 'Any', via { confess }; +} + +{ + my ($attr_foo_line, $attr_bar_line, $ctor_line); + { + package Foo; + use Moose; + + has foo => ( + is => 'rw', + isa => 'Death', + coerce => 1, + ); + $attr_foo_line = __LINE__ - 5; + + has bar => ( + accessor => 'baz', + isa => 'Death', + coerce => 1, + ); + $attr_bar_line = __LINE__ - 5; + + __PACKAGE__->meta->make_immutable; + $ctor_line = __LINE__ - 1; + } + + like( + exception { Foo->new(foo => 2) }, + qr/\Qcalled at constructor Foo::new (defined at $0 line $ctor_line)\E/, + "got definition context for the constructor" + ); + + like( + exception { my $f = Foo->new(foo => 1); $f->foo(2) }, + qr/\Qcalled at accessor Foo::foo (defined at $0 line $attr_foo_line)\E/, + "got definition context for the accessor" + ); + + like( + exception { my $f = Foo->new(foo => 1); $f->baz(2) }, + qr/\Qcalled at accessor Foo::baz of attribute bar (defined at $0 line $attr_bar_line)\E/, + "got definition context for the accessor" + ); +} + +{ + my ($dtor_line); + { + package Bar; + use Moose; + + # just dying here won't work, because perl's exception handling is + # terrible + sub DEMOLISH { try { confess } catch { warn $_ } } + + __PACKAGE__->meta->make_immutable; + $dtor_line = __LINE__ - 1; + } + + { + my $warning = ''; + local $SIG{__WARN__} = sub { $warning .= $_[0] }; + { Bar->new } + like( + $warning, + qr/\Qcalled at destructor Bar::DESTROY (defined at $0 line $dtor_line)\E/, + "got definition context for the destructor" + ); + } +} + +done_testing; |