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