Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 104_AttributesWithHistory_test.t
CommitLineData
343203ee 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
343203ee 5use File::Spec;
6
86a4d873 7use Class::MOP;
8
9BEGIN {
10dd437b 10 require_ok(File::Spec->catfile('examples', 'AttributesWithHistory.pod'));
343203ee 11}
12
13{
14 package Foo;
aa448b16 15 use metaclass;
86a4d873 16
343203ee 17 Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
18 accessor => 'foo',
19 history_accessor => 'get_foo_history',
86a4d873 20 )));
21
343203ee 22 Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
23 reader => 'get_bar',
24 writer => 'set_bar',
25 history_accessor => 'get_bar_history',
86a4d873 26 )));
27
343203ee 28 sub new {
29 my $class = shift;
5659d76e 30 $class->meta->new_object(@_);
86a4d873 31 }
343203ee 32}
33
34my $foo = Foo->new();
35isa_ok($foo, 'Foo');
36
37can_ok($foo, 'foo');
38can_ok($foo, 'get_foo_history');
39can_ok($foo, 'set_bar');
40can_ok($foo, 'get_bar');
41can_ok($foo, 'get_bar_history');
42
0b8eb325 43my $foo2 = Foo->new();
44isa_ok($foo2, 'Foo');
45
343203ee 46is($foo->foo, undef, '... foo is not yet defined');
47is_deeply(
48 [ $foo->get_foo_history() ],
49 [ ],
50 '... got correct empty history for foo');
86a4d873 51
0b8eb325 52is($foo2->foo, undef, '... foo2 is not yet defined');
53is_deeply(
54 [ $foo2->get_foo_history() ],
55 [ ],
86a4d873 56 '... got correct empty history for foo2');
343203ee 57
58$foo->foo(42);
59is($foo->foo, 42, '... foo == 42');
60is_deeply(
61 [ $foo->get_foo_history() ],
62 [ 42 ],
63 '... got correct history for foo');
64
0b8eb325 65is($foo2->foo, undef, '... foo2 is still not yet defined');
66is_deeply(
67 [ $foo2->get_foo_history() ],
68 [ ],
69 '... still got correct empty history for foo2');
86a4d873 70
0b8eb325 71$foo2->foo(100);
72is($foo->foo, 42, '... foo is still == 42');
73is_deeply(
74 [ $foo->get_foo_history() ],
75 [ 42 ],
76 '... still got correct history for foo');
77
78is($foo2->foo, 100, '... foo2 == 100');
79is_deeply(
80 [ $foo2->get_foo_history() ],
81 [ 100 ],
82 '... got correct empty history for foo2');
83
343203ee 84$foo->foo(43);
85$foo->foo(44);
86$foo->foo(45);
87$foo->foo(46);
88
89is_deeply(
90 [ $foo->get_foo_history() ],
91 [ 42, 43, 44, 45, 46 ],
86a4d873 92 '... got correct history for foo');
343203ee 93
94is($foo->get_bar, undef, '... bar is not yet defined');
95is_deeply(
96 [ $foo->get_bar_history() ],
97 [ ],
98 '... got correct empty history for foo');
99
100
101$foo->set_bar("FOO");
102is($foo->get_bar, "FOO", '... bar == "FOO"');
103is_deeply(
104 [ $foo->get_bar_history() ],
105 [ "FOO" ],
106 '... got correct history for foo');
107
108$foo->set_bar("BAR");
109$foo->set_bar("BAZ");
110
111is_deeply(
112 [ $foo->get_bar_history() ],
113 [ qw/FOO BAR BAZ/ ],
114 '... got correct history for bar');
115
116is_deeply(
117 [ $foo->get_foo_history() ],
118 [ 42, 43, 44, 45, 46 ],
119 '... still have the correct history for foo');
86a4d873 120
121done_testing;