X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F003-immutable.t;h=618391e9228c94e106982cebd07246645e010d58;hb=f943458f1cf1fba934f789e3eebf000e67da892b;hp=e5a4047215f1010b1ec92d9bde29ecb939b33c57;hpb=ade9ece08d09a5a858e7978a970831f0f65ac3a4;p=gitmo%2FMooseX-Singleton.git diff --git a/t/003-immutable.t b/t/003-immutable.t index e5a4047..618391e 100644 --- a/t/003-immutable.t +++ b/t/003-immutable.t @@ -1,15 +1,12 @@ use strict; use warnings; +use Scalar::Util qw( refaddr ); use Test::More; BEGIN { - unless ( eval 'use Test::Warn; 1' ) { - plan skip_all => 'These tests require Test::Warn'; - } - else { - plan tests => 17; - } + eval 'use Test::Warn'; + plan skip_all => 'These tests require Test::Warn' if $@; } { @@ -29,54 +26,70 @@ BEGIN { sub clear { my $self = shift; - $self->bag({}); + $self->bag( {} ); } sub add { - my $self = shift; - my $key = shift; + my $self = shift; + my $key = shift; my $value = @_ ? shift : 1; $self->bag->{$key} += $value; } -__PACKAGE__->meta->make_immutable; + ::warning_is sub { __PACKAGE__->meta->make_immutable }, '', 'no warnings when calling make_immutable'; } my $mst = MooseX::Singleton::Test->instance; -isa_ok($mst, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance'); +isa_ok( $mst, 'MooseX::Singleton::Test', + 'Singleton->instance returns a real instance' ); -is($mst->distinct_keys, 1, "default keys"); +is( $mst->distinct_keys, 1, "default keys" ); -$mst->add(foo => 10); -is($mst->distinct_keys, 2, "added key"); +$mst->add( foo => 10 ); +is( $mst->distinct_keys, 2, "added key" ); -$mst->add(bar => 5); -is($mst->distinct_keys, 3, "added another key"); +$mst->add( bar => 5 ); +is( $mst->distinct_keys, 3, "added another key" ); my $mst2 = MooseX::Singleton::Test->instance; -is($mst, $mst2, 'instances are the same object'); -isa_ok($mst2, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance'); +is( $mst, $mst2, 'instances are the same object' ); +isa_ok( $mst2, 'MooseX::Singleton::Test', + 'Singleton->instance returns a real instance' ); -is($mst2->distinct_keys, 3, "keys from before"); +is( $mst2->distinct_keys, 3, "keys from before" ); -$mst->add(baz => 2); +$mst->add( baz => 2 ); -is($mst->distinct_keys, 4, "attributes are shared even after ->instance"); -is($mst2->distinct_keys, 4, "attributes are shared even after ->instance"); +is( $mst->distinct_keys, 4, "attributes are shared even after ->instance" ); +is( $mst2->distinct_keys, 4, "attributes are shared even after ->instance" ); -is(MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works"); +is( MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works" ); -MooseX::Singleton::Test->add(quux => 9000); +MooseX::Singleton::Test->add( quux => 9000 ); -is($mst->distinct_keys, 5, "Package->add works"); -is($mst2->distinct_keys, 5, "Package->add works"); -is(MooseX::Singleton::Test->distinct_keys, 5, "Package->add works"); +is( $mst->distinct_keys, 5, "Package->add works" ); +is( $mst2->distinct_keys, 5, "Package->add works" ); +is( MooseX::Singleton::Test->distinct_keys, 5, "Package->add works" ); MooseX::Singleton::Test->clear; -is($mst->distinct_keys, 0, "Package->clear works"); -is($mst2->distinct_keys, 0, "Package->clear works"); -is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works"); +is( $mst->distinct_keys, 0, "Package->clear works" ); +is( $mst2->distinct_keys, 0, "Package->clear works" ); +is( MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works" ); + +{ + my $addr; + + { + $addr = refaddr( MooseX::Singleton::Test->instance ); + } + + is( + $addr, refaddr( MooseX::Singleton::Test->instance ), + 'singleton is not randomly destroyed' + ); +} +done_testing;