Fix clone_and_inherit_options() to take 'traits' and 'metaclass' correctly
[gitmo/Mouse.git] / t / 007-attributes.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
a2096df6 4use Test::More tests => 15;
eab81545 5use Test::Exception;
c3398f5b 6
7do {
8 package Class;
9 use Mouse;
10
74be9f76 11 has 'x' => (
12 is => 'bare',
13 );
c3398f5b 14
15 has 'y' => (
16 is => 'ro',
17 );
18
19 has 'z' => (
20 is => 'rw',
21 );
a2096df6 22
23 has 'attr' => (
24 accessor => 'rw_attr',
25 reader => 'read_attr',
26 writer => 'write_attr',
27 );
c3398f5b 28};
29
30ok(!Class->can('x'), "No accessor is injected if 'is' has no value");
31can_ok('Class', 'y', 'z');
32
33my $object = Class->new;
34
35ok(!$object->can('x'), "No accessor is injected if 'is' has no value");
36can_ok($object, 'y', 'z');
37
38is($object->y, undef);
636c002e 39
40throws_ok {
41 $object->y(10);
42} qr/Cannot assign a value to a read-only accessor/;
43
c3398f5b 44is($object->y, undef);
45
46is($object->z, undef);
47is($object->z(10), 10);
48is($object->z, 10);
49
a2096df6 50can_ok($object, qw(rw_attr read_attr write_attr));
51$object->write_attr(42);
52is $object->rw_attr, 42;
53is $object->read_attr, 42;
54$object->rw_attr(100);
55is $object->rw_attr, 100;
56is $object->read_attr, 100;
57