3 use Test::More tests => 17;
6 package MooseX::Singleton::Test;
11 isa => 'HashRef[Int]',
12 default => sub { { default => 42 } },
17 scalar keys %{ $self->bag };
28 my $value = @_ ? shift : 1;
30 $self->bag->{$key} += $value;
34 my $mst = MooseX::Singleton::Test->instance;
35 isa_ok($mst, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
37 is($mst->distinct_keys, 1, "default keys");
40 is($mst->distinct_keys, 2, "added key");
43 is($mst->distinct_keys, 3, "added another key");
45 my $mst2 = MooseX::Singleton::Test->instance;
46 is($mst, $mst2, 'instances are the same object');
47 isa_ok($mst2, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
49 is($mst2->distinct_keys, 3, "keys from before");
53 is($mst->distinct_keys, 4, "attributes are shared even after ->instance");
54 is($mst2->distinct_keys, 4, "attributes are shared even after ->instance");
56 is(MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works");
58 MooseX::Singleton::Test->add(quux => 9000);
60 is($mst->distinct_keys, 5, "Package->add works");
61 is($mst2->distinct_keys, 5, "Package->add works");
62 is(MooseX::Singleton::Test->distinct_keys, 5, "Package->add works");
64 MooseX::Singleton::Test->clear;
66 is($mst->distinct_keys, 0, "Package->clear works");
67 is($mst2->distinct_keys, 0, "Package->clear works");
68 is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works");
70 MooseX::Singleton::Test->_clear_instance;
72 is(MooseX::Singleton::Test->new->distinct_keys, 1, "back to the default");