7 unless ( eval 'use Test::Warn; 1' ) {
8 plan skip_all => 'These tests require Test::Warn';
16 package MooseX::Singleton::Test;
17 use MooseX::Singleton;
21 isa => 'HashRef[Int]',
22 default => sub { { default => 42 } },
27 scalar keys %{ $self->bag };
38 my $value = @_ ? shift : 1;
40 $self->bag->{$key} += $value;
43 ::warning_is sub { __PACKAGE__->meta->make_immutable }, '',
44 'no warnings when calling make_immutable';
47 my $mst = MooseX::Singleton::Test->instance;
48 isa_ok($mst, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
50 is($mst->distinct_keys, 1, "default keys");
53 is($mst->distinct_keys, 2, "added key");
56 is($mst->distinct_keys, 3, "added another key");
58 my $mst2 = MooseX::Singleton::Test->instance;
59 is($mst, $mst2, 'instances are the same object');
60 isa_ok($mst2, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
62 is($mst2->distinct_keys, 3, "keys from before");
66 is($mst->distinct_keys, 4, "attributes are shared even after ->instance");
67 is($mst2->distinct_keys, 4, "attributes are shared even after ->instance");
69 is(MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works");
71 MooseX::Singleton::Test->add(quux => 9000);
73 is($mst->distinct_keys, 5, "Package->add works");
74 is($mst2->distinct_keys, 5, "Package->add works");
75 is(MooseX::Singleton::Test->distinct_keys, 5, "Package->add works");
77 MooseX::Singleton::Test->clear;
79 is($mst->distinct_keys, 0, "Package->clear works");
80 is($mst2->distinct_keys, 0, "Package->clear works");
81 is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works");