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/metaclasses/create_anon_with_required_attr.t | |
download | Moose-tarball-5ac2026f7eed78958d69d051e7a8e993dcf51205.tar.gz |
Moose-2.1405HEADMoose-2.1405master
Diffstat (limited to 't/metaclasses/create_anon_with_required_attr.t')
-rw-r--r-- | t/metaclasses/create_anon_with_required_attr.t | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/t/metaclasses/create_anon_with_required_attr.t b/t/metaclasses/create_anon_with_required_attr.t new file mode 100644 index 0000000..3a37773 --- /dev/null +++ b/t/metaclasses/create_anon_with_required_attr.t @@ -0,0 +1,86 @@ +use strict; +use warnings; + +# this functionality may be pushing toward parametric roles/classes +# it's off in a corner and may not be that important + +use Test::More; +use Test::Fatal; + +{ + package HasFoo; + use Moose::Role; + has 'foo' => ( + is => 'ro', + isa => 'Str', + required => 1, + ); + +} + +{ + package My::Metaclass; + use Moose; + extends 'Moose::Meta::Class'; + with 'HasFoo'; +} + +package main; + +my $anon; +is( exception { + $anon = My::Metaclass->create_anon_class( foo => 'this' ); +}, undef, 'create anon class with required attr' ); +isa_ok( $anon, 'My::Metaclass' ); +cmp_ok( $anon->foo, 'eq', 'this', 'foo is this' ); +isnt( exception { + $anon = My::Metaclass->create_anon_class(); +}, undef, 'failed to create anon class without required attr' ); + +my $meta; +is( exception { + $meta + = My::Metaclass->initialize( 'Class::Name1' => ( foo => 'that' ) ); +}, undef, 'initialize a class with required attr' ); +isa_ok( $meta, 'My::Metaclass' ); +cmp_ok( $meta->foo, 'eq', 'that', 'foo is that' ); +cmp_ok( $meta->name, 'eq', 'Class::Name1', 'for the correct class' ); +isnt( exception { + $meta + = My::Metaclass->initialize( 'Class::Name2' ); +}, undef, 'failed to initialize a class without required attr' ); + +is( exception { + eval qq{ + package Class::Name3; + use metaclass 'My::Metaclass' => ( + foo => 'another', + ); + use Moose; + }; + die $@ if $@; +}, undef, 'use metaclass with required attr' ); +$meta = Class::Name3->meta; +isa_ok( $meta, 'My::Metaclass' ); +cmp_ok( $meta->foo, 'eq', 'another', 'foo is another' ); +cmp_ok( $meta->name, 'eq', 'Class::Name3', 'for the correct class' ); +isnt( exception { + eval qq{ + package Class::Name4; + use metaclass 'My::Metaclass'; + use Moose; + }; + die $@ if $@; +}, undef, 'failed to use metaclass without required attr' ); + + +# how do we pass a required attribute to -traits? +isnt( exception { + eval qq{ + package Class::Name5; + use Moose -traits => 'HasFoo'; + }; + die $@ if $@; +}, undef, 'failed to use trait without required attr' ); + +done_testing; |