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