File: 001-basic.t

package info (click to toggle)
libmoosex-singleton-perl 0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 164 kB
  • ctags: 12
  • sloc: perl: 279; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 2,075 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use strict;
use warnings;
use Test::More;

BEGIN {
    package MooseX::Singleton::Test;
    use MooseX::Singleton;

    has bag => (
        is      => 'rw',
        isa     => 'HashRef[Int]',
        default => sub { { default => 42 } },
    );

    sub distinct_keys {
        my $self = shift;
        scalar keys %{ $self->bag };
    }

    sub clear {
        my $self = shift;
        $self->bag( {} );
    }

    sub add {
        my $self  = shift;
        my $key   = shift;
        my $value = @_ ? shift : 1;

        $self->bag->{$key} += $value;
    }
}

my $mst = MooseX::Singleton::Test->instance;
isa_ok( $mst, 'MooseX::Singleton::Test',
    'Singleton->instance returns a real instance' );

is( $mst->distinct_keys, 1, "default keys" );

$mst->add( foo => 10 );
is( $mst->distinct_keys, 2, "added key" );

$mst->add( bar => 5 );
is( $mst->distinct_keys, 3, "added another key" );

my $mst2 = MooseX::Singleton::Test->instance;
is( $mst, $mst2, 'instances are the same object' );
isa_ok( $mst2, 'MooseX::Singleton::Test',
    'Singleton->instance returns a real instance' );

is( $mst2->distinct_keys, 3, "keys from before" );

$mst->add( baz => 2 );

is( $mst->distinct_keys,  4, "attributes are shared even after ->instance" );
is( $mst2->distinct_keys, 4, "attributes are shared even after ->instance" );

is( MooseX::Singleton::Test->distinct_keys, 4, "Package->reader works" );

MooseX::Singleton::Test->add( quux => 9000 );

is( $mst->distinct_keys,                    5, "Package->add works" );
is( $mst2->distinct_keys,                   5, "Package->add works" );
is( MooseX::Singleton::Test->distinct_keys, 5, "Package->add works" );

MooseX::Singleton::Test->clear;

is( $mst->distinct_keys,                    0, "Package->clear works" );
is( $mst2->distinct_keys,                   0, "Package->clear works" );
is( MooseX::Singleton::Test->distinct_keys, 0, "Package->clear works" );

MooseX::Singleton::Test->_clear_instance;
$mst = $mst2 = undef;
is( MooseX::Singleton::Test->instance->distinct_keys, 1, "back to the default" );

done_testing;