Commit | Line | Data |
2b4ce4bd |
1 | use strict; |
2 | use warnings; |
3 | |
a82b9823 |
4 | use Scalar::Util qw( refaddr ); |
2b4ce4bd |
5 | use Test::More; |
6 | |
7 | BEGIN { |
4c256923 |
8 | unless ( eval 'use Test::Warn; 1' ) { |
2b4ce4bd |
9 | plan skip_all => 'These tests require Test::Warn'; |
10 | } |
11 | else { |
a82b9823 |
12 | plan tests => 18; |
2b4ce4bd |
13 | } |
14 | } |
15 | |
42915f8a |
16 | { |
2b4ce4bd |
17 | package MooseX::Singleton::Test; |
18 | use MooseX::Singleton; |
19 | |
20 | has bag => ( |
21 | is => 'rw', |
22 | isa => 'HashRef[Int]', |
23 | default => sub { { default => 42 } }, |
24 | ); |
25 | |
26 | sub distinct_keys { |
27 | my $self = shift; |
28 | scalar keys %{ $self->bag }; |
29 | } |
30 | |
31 | sub clear { |
32 | my $self = shift; |
4c256923 |
33 | $self->bag( {} ); |
2b4ce4bd |
34 | } |
35 | |
36 | sub add { |
4c256923 |
37 | my $self = shift; |
38 | my $key = shift; |
2b4ce4bd |
39 | my $value = @_ ? shift : 1; |
40 | |
41 | $self->bag->{$key} += $value; |
42 | } |
b6591604 |
43 | |
0ab35c13 |
44 | ::warning_is sub { __PACKAGE__->meta->make_immutable }, '', |
2b4ce4bd |
45 | 'no warnings when calling make_immutable'; |
46 | } |
47 | |
48 | my $mst = MooseX::Singleton::Test->instance; |
4c256923 |
49 | isa_ok( $mst, 'MooseX::Singleton::Test', |
50 | 'Singleton->instance returns a real instance' ); |
2b4ce4bd |
51 | |
4c256923 |
52 | is( $mst->distinct_keys, 1, "default keys" ); |
2b4ce4bd |
53 | |
4c256923 |
54 | $mst->add( foo => 10 ); |
55 | is( $mst->distinct_keys, 2, "added key" ); |
2b4ce4bd |
56 | |
4c256923 |
57 | $mst->add( bar => 5 ); |
58 | is( $mst->distinct_keys, 3, "added another key" ); |
2b4ce4bd |
59 | |
60 | my $mst2 = MooseX::Singleton::Test->instance; |
4c256923 |
61 | is( $mst, $mst2, 'instances are the same object' ); |
62 | isa_ok( $mst2, 'MooseX::Singleton::Test', |
63 | 'Singleton->instance returns a real instance' ); |
2b4ce4bd |
64 | |
4c256923 |
65 | is( $mst2->distinct_keys, 3, "keys from before" ); |
2b4ce4bd |
66 | |
4c256923 |
67 | $mst->add( baz => 2 ); |
2b4ce4bd |
68 | |
4c256923 |
69 | is( $mst->distinct_keys, 4, "attributes are shared even after ->instance" ); |
70 | is( $mst2->distinct_keys, 4, "attributes are shared even after ->instance" ); |
2b4ce4bd |
71 | |
4c256923 |
72 | is( MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works" ); |
2b4ce4bd |
73 | |
4c256923 |
74 | MooseX::Singleton::Test->add( quux => 9000 ); |
2b4ce4bd |
75 | |
4c256923 |
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" ); |
2b4ce4bd |
79 | |
80 | MooseX::Singleton::Test->clear; |
81 | |
4c256923 |
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" ); |
2b4ce4bd |
85 | |
a82b9823 |
86 | { |
87 | my $addr; |
88 | |
89 | { |
90 | $addr = refaddr( MooseX::Singleton::Test->instance ); |
91 | } |
92 | |
4c256923 |
93 | is( |
94 | $addr, refaddr( MooseX::Singleton::Test->instance ), |
95 | 'singleton is not randomly destroyed' |
96 | ); |
a82b9823 |
97 | } |