test tweaks
[gitmo/Class-MOP.git] / t / 104_AttributesWithHistory_test.t
CommitLineData
343203ee 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
0b8eb325 6use Test::More tests => 28;
343203ee 7use File::Spec;
8
9BEGIN {
10 use_ok('Class::MOP');
10dd437b 11 require_ok(File::Spec->catfile('examples', 'AttributesWithHistory.pod'));
343203ee 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
0b8eb325 44my $foo2 = Foo->new();
45isa_ok($foo2, 'Foo');
46
343203ee 47is($foo->foo, undef, '... foo is not yet defined');
48is_deeply(
49 [ $foo->get_foo_history() ],
50 [ ],
51 '... got correct empty history for foo');
0b8eb325 52
53is($foo2->foo, undef, '... foo2 is not yet defined');
54is_deeply(
55 [ $foo2->get_foo_history() ],
56 [ ],
57 '... got correct empty history for foo2');
343203ee 58
59$foo->foo(42);
60is($foo->foo, 42, '... foo == 42');
61is_deeply(
62 [ $foo->get_foo_history() ],
63 [ 42 ],
64 '... got correct history for foo');
65
0b8eb325 66is($foo2->foo, undef, '... foo2 is still not yet defined');
67is_deeply(
68 [ $foo2->get_foo_history() ],
69 [ ],
70 '... still got correct empty history for foo2');
71
72$foo2->foo(100);
73is($foo->foo, 42, '... foo is still == 42');
74is_deeply(
75 [ $foo->get_foo_history() ],
76 [ 42 ],
77 '... still got correct history for foo');
78
79is($foo2->foo, 100, '... foo2 == 100');
80is_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
90is_deeply(
91 [ $foo->get_foo_history() ],
92 [ 42, 43, 44, 45, 46 ],
93 '... got correct history for foo');
94
95is($foo->get_bar, undef, '... bar is not yet defined');
96is_deeply(
97 [ $foo->get_bar_history() ],
98 [ ],
99 '... got correct empty history for foo');
100
101
102$foo->set_bar("FOO");
103is($foo->get_bar, "FOO", '... bar == "FOO"');
104is_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
112is_deeply(
113 [ $foo->get_bar_history() ],
114 [ qw/FOO BAR BAZ/ ],
115 '... got correct history for bar');
116
117is_deeply(
118 [ $foo->get_foo_history() ],
119 [ 42, 43, 44, 45, 46 ],
120 '... still have the correct history for foo');