Fix a test bug where a value was being assigned to a reader and then checked for...
[gitmo/Mouse.git] / t / 016-trigger.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 10;
5use Test::Exception;
6
7my @trigger;
8
9do {
10 package Class;
11 use Mouse;
12
13 has attr => (
14 is => 'rw',
15 default => 10,
16 trigger => sub {
17 my ($self, $value, $attr) = @_;
18 push @trigger, [$self, $value, $attr];
19 },
20 );
21
22 ::throws_ok {
23 has error => (
24 is => 'ro',
25 trigger => sub { },
26 );
27 } qr/Trigger is not allowed on read-only attribute 'error'/;
28};
29
30can_ok(Class => 'attr');
31
32my $object = Class->new;
33is(@trigger, 0, "trigger not called yet");
34
35is($object->attr, 10, "default value");
36is(@trigger, 0, "trigger not called on read");
37
38is($object->attr(50), 50, "setting the value");
39is(@trigger, 1, "trigger was called on read");
40is_deeply(shift(@trigger), [$object, 50, $object->meta->get_attribute('attr')], "correct arguments to trigger in the accessor");
41
42my $object2 = Class->new(attr => 100);
43is(@trigger, 1, "trigger was called on new with the attribute specified");
44is_deeply(shift(@trigger), [$object2, 100, $object2->meta->get_attribute('attr')], "correct arguments to trigger in the constructor");
45