X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Singleton.git;a=blobdiff_plain;f=t%2F002-init.t;h=bb1a5481542393761899972224692c0041d104ad;hp=8b5f14406b016def63cf44df40ba5fa2a4da1dd1;hb=e6fd0d07a3e79832893c8095ec634f9481c6aff9;hpb=d928ce3a26634463da667f6bdba903e94b31ebac diff --git a/t/002-init.t b/t/002-init.t index 8b5f144..bb1a548 100644 --- a/t/002-init.t +++ b/t/002-init.t @@ -1,6 +1,7 @@ use strict; use warnings; -use Test::More tests => 9; +use Test::More; +use Test::Exception; my $i = 0; sub new_singleton_pkg { @@ -15,41 +16,53 @@ sub new_singleton_pkg { return $pkg_name; } -eval { new_singleton_pkg()->instance; }; -like( - $@, - qr/\QAttribute (number) is required/, - q{can't get the Singleton if requires attrs and we don't provide them}, -); +throws_ok { new_singleton_pkg()->instance } + qr/\QAttribute (number) is required/, + q{can't get the Singleton if requires attrs and we don't provide them}; -eval { new_singleton_pkg()->string; }; -like( - $@, - qr/\QAttribute (number) is required/, - q{can't call any Singleton attr reader if Singleton can't be inited}, -); +throws_ok { new_singleton_pkg()->string } + qr/\QAttribute (number) is required/, + q{can't call any Singleton attr reader if Singleton can't be inited}; for my $pkg (new_singleton_pkg) { - my $mst = $pkg->new(number => 5); - isa_ok($mst, $pkg); + my $mst = $pkg->new( number => 5 ); + isa_ok( $mst, $pkg ); - is($mst->number, 5, "the instance has the given attribute value"); + is( $mst->number, 5, "the instance has the given attribute value" ); - is( - $pkg->number, - 5, - "the class method, called directly, returns the given attribute value" - ); + is( + $pkg->number, + 5, + "the class method, called directly, returns the given attribute value" + ); - eval { $pkg->new(number => 3) }; - like($@, qr/already/, "can't make new singleton with conflicting attributes"); + throws_ok { $pkg->new( number => 3 ) } + qr/already/, + "can't make new singleton with conflicting attributes"; - my $second = eval { $pkg->new }; - ok(!$@, "...but a second ->new without args is okay"); + my $second = eval { $pkg->new }; + ok( !$@, "...but a second ->new without args is okay" ); - is($second->number, 5, "...we get the originally inited number from it"); + is( $second->number, 5, + "...we get the originally inited number from it" ); - eval { $pkg->initialize }; - like($@, qr/already/, "...but ->initialize() is still an error"); + throws_ok { $pkg->initialize } + qr/already/, + "...but ->initialize() is still an error"; } +{ + package Single; + + use MooseX::Singleton; + + has foo => ( is => 'ro' ); +} + +{ + Single->initialize( foo => 2 ); + ok( Single->new, 'can call ->new without any args' ); + ok( Single->instance, 'can call ->instance without any args' ); +} + +done_testing;