Remove useless whitespace
[gitmo/MooseX-Singleton.git] / t / 002-init.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5
6 my $i = 0;
7 sub new_singleton_pkg {
8   my $pkg_name = sprintf 'MooseX::Singleton::Test%s', $i++;
9   eval qq{
10     package $pkg_name;
11     use MooseX::Singleton;
12     has number => (is => 'rw', isa => 'Num', required => 1);
13     has string => (is => 'rw', isa => 'Str', default  => 'Hello!');
14   };
15
16   return $pkg_name;
17 }
18
19 throws_ok { new_singleton_pkg()->instance }
20     qr/\QAttribute (number) is required/,
21     q{can't get the Singleton if requires attrs and we don't provide them};
22
23 throws_ok { new_singleton_pkg()->string }
24     qr/\QAttribute (number) is required/,
25     q{can't call any Singleton attr reader if Singleton can't be inited};
26
27 for my $pkg (new_singleton_pkg) {
28     my $mst = $pkg->new( number => 5 );
29     isa_ok( $mst, $pkg );
30
31     is( $mst->number, 5, "the instance has the given attribute value" );
32
33     is(
34         $pkg->number,
35         5,
36         "the class method, called directly, returns the given attribute value"
37     );
38
39     throws_ok { $pkg->new( number => 3 ) }
40         qr/already/,
41         "can't make new singleton with conflicting attributes";
42
43     my $second = eval { $pkg->new };
44     ok( !$@, "...but a second ->new without args is okay" );
45
46     is( $second->number, 5,
47         "...we get the originally inited number from it" );
48
49     throws_ok { $pkg->initialize }
50         qr/already/,
51         "...but ->initialize() is still an error";
52 }
53
54 {
55     package Single;
56
57     use MooseX::Singleton;
58
59     has foo => ( is => 'ro' );
60 }
61
62 {
63     Single->initialize( foo => 2 );
64     ok( Single->new, 'can call ->new without any args' );
65     ok( Single->instance, 'can call ->instance without any args' );
66 }
67
68 done_testing;