Commit | Line | Data |
343203ee |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 19; |
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 | |
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'); |
49 | |
50 | $foo->foo(42); |
51 | is($foo->foo, 42, '... foo == 42'); |
52 | is_deeply( |
53 | [ $foo->get_foo_history() ], |
54 | [ 42 ], |
55 | '... got correct history for foo'); |
56 | |
57 | $foo->foo(43); |
58 | $foo->foo(44); |
59 | $foo->foo(45); |
60 | $foo->foo(46); |
61 | |
62 | is_deeply( |
63 | [ $foo->get_foo_history() ], |
64 | [ 42, 43, 44, 45, 46 ], |
65 | '... got correct history for foo'); |
66 | |
67 | is($foo->get_bar, undef, '... bar is not yet defined'); |
68 | is_deeply( |
69 | [ $foo->get_bar_history() ], |
70 | [ ], |
71 | '... got correct empty history for foo'); |
72 | |
73 | |
74 | $foo->set_bar("FOO"); |
75 | is($foo->get_bar, "FOO", '... bar == "FOO"'); |
76 | is_deeply( |
77 | [ $foo->get_bar_history() ], |
78 | [ "FOO" ], |
79 | '... got correct history for foo'); |
80 | |
81 | $foo->set_bar("BAR"); |
82 | $foo->set_bar("BAZ"); |
83 | |
84 | is_deeply( |
85 | [ $foo->get_bar_history() ], |
86 | [ qw/FOO BAR BAZ/ ], |
87 | '... got correct history for bar'); |
88 | |
89 | is_deeply( |
90 | [ $foo->get_foo_history() ], |
91 | [ 42, 43, 44, 45, 46 ], |
92 | '... still have the correct history for foo'); |