Add type constraint
[gitmo/Mouse.git] / t / 022-init-arg.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
6d46df7a 4use Test::More tests => 11;
c3398f5b 5
6do {
7 package Class;
8 use Mouse;
9
10 has name => (
11 is => 'rw',
b4160dbe 12 isa => 'Str',
c3398f5b 13 init_arg => 'key',
14 default => 'default',
15 );
16};
17
18my $object = Class->new;
19is($object->name, 'default', 'accessor uses attribute name');
f3c1ccc8 20is($object->{key}, undef, 'nothing in object->{init_arg}!');
21is($object->{name}, 'default', 'value is in object->{name}');
c3398f5b 22
23my $object2 = Class->new(name => 'name', key => 'key');
384072a3 24is($object2->name, 'key', 'attribute value is from name');
f3c1ccc8 25is($object2->{key}, undef, 'no value for the init_arg');
384072a3 26is($object2->{name}, 'key', 'value is in key from name');
c3398f5b 27
28my $attr = $object2->meta->get_attribute('name');
29ok($attr, 'got the attribute object by name (not init_arg)');
30is($attr->name, 'name', 'name is name');
31is($attr->init_arg, 'key', 'init_arg is key');
1f679986 32
33do {
34 package Foo;
35 use Mouse;
36
37 has name => (
38 is => 'rw',
39 init_arg => undef,
40 default => 'default',
41 );
42};
43
44my $foo = Foo->new(name => 'joe');
45is($foo->name, 'default', 'init_arg => undef ignores attribute name in the constructor');
46
6d46df7a 47Foo->meta->make_immutable;
48
49my $bar = Foo->new(name => 'joe');
50is($bar->name, 'default', 'init_arg => undef ignores attribute name in the inlined constructor');