Renamed file so we don't have two files with the same number
[gitmo/Moose.git] / t / 020_attributes / 006_attribute_required.t
CommitLineData
bbd2fe69 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
bbd2fe69 7use Test::Exception;
8
7ff56534 9
bbd2fe69 10{
11 package Foo;
bbd2fe69 12 use Moose;
1ed0b94f 13
bbd2fe69 14 has 'bar' => (is => 'ro', required => 1);
1ed0b94f 15 has 'baz' => (is => 'rw', default => 100, required => 1);
16 has 'boo' => (is => 'rw', lazy => 1, default => 50, required => 1);
bbd2fe69 17}
18
19{
20 my $foo = Foo->new(bar => 10, baz => 20, boo => 100);
21 isa_ok($foo, 'Foo');
1ed0b94f 22
bbd2fe69 23 is($foo->bar, 10, '... got the right bar');
1ed0b94f 24 is($foo->baz, 20, '... got the right baz');
25 is($foo->boo, 100, '... got the right boo');
bbd2fe69 26}
27
28{
29 my $foo = Foo->new(bar => 10, boo => 5);
30 isa_ok($foo, 'Foo');
1ed0b94f 31
bbd2fe69 32 is($foo->bar, 10, '... got the right bar');
1ed0b94f 33 is($foo->baz, 100, '... got the right baz');
34 is($foo->boo, 5, '... got the right boo');
bbd2fe69 35}
36
37{
38 my $foo = Foo->new(bar => 10);
39 isa_ok($foo, 'Foo');
1ed0b94f 40
bbd2fe69 41 is($foo->bar, 10, '... got the right bar');
1ed0b94f 42 is($foo->baz, 100, '... got the right baz');
43 is($foo->boo, 50, '... got the right boo');
bbd2fe69 44}
45
1ed0b94f 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
55lives_ok {
ab859145 56 Foo->new(bar => 10, baz => undef);
1ed0b94f 57} '... undef is a valid attribute value';
ab859145 58
1ed0b94f 59lives_ok {
ab859145 60 Foo->new(bar => 10, boo => undef);
1ed0b94f 61} '... undef is a valid attribute value';
62
ab859145 63
64throws_ok {
bbd2fe69 65 Foo->new;
66} qr/^Attribute \(bar\) is required/, '... must supply all the required attribute';
67
a28e50e4 68done_testing;