Moose::Cookbook::Recipe5:
[gitmo/Moose.git] / t / 035_attribute_required.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 14;
7 use Test::Exception;
8
9 BEGIN {
10     use_ok('Moose');           
11 }
12
13 {
14     package Foo;
15     use Moose;
16     
17     has 'bar' => (is => 'ro', required => 1);
18     has 'baz' => (is => 'rw', default => 100, required => 1); 
19
20     # NOTE:
21     # this attribute is actually kind of silly  
22     # since lazy requires default, then the 
23     # required attribute becomes void in this 
24     # case. But hey, best to test it :)
25     has 'boo' => (is => 'rw', lazy => 1, default => 50, required => 1);       
26 }
27
28 {
29     my $foo = Foo->new(bar => 10, baz => 20, boo => 100);
30     isa_ok($foo, 'Foo');
31     
32     is($foo->bar, 10, '... got the right bar');
33     is($foo->baz, 20, '... got the right baz');    
34     is($foo->boo, 100, '... got the right boo');        
35 }
36
37 {
38     my $foo = Foo->new(bar => 10, boo => 5);
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, 5, '... got the right boo');            
44 }
45
46 {
47     my $foo = Foo->new(bar => 10);
48     isa_ok($foo, 'Foo');
49     
50     is($foo->bar, 10, '... got the right bar');
51     is($foo->baz, 100, '... got the right baz');    
52     is($foo->boo, 50, '... got the right boo');            
53 }
54
55 throws_ok {
56     Foo->new;
57 } qr/^Attribute \(bar\) is required/, '... must supply all the required attribute';
58