Convert all tests to done_testing.
[gitmo/Moose.git] / t / 020_attributes / 019_attribute_lazy_initializer.t
CommitLineData
7a9d14bd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
7a9d14bd 7use Test::Exception;
8
7ff56534 9
7a9d14bd 10{
11 package Foo;
12 use Moose;
d03bd989 13
7a9d14bd 14 has 'foo' => (
15 reader => 'get_foo',
16 writer => 'set_foo',
17 initializer => sub {
29ed9dd9 18 my ($self, $value, $callback, $attr) = @_;
d03bd989 19
9df136d0 20 ::isa_ok($attr, 'Moose::Meta::Attribute');
21 ::is($attr->name, 'foo', '... got the right name');
d03bd989 22
7a9d14bd 23 $callback->($value * 2);
24 },
25 );
26
27 has 'lazy_foo' => (
9df136d0 28 reader => 'get_lazy_foo',
29 lazy => 1,
30 default => 10,
7a9d14bd 31 initializer => sub {
29ed9dd9 32 my ($self, $value, $callback, $attr) = @_;
d03bd989 33
9df136d0 34 ::isa_ok($attr, 'Moose::Meta::Attribute');
d03bd989 35 ::is($attr->name, 'lazy_foo', '... got the right name');
36
7a9d14bd 37 $callback->($value * 2);
38 },
39 );
d03bd989 40
9df136d0 41 has 'lazy_foo_w_type' => (
42 reader => 'get_lazy_foo_w_type',
43 isa => 'Int',
44 lazy => 1,
45 default => 20,
46 initializer => sub {
47 my ($self, $value, $callback, $attr) = @_;
d03bd989 48
9df136d0 49 ::isa_ok($attr, 'Moose::Meta::Attribute');
d03bd989 50 ::is($attr->name, 'lazy_foo_w_type', '... got the right name');
51
9df136d0 52 $callback->($value * 2);
53 },
d03bd989 54 );
55
9df136d0 56 has 'lazy_foo_builder' => (
57 reader => 'get_lazy_foo_builder',
58 builder => 'get_foo_builder',
59 initializer => sub {
60 my ($self, $value, $callback, $attr) = @_;
d03bd989 61
9df136d0 62 ::isa_ok($attr, 'Moose::Meta::Attribute');
d03bd989 63 ::is($attr->name, 'lazy_foo_builder', '... got the right name');
64
9df136d0 65 $callback->($value * 2);
66 },
67 );
d03bd989 68
9df136d0 69 has 'lazy_foo_builder_w_type' => (
70 reader => 'get_lazy_foo_builder_w_type',
d03bd989 71 isa => 'Int',
9df136d0 72 builder => 'get_foo_builder_w_type',
73 initializer => sub {
74 my ($self, $value, $callback, $attr) = @_;
d03bd989 75
9df136d0 76 ::isa_ok($attr, 'Moose::Meta::Attribute');
d03bd989 77 ::is($attr->name, 'lazy_foo_builder_w_type', '... got the right name');
78
9df136d0 79 $callback->($value * 2);
80 },
81 );
d03bd989 82
83 sub get_foo_builder { 100 }
84 sub get_foo_builder_w_type { 1000 }
7a9d14bd 85}
86
87{
88 my $foo = Foo->new(foo => 10);
89 isa_ok($foo, 'Foo');
90
9df136d0 91 is($foo->get_foo, 20, 'initial value set to 2x given value');
92 is($foo->get_lazy_foo, 20, 'initial lazy value set to 2x given value');
93 is($foo->get_lazy_foo_w_type, 40, 'initial lazy value with type set to 2x given value');
94 is($foo->get_lazy_foo_builder, 200, 'initial lazy value with builder set to 2x given value');
d03bd989 95 is($foo->get_lazy_foo_builder_w_type, 2000, 'initial lazy value with builder and type set to 2x given value');
9df136d0 96}
97
98{
99 package Bar;
100 use Moose;
d03bd989 101
9df136d0 102 has 'foo' => (
103 reader => 'get_foo',
104 writer => 'set_foo',
105 initializer => sub {
106 my ($self, $value, $callback, $attr) = @_;
d03bd989 107
9df136d0 108 ::isa_ok($attr, 'Moose::Meta::Attribute');
109 ::is($attr->name, 'foo', '... got the right name');
d03bd989 110
9df136d0 111 $callback->($value * 2);
112 },
d03bd989 113 );
114
5a3217de 115 __PACKAGE__->meta->make_immutable;
7a9d14bd 116}
117
9df136d0 118{
119 my $bar = Bar->new(foo => 10);
120 isa_ok($bar, 'Bar');
121
d03bd989 122 is($bar->get_foo, 20, 'initial value set to 2x given value');
9df136d0 123}
124
8abe9636 125{
126 package Fail::Bar;
127 use Moose;
d03bd989 128
8abe9636 129 has 'foo' => (
130 reader => 'get_foo',
131 writer => 'set_foo',
132 isa => 'Int',
133 initializer => sub {
134 my ($self, $value, $callback, $attr) = @_;
d03bd989 135
8abe9636 136 ::isa_ok($attr, 'Moose::Meta::Attribute');
137 ::is($attr->name, 'foo', '... got the right name');
d03bd989 138
8abe9636 139 $callback->("Hello $value World");
140 },
d03bd989 141 );
142
5a3217de 143 __PACKAGE__->meta->make_immutable;
8abe9636 144}
145
146dies_ok {
147 Fail::Bar->new(foo => 10)
148} '... this fails, because initializer returns a bad type';
149
a28e50e4 150done_testing;