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