File: 022-init-arg.t

package info (click to toggle)
libmouse-perl 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,156 kB
  • sloc: perl: 14,569; ansic: 218; makefile: 8
file content (46 lines) | stat: -rw-r--r-- 1,318 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 20;

do {
    package Class;
    use Mouse;

    has name => (
        is       => 'rw',
        isa      => 'Str',
        init_arg => 'key',
        default  => 'default',
    );

    has no_init_arg => (
        is       => 'rw',
        isa      => 'Str',
        init_arg => undef,
        default  => 'default',
    );

};

for('mutable', 'immutable'){
    my $object = Class->new;
    is($object->name, 'default', "accessor uses attribute name ($_)");
    is($object->{key}, undef, 'nothing in object->{init_arg}!');
    is($object->{name}, 'default', 'value is in object->{name}');

    my $object2 = Class->new(name => 'name', key => 'key');
    is($object2->name, 'key', 'attribute value is from name');
    is($object2->{key}, undef, 'no value for the init_arg');
    is($object2->{name}, 'key', 'value is in key from name');

    my $attr = $object2->meta->get_attribute('name');
    ok($attr, 'got the attribute object by name (not init_arg)');
    is($attr->name, 'name', 'name is name');
    is($attr->init_arg, 'key', 'init_arg is key');

    my $object3 = Class->new(no_init_arg => 'joe');
    is($object3->no_init_arg, 'default', 'init_arg => undef ignores attribute name in the constructor');

    Class->meta->make_immutable;
}