Document how to pass new attribute values at instance-role application time
[gitmo/Moose.git] / t / attributes / attribute_required.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Fatal;
8
9
10 {
11     package Foo;
12     use Moose;
13
14     has 'bar' => (is => 'ro', required => 1);
15     has 'baz' => (is => 'rw',  default => 100, required => 1);
16     has 'boo' => (is => 'rw', lazy => 1, default => 50, required => 1);
17 }
18
19 {
20     my $foo = Foo->new(bar => 10, baz => 20, boo => 100);
21     isa_ok($foo, 'Foo');
22
23     is($foo->bar, 10, '... got the right bar');
24     is($foo->baz, 20, '... got the right baz');
25     is($foo->boo, 100, '... got the right boo');
26 }
27
28 {
29     my $foo = Foo->new(bar => 10, boo => 5);
30     isa_ok($foo, 'Foo');
31
32     is($foo->bar, 10, '... got the right bar');
33     is($foo->baz, 100, '... got the right baz');
34     is($foo->boo, 5, '... got the right boo');
35 }
36
37 {
38     my $foo = Foo->new(bar => 10);
39     isa_ok($foo, 'Foo');
40
41     is($foo->bar, 10, '... got the right bar');
42     is($foo->baz, 100, '... got the right baz');
43     is($foo->boo, 50, '... got the right boo');
44 }
45
46 #Yeah.. this doesn't work like this anymore, see below. (groditi)
47 #throws_ok {
48 #    Foo->new(bar => 10, baz => undef);
49 #} qr/^Attribute \(baz\) is required and cannot be undef/, '... must supply all the required attribute';
50
51 #throws_ok {
52 #    Foo->new(bar => 10, boo => undef);
53 #} qr/^Attribute \(boo\) is required and cannot be undef/, '... must supply all the required attribute';
54
55 is( exception {
56     Foo->new(bar => 10, baz => undef);
57 }, undef, '... undef is a valid attribute value' );
58
59 is( exception {
60     Foo->new(bar => 10, boo => undef);
61 }, undef, '... undef is a valid attribute value' );
62
63
64 like( exception {
65     Foo->new;
66 }, qr/^Attribute \(bar\) is required/, '... must supply all the required attribute' );
67
68 done_testing;