3 use Test::More tests => 8;
6 sub new_singleton_pkg {
7 my $pkg_name = sprintf 'MooseX::Singleton::Test%s', $i++;
10 use MooseX::Singleton;
11 has number => (is => 'rw', isa => 'Num', required => 1);
12 has string => (is => 'rw', isa => 'Str', default => 'Hello!');
18 eval { new_singleton_pkg()->instance; };
21 qr/\QAttribute (number) is required/,
22 q{can't get the Singleton if requires attrs and we don't provide them},
25 eval { new_singleton_pkg()->string; };
28 qr/\QAttribute (number) is required/,
29 q{can't call any Singleton attr reader if Singleton can't be inited},
32 for my $pkg (new_singleton_pkg) {
33 my $mst = $pkg->new(number => 5);
36 is($mst->number, 5, "the instance has the given attribute value");
41 "the class method, called directly, returns the given attribute value"
44 eval { $pkg->new(number => 3) };
45 ok($@, "can't make new singleton with conflicting attributes");
47 my $second = eval { $pkg->new };
48 ok(!$@, "...but a second ->new without args is okay");
50 is($second->number, 5, "...we get the originally inited number from it");