Don't use $_ as loop variable when calling arbitrary code (RT#81072)
[gitmo/Moo.git] / t / init-arg.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4
5 {
6   package Foo;
7
8   use Moo;
9
10   has optional => (
11     is => 'rw',
12     init_arg => 'might_have',
13     isa => sub { die "isa" if $_[0] % 2 },
14     default => sub { 7 },
15   );
16
17   has lazy => (
18     is => 'rw',
19     init_arg => 'workshy',
20     isa => sub { die "aieee" if $_[0] % 2 },
21     default => sub { 7 },
22     lazy => 1,
23   );
24 }
25
26 like(
27   exception { Foo->new },
28   qr/\Aisa check for "optional" \(constructor argument: "might_have"\) failed:/,
29   "isa default"
30 );
31
32 like(
33   exception { Foo->new(might_have => 3) },
34   qr/\Aisa check for "optional" \(constructor argument: "might_have"\) failed:/,
35   "isa init_arg",
36 );
37
38 is(
39   exception { Foo->new(might_have => 2) },
40   undef, "isa init_arg ok"
41 );
42
43 my $foo = Foo->new(might_have => 2);
44
45 like(
46   exception { $foo->optional(3) },
47   qr/\Aisa check for "optional" failed:/,
48   "isa accessor",
49 );
50
51 like(
52   exception { $foo->lazy },
53   qr/\Aisa check for "lazy" failed:/,
54   "lazy accessor",
55 );
56
57 like(
58   exception { $foo->lazy(3) },
59   qr/\Aisa check for "lazy" failed:/,
60   "lazy set isa fail",
61 );
62
63 is(
64   exception { $foo->lazy(4) },
65   undef,
66   "lazy set isa ok",
67 );
68
69 like(
70   exception { Foo->new(might_have => 2, workshy => 3) },
71   qr/\Aisa check for "lazy" \(constructor argument: "workshy"\) failed:/,
72   "lazy init_arg",
73 );
74
75 done_testing;