adding in the metaclass pragma
[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;
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;
d6fbcd05 32 bless $class->meta->construct_instance(@_) => $class;
343203ee 33 }
34}
35
36my $foo = Foo->new();
37isa_ok($foo, 'Foo');
38
39can_ok($foo, 'foo');
40can_ok($foo, 'get_foo_history');
41can_ok($foo, 'set_bar');
42can_ok($foo, 'get_bar');
43can_ok($foo, 'get_bar_history');
44
45is($foo->foo, undef, '... foo is not yet defined');
46is_deeply(
47 [ $foo->get_foo_history() ],
48 [ ],
49 '... got correct empty history for foo');
50
51$foo->foo(42);
52is($foo->foo, 42, '... foo == 42');
53is_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
63is_deeply(
64 [ $foo->get_foo_history() ],
65 [ 42, 43, 44, 45, 46 ],
66 '... got correct history for foo');
67
68is($foo->get_bar, undef, '... bar is not yet defined');
69is_deeply(
70 [ $foo->get_bar_history() ],
71 [ ],
72 '... got correct empty history for foo');
73
74
75$foo->set_bar("FOO");
76is($foo->get_bar, "FOO", '... bar == "FOO"');
77is_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
85is_deeply(
86 [ $foo->get_bar_history() ],
87 [ qw/FOO BAR BAZ/ ],
88 '... got correct history for bar');
89
90is_deeply(
91 [ $foo->get_foo_history() ],
92 [ 42, 43, 44, 45, 46 ],
93 '... still have the correct history for foo');