Remove useless whitespace
[gitmo/MooseX-Singleton.git] / t / 002-init.t
CommitLineData
1de95613 1use strict;
2use warnings;
e01c7277 3use Test::More;
a5507842 4use Test::Exception;
1de95613 5
6my $i = 0;
7sub 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
a5507842 19throws_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};
1de95613 22
a5507842 23throws_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};
1de95613 26
27for my $pkg (new_singleton_pkg) {
a5507842 28 my $mst = $pkg->new( number => 5 );
29 isa_ok( $mst, $pkg );
1de95613 30
a5507842 31 is( $mst->number, 5, "the instance has the given attribute value" );
1de95613 32
a5507842 33 is(
34 $pkg->number,
35 5,
36 "the class method, called directly, returns the given attribute value"
37 );
1de95613 38
a5507842 39 throws_ok { $pkg->new( number => 3 ) }
40 qr/already/,
41 "can't make new singleton with conflicting attributes";
1de95613 42
a5507842 43 my $second = eval { $pkg->new };
44 ok( !$@, "...but a second ->new without args is okay" );
1de95613 45
a5507842 46 is( $second->number, 5,
47 "...we get the originally inited number from it" );
d928ce3a 48
a5507842 49 throws_ok { $pkg->initialize }
50 qr/already/,
51 "...but ->initialize() is still an error";
1de95613 52}
53
61c6d582 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}
e01c7277 67
68done_testing;