File: lazy_isa.t

package info (click to toggle)
libmoo-perl 0.091011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 476 kB
  • sloc: perl: 1,688; makefile: 4; sh: 1
file content (67 lines) | stat: -rw-r--r-- 1,417 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use strictures 1;
use Test::More;
use Test::Fatal;

{
  package FooISA;

  use Moo;

  my $isa = sub { die "I want to die" unless $_[0] eq 'live' };

  has a_lazy_attr => (
    is => 'ro',
    isa => $isa,
    lazy => 1,
    builder => '_build_attr',
  );

  has non_lazy => (
    is => 'ro',
    isa => $isa,
    builder => '_build_attr',
  );

  sub _build_attr { 'die' }
}

ok my $lives = FooISA->new(a_lazy_attr=>'live', non_lazy=>'live'),
  'expect to live when both attrs are set to live in init';

like(
  exception { FooISA->new(a_lazy_attr=>'live', non_lazy=>'die') },
  qr/I want to die/,
  'expect to die when non lazy is set to die in init',
);

like(
  exception { FooISA->new(a_lazy_attr=>'die', non_lazy=>'die') },
  qr/I want to die/,
  'expect to die when non lazy and lazy is set to die in init',
);

like(
  exception { FooISA->new(a_lazy_attr=>'die', non_lazy=>'live') },
  qr/I want to die/,
  'expect to die when lazy is set to die in init',
);

like(
  exception { FooISA->new() },
  qr/I want to die/,
  'expect to die when both lazy and non lazy are allowed to default',
);

like(
  exception { FooISA->new(a_lazy_attr=>'live') },
  qr/I want to die/,
  'expect to die when lazy is set to live but non lazy is allowed to default',
);

is(
  exception { FooISA->new(non_lazy=>'live') },
  undef,
  'ok when non lazy is set to something valid but lazy is allowed to default',
);

done_testing;