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