X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F020_attributes%2F012_misc_attribute_tests.t;h=ac532c655437f8a10710ea4f387c970b239378f9;hb=ea829e77f657851c78cd65dd2b7ed05ce6c6ffff;hp=24e3f70f12ddde8a42999ac7dfad5dbbb720d52d;hpb=1580432f71e8d42a4070f19f15e94a062300fb47;p=gitmo%2FMoose.git diff --git a/t/020_attributes/012_misc_attribute_tests.t b/t/020_attributes/012_misc_attribute_tests.t index 24e3f70..ac532c6 100644 --- a/t/020_attributes/012_misc_attribute_tests.t +++ b/t/020_attributes/012_misc_attribute_tests.t @@ -3,12 +3,9 @@ use strict; use warnings; -use Test::More tests => 44; -use Test::Exception; +use Test::More; +use Test::Fatal; -BEGIN { - use_ok('Moose'); -} { { @@ -19,7 +16,8 @@ BEGIN { documentation => q{ The 'foo' attribute is my favorite attribute in the whole wide world. - } + }, + is => 'bare', ); } @@ -59,13 +57,13 @@ BEGIN { my $test = Test::For::Lazy::TypeConstraint->new; isa_ok($test, 'Test::For::Lazy::TypeConstraint'); - dies_ok { + isnt( exception { $test->bad_lazy_attr; - } '... this does not work'; + }, undef, '... this does not work' ); - lives_ok { + is( exception { $test->good_lazy_attr; - } '... this does not work'; + }, undef, '... this does not work' ); } { @@ -120,9 +118,9 @@ BEGIN { } - dies_ok { + isnt( exception { Test::UndefDefault::Attributes->new; - } '... default must return a value which passes the type constraint'; + }, undef, '... default must return a value which passes the type constraint' ); } @@ -140,10 +138,9 @@ BEGIN { is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string'); ok($moose_obj, 'this is a *not* a string'); - throws_ok { + like( exception { $moose_obj->a_str( $moose_obj ) - } qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' failed with value OverloadedStr=HASH\(0x.+?\)/, - '... dies without overloading the string'; + }, qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' with value OverloadedStr=HASH\(0x.+?\)/, '... dies without overloading the string' ); } @@ -155,14 +152,13 @@ BEGIN { has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 ); } - throws_ok { + like( exception { OverloadBreaker->new; - } qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' failed with value 7\.5/, - '... this doesnt trip overload to break anymore '; + }, qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' with value 7\.5/, '... this doesnt trip overload to break anymore ' ); - lives_ok { + is( exception { OverloadBreaker->new(a_num => 5); - } '... this works fine though'; + }, undef, '... this works fine though' ); } @@ -186,17 +182,17 @@ BEGIN { is($instance->foo, 'works', "foo builder works"); } -{ +{ { package Test::Builder::Attribute::Broken; use Moose; has 'foo' => ( required => 1, builder => 'build_foo', is => 'ro'); } - - dies_ok { + + isnt( exception { Test::Builder::Attribute::Broken->new; - } '... no builder, wtf'; + }, undef, '... no builder, wtf' ); } @@ -217,7 +213,6 @@ BEGIN { my $_foo_attr = $meta->get_attribute("_foo"); ok($foo_attr->is_lazy, "foo is lazy"); - ok($foo_attr->is_required, "foo is required"); ok($foo_attr->is_lazy_build, "foo is lazy_build"); ok($foo_attr->has_clearer, "foo has clearer"); @@ -230,7 +225,7 @@ BEGIN { is($foo_attr->predicate, "has_foo", ".. and it's named has_foo"); ok($_foo_attr->is_lazy, "_foo is lazy"); - ok($_foo_attr->is_required, "_foo is required"); + ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required"); ok($_foo_attr->is_lazy_build, "_foo is lazy_build"); ok($_foo_attr->has_clearer, "_foo has clearer"); @@ -247,9 +242,31 @@ BEGIN { ok(!$instance->_has_foo, "noo _foo value yet"); is($instance->foo, 'works', "foo builder works"); is($instance->_foo, 'works too', "foo builder works too"); - throws_ok { $instance->fool } - qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/, - "Correct error when a builder method is not present"; + like( exception { $instance->fool }, qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/, "Correct error when a builder method is not present" ); + +} + +{ + package OutOfClassTest; + + use Moose; +} + +is( exception { OutOfClassTest::has('foo', is => 'bare'); }, undef, 'create attr via direct sub call' ); +is( exception { OutOfClassTest->can('has')->('bar', is => 'bare'); }, undef, 'create attr via can' ); + +ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call'); +ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can'); + + +{ + { + package Foo; + use Moose; + + ::like( ::exception { has 'foo' => ( 'ro', isa => 'Str' ) }, qr/^Usage/, 'has throws error with odd number of attribute options' ); + } } +done_testing;