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