File: 13_dancer_singleton.t

package info (click to toggle)
libdancer-perl 1.3521%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,460 kB
  • sloc: perl: 7,436; xml: 2,211; sh: 54; makefile: 32; sql: 5
file content (42 lines) | stat: -rw-r--r-- 957 bytes parent folder | download | duplicates (6)
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
use strict;
use warnings;

use Test::More import => ['!pass'];

plan tests => 10;

my $test_counter = 0;

{
    package MySingleton;
    use base qw(Dancer::Object::Singleton);

    __PACKAGE__->attributes( qw/foo/ );

    sub init {
        my ($class, $instance) = @_;
        $test_counter++;
        $instance->foo('bar');
    }
}

eval { MySingleton->new() };
like $@, qr/you can't call 'new'/, 'new unauthorized';

eval { MySingleton->clone() };
like $@, qr/you can't call 'clone'/, 'clone unauthorized';

can_ok 'MySingleton', 'foo';

my $instance = MySingleton->instance();
ok $instance, 'instance build';
is $test_counter, 1, 'counter incremented';
is $instance->foo, 'bar', 'attribute is set';
$instance->foo('baz');
is $instance->foo, 'baz', 'attribute changed';

my $instance2 =  MySingleton->instance();
ok $instance2, 'instance retrieved';
is $instance2, $instance, 'instance is the same';
is $test_counter, 1, 'counter was not incremented';