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