4 use Scalar::Util qw( refaddr );
8 unless ( eval 'use Test::Warn; 1' ) {
9 plan skip_all => 'These tests require Test::Warn';
17 package MooseX::Singleton::Test;
18 use MooseX::Singleton;
22 isa => 'HashRef[Int]',
23 default => sub { { default => 42 } },
28 scalar keys %{ $self->bag };
39 my $value = @_ ? shift : 1;
41 $self->bag->{$key} += $value;
44 ::warning_is sub { __PACKAGE__->meta->make_immutable }, '',
45 'no warnings when calling make_immutable';
48 my $mst = MooseX::Singleton::Test->instance;
49 isa_ok( $mst, 'MooseX::Singleton::Test',
50 'Singleton->instance returns a real instance' );
52 is( $mst->distinct_keys, 1, "default keys" );
54 $mst->add( foo => 10 );
55 is( $mst->distinct_keys, 2, "added key" );
57 $mst->add( bar => 5 );
58 is( $mst->distinct_keys, 3, "added another key" );
60 my $mst2 = MooseX::Singleton::Test->instance;
61 is( $mst, $mst2, 'instances are the same object' );
62 isa_ok( $mst2, 'MooseX::Singleton::Test',
63 'Singleton->instance returns a real instance' );
65 is( $mst2->distinct_keys, 3, "keys from before" );
67 $mst->add( baz => 2 );
69 is( $mst->distinct_keys, 4, "attributes are shared even after ->instance" );
70 is( $mst2->distinct_keys, 4, "attributes are shared even after ->instance" );
72 is( MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works" );
74 MooseX::Singleton::Test->add( quux => 9000 );
76 is( $mst->distinct_keys, 5, "Package->add works" );
77 is( $mst2->distinct_keys, 5, "Package->add works" );
78 is( MooseX::Singleton::Test->distinct_keys, 5, "Package->add works" );
80 MooseX::Singleton::Test->clear;
82 is( $mst->distinct_keys, 0, "Package->clear works" );
83 is( $mst2->distinct_keys, 0, "Package->clear works" );
84 is( MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works" );
90 $addr = refaddr( MooseX::Singleton::Test->instance );
94 $addr, refaddr( MooseX::Singleton::Test->instance ),
95 'singleton is not randomly destroyed'