3 use Test::More tests => 11;
7 sub new_singleton_pkg {
8 my $pkg_name = sprintf 'MooseX::Singleton::Test%s', $i++;
11 use MooseX::Singleton;
12 has number => (is => 'rw', isa => 'Num', required => 1);
13 has string => (is => 'rw', isa => 'Str', default => 'Hello!');
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};
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};
27 for my $pkg (new_singleton_pkg) {
28 my $mst = $pkg->new( number => 5 );
31 is( $mst->number, 5, "the instance has the given attribute value" );
36 "the class method, called directly, returns the given attribute value"
39 throws_ok { $pkg->new( number => 3 ) }
41 "can't make new singleton with conflicting attributes";
43 my $second = eval { $pkg->new };
44 ok( !$@, "...but a second ->new without args is okay" );
46 is( $second->number, 5,
47 "...we get the originally inited number from it" );
49 throws_ok { $pkg->initialize }
51 "...but ->initialize() is still an error";
57 use MooseX::Singleton;
59 has foo => ( is => 'ro' );
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' );