Commit | Line | Data |
343203ee |
1 | use strict; |
2 | use warnings; |
3 | |
efd3d14c |
4 | use Test::More tests => 27; |
343203ee |
5 | use File::Spec; |
6 | |
efd3d14c |
7 | BEGIN {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 | |
32 | my $foo = Foo->new(); |
33 | isa_ok($foo, 'Foo'); |
34 | |
35 | can_ok($foo, 'foo'); |
36 | can_ok($foo, 'get_foo_history'); |
37 | can_ok($foo, 'set_bar'); |
38 | can_ok($foo, 'get_bar'); |
39 | can_ok($foo, 'get_bar_history'); |
40 | |
0b8eb325 |
41 | my $foo2 = Foo->new(); |
42 | isa_ok($foo2, 'Foo'); |
43 | |
343203ee |
44 | is($foo->foo, undef, '... foo is not yet defined'); |
45 | is_deeply( |
46 | [ $foo->get_foo_history() ], |
47 | [ ], |
48 | '... got correct empty history for foo'); |
0b8eb325 |
49 | |
50 | is($foo2->foo, undef, '... foo2 is not yet defined'); |
51 | is_deeply( |
52 | [ $foo2->get_foo_history() ], |
53 | [ ], |
54 | '... got correct empty history for foo2'); |
343203ee |
55 | |
56 | $foo->foo(42); |
57 | is($foo->foo, 42, '... foo == 42'); |
58 | is_deeply( |
59 | [ $foo->get_foo_history() ], |
60 | [ 42 ], |
61 | '... got correct history for foo'); |
62 | |
0b8eb325 |
63 | is($foo2->foo, undef, '... foo2 is still not yet defined'); |
64 | is_deeply( |
65 | [ $foo2->get_foo_history() ], |
66 | [ ], |
67 | '... still got correct empty history for foo2'); |
68 | |
69 | $foo2->foo(100); |
70 | is($foo->foo, 42, '... foo is still == 42'); |
71 | is_deeply( |
72 | [ $foo->get_foo_history() ], |
73 | [ 42 ], |
74 | '... still got correct history for foo'); |
75 | |
76 | is($foo2->foo, 100, '... foo2 == 100'); |
77 | is_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 | |
87 | is_deeply( |
88 | [ $foo->get_foo_history() ], |
89 | [ 42, 43, 44, 45, 46 ], |
90 | '... got correct history for foo'); |
91 | |
92 | is($foo->get_bar, undef, '... bar is not yet defined'); |
93 | is_deeply( |
94 | [ $foo->get_bar_history() ], |
95 | [ ], |
96 | '... got correct empty history for foo'); |
97 | |
98 | |
99 | $foo->set_bar("FOO"); |
100 | is($foo->get_bar, "FOO", '... bar == "FOO"'); |
101 | is_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 | |
109 | is_deeply( |
110 | [ $foo->get_bar_history() ], |
111 | [ qw/FOO BAR BAZ/ ], |
112 | '... got correct history for bar'); |
113 | |
114 | is_deeply( |
115 | [ $foo->get_foo_history() ], |
116 | [ 42, 43, 44, 45, 46 ], |
117 | '... still have the correct history for foo'); |