whole bunch of stuff
[gitmo/Class-MOP.git] / t / 104_AttributesWithHistory_test.t
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;
16     
17     use Class::MOP 'meta';
18     
19     Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
20         accessor         => 'foo',
21         history_accessor => 'get_foo_history',
22     )));    
23     
24     Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
25         reader           => 'get_bar',
26         writer           => 'set_bar',
27         history_accessor => 'get_bar_history',
28     )));    
29     
30     sub new  {
31         my $class = shift;
32         $class->meta->new_object(@_);
33     }   
34 }
35
36 my $foo = Foo->new();
37 isa_ok($foo, 'Foo');
38
39 can_ok($foo, 'foo');
40 can_ok($foo, 'get_foo_history');
41 can_ok($foo, 'set_bar');
42 can_ok($foo, 'get_bar');
43 can_ok($foo, 'get_bar_history');
44
45 is($foo->foo, undef, '... foo is not yet defined');
46 is_deeply(
47     [ $foo->get_foo_history() ],
48     [ ],
49     '... got correct empty history for foo');
50
51 $foo->foo(42);
52 is($foo->foo, 42, '... foo == 42');
53 is_deeply(
54     [ $foo->get_foo_history() ],
55     [ 42 ],
56     '... got correct history for foo');
57
58 $foo->foo(43);
59 $foo->foo(44);
60 $foo->foo(45);
61 $foo->foo(46);
62
63 is_deeply(
64     [ $foo->get_foo_history() ],
65     [ 42, 43, 44, 45, 46 ],
66     '... got correct history for foo');    
67
68 is($foo->get_bar, undef, '... bar is not yet defined');
69 is_deeply(
70     [ $foo->get_bar_history() ],
71     [ ],
72     '... got correct empty history for foo');
73
74
75 $foo->set_bar("FOO");
76 is($foo->get_bar, "FOO", '... bar == "FOO"');
77 is_deeply(
78     [ $foo->get_bar_history() ],
79     [ "FOO" ],
80     '... got correct history for foo');
81
82 $foo->set_bar("BAR");
83 $foo->set_bar("BAZ");
84
85 is_deeply(
86     [ $foo->get_bar_history() ],
87     [ qw/FOO BAR BAZ/ ],
88     '... got correct history for bar');
89
90 is_deeply(
91     [ $foo->get_foo_history() ],
92     [ 42, 43, 44, 45, 46 ],
93     '... still have the correct history for foo');