getting close to a 0.07 release
[gitmo/Class-MOP.git] / t / 104_AttributesWithHistory_test.t
CommitLineData
343203ee 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 19;
7use File::Spec;
8
9BEGIN {
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
35my $foo = Foo->new();
36isa_ok($foo, 'Foo');
37
38can_ok($foo, 'foo');
39can_ok($foo, 'get_foo_history');
40can_ok($foo, 'set_bar');
41can_ok($foo, 'get_bar');
42can_ok($foo, 'get_bar_history');
43
44is($foo->foo, undef, '... foo is not yet defined');
45is_deeply(
46 [ $foo->get_foo_history() ],
47 [ ],
48 '... got correct empty history for foo');
49
50$foo->foo(42);
51is($foo->foo, 42, '... foo == 42');
52is_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
62is_deeply(
63 [ $foo->get_foo_history() ],
64 [ 42, 43, 44, 45, 46 ],
65 '... got correct history for foo');
66
67is($foo->get_bar, undef, '... bar is not yet defined');
68is_deeply(
69 [ $foo->get_bar_history() ],
70 [ ],
71 '... got correct empty history for foo');
72
73
74$foo->set_bar("FOO");
75is($foo->get_bar, "FOO", '... bar == "FOO"');
76is_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
84is_deeply(
85 [ $foo->get_bar_history() ],
86 [ qw/FOO BAR BAZ/ ],
87 '... got correct history for bar');
88
89is_deeply(
90 [ $foo->get_foo_history() ],
91 [ 42, 43, 44, 45, 46 ],
92 '... still have the correct history for foo');