bump version to 0.16
[gitmo/MooseX-Singleton.git] / t / 003-immutable.t
CommitLineData
2b4ce4bd 1use strict;
2use warnings;
3
4use Test::More;
5
6BEGIN {
7 unless ( eval 'use Test::Warn; 1' ) {
8 plan skip_all => 'These tests require Test::Warn';
9 }
10 else {
11 plan tests => 17;
12 }
13}
14
15BEGIN {
16 package MooseX::Singleton::Test;
17 use MooseX::Singleton;
18
19 has bag => (
20 is => 'rw',
21 isa => 'HashRef[Int]',
22 default => sub { { default => 42 } },
23 );
24
25 sub distinct_keys {
26 my $self = shift;
27 scalar keys %{ $self->bag };
28 }
29
30 sub clear {
31 my $self = shift;
32 $self->bag({});
33 }
34
35 sub add {
36 my $self = shift;
37 my $key = shift;
38 my $value = @_ ? shift : 1;
39
40 $self->bag->{$key} += $value;
41 }
42
0ab35c13 43 ::warning_is sub { __PACKAGE__->meta->make_immutable }, '',
2b4ce4bd 44 'no warnings when calling make_immutable';
45}
46
47my $mst = MooseX::Singleton::Test->instance;
48isa_ok($mst, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
49
50is($mst->distinct_keys, 1, "default keys");
51
52$mst->add(foo => 10);
53is($mst->distinct_keys, 2, "added key");
54
55$mst->add(bar => 5);
56is($mst->distinct_keys, 3, "added another key");
57
58my $mst2 = MooseX::Singleton::Test->instance;
59is($mst, $mst2, 'instances are the same object');
60isa_ok($mst2, 'MooseX::Singleton::Test', 'Singleton->instance returns a real instance');
61
62is($mst2->distinct_keys, 3, "keys from before");
63
64$mst->add(baz => 2);
65
66is($mst->distinct_keys, 4, "attributes are shared even after ->instance");
67is($mst2->distinct_keys, 4, "attributes are shared even after ->instance");
68
69is(MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works");
70
71MooseX::Singleton::Test->add(quux => 9000);
72
73is($mst->distinct_keys, 5, "Package->add works");
74is($mst2->distinct_keys, 5, "Package->add works");
75is(MooseX::Singleton::Test->distinct_keys, 5, "Package->add works");
76
77MooseX::Singleton::Test->clear;
78
79is($mst->distinct_keys, 0, "Package->clear works");
80is($mst2->distinct_keys, 0, "Package->clear works");
81is(MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works");
82