More tests for strict constructor
[gitmo/Mouse.git] / t / 001_mouse / 068-strict-constructor.t
CommitLineData
29cb82b7 1#!perl
2use strict;
3use warnings;
4
7fc1f782 5use if 'Mouse' eq 'Moose',
6 'Test::More' => skip_all => 'Moose does nots support strict constructor';
29cb82b7 7use Test::More;
8use Test::Exception;
9
10{
11 package MyClass;
12 use Mouse;
13
14 has foo => (
15 is => 'rw',
16 );
17
18 has bar => (
19 is => 'rw',
20 init_arg => undef,
21 );
22
81c629eb 23 has baz => (
24 is => 'rw',
25 default => 42,
26 );
27
29cb82b7 28 __PACKAGE__->meta->make_immutable(strict_constructor => 1);
29}
30
81c629eb 31lives_and {
32 my $o = MyClass->new(foo => 1);
33 isa_ok($o, 'MyClass');
34 is $o->baz, 42;
35} 'correc use of the constructor';
36
37lives_and {
38 my $o = MyClass->new(foo => 1, baz => 10);
39 isa_ok($o, 'MyClass');
40 is $o->baz, 10;
41} 'correc use of the constructor';
42
29cb82b7 43
44throws_ok {
45 MyClass->new(foo => 1, hoge => 42);
46} qr/\b hoge \b/xms;
47
48throws_ok {
49 MyClass->new(foo => 1, bar => 42);
50} qr/\b bar \b/xms, "init_arg => undef";
51
52
53throws_ok {
54 MyClass->new(aaa => 1, bbb => 2, ccc => 3);
81c629eb 55} qr/\b aaa \b/xms, $@;
29cb82b7 56
57throws_ok {
58 MyClass->new(aaa => 1, bbb => 2, ccc => 3);
81c629eb 59} qr/\b bbb \b/xms, $@;
29cb82b7 60
61throws_ok {
62 MyClass->new(aaa => 1, bbb => 2, ccc => 3);
81c629eb 63} qr/\b ccc \b/xms, $@;
29cb82b7 64
65done_testing;