File: 104_AttributesWithHistory_test.t

package info (click to toggle)
libclass-mop-perl 0.36-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 552 kB
  • ctags: 209
  • sloc: perl: 6,157; makefile: 46
file content (120 lines) | stat: -rw-r--r-- 2,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 28;
use File::Spec;

BEGIN { 
    use_ok('Class::MOP');    
    require_ok(File::Spec->catdir('examples', 'AttributesWithHistory.pod'));
}

{
    package Foo;
    use metaclass;
    
    Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
        accessor         => 'foo',
        history_accessor => 'get_foo_history',
    )));    
    
    Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
        reader           => 'get_bar',
        writer           => 'set_bar',
        history_accessor => 'get_bar_history',
    )));    
    
    sub new  {
        my $class = shift;
        $class->meta->new_object(@_);
    }   
}

my $foo = Foo->new();
isa_ok($foo, 'Foo');

can_ok($foo, 'foo');
can_ok($foo, 'get_foo_history');
can_ok($foo, 'set_bar');
can_ok($foo, 'get_bar');
can_ok($foo, 'get_bar_history');

my $foo2 = Foo->new();
isa_ok($foo2, 'Foo');

is($foo->foo, undef, '... foo is not yet defined');
is_deeply(
    [ $foo->get_foo_history() ],
    [ ],
    '... got correct empty history for foo');
    
is($foo2->foo, undef, '... foo2 is not yet defined');
is_deeply(
    [ $foo2->get_foo_history() ],
    [ ],
    '... got correct empty history for foo2');    

$foo->foo(42);
is($foo->foo, 42, '... foo == 42');
is_deeply(
    [ $foo->get_foo_history() ],
    [ 42 ],
    '... got correct history for foo');

is($foo2->foo, undef, '... foo2 is still not yet defined');
is_deeply(
    [ $foo2->get_foo_history() ],
    [ ],
    '... still got correct empty history for foo2');
        
$foo2->foo(100);
is($foo->foo, 42, '... foo is still == 42');
is_deeply(
    [ $foo->get_foo_history() ],
    [ 42 ],
    '... still got correct history for foo');

is($foo2->foo, 100, '... foo2 == 100');
is_deeply(
    [ $foo2->get_foo_history() ],
    [ 100 ],
    '... got correct empty history for foo2');

$foo->foo(43);
$foo->foo(44);
$foo->foo(45);
$foo->foo(46);

is_deeply(
    [ $foo->get_foo_history() ],
    [ 42, 43, 44, 45, 46 ],
    '... got correct history for foo');    

is($foo->get_bar, undef, '... bar is not yet defined');
is_deeply(
    [ $foo->get_bar_history() ],
    [ ],
    '... got correct empty history for foo');


$foo->set_bar("FOO");
is($foo->get_bar, "FOO", '... bar == "FOO"');
is_deeply(
    [ $foo->get_bar_history() ],
    [ "FOO" ],
    '... got correct history for foo');

$foo->set_bar("BAR");
$foo->set_bar("BAZ");

is_deeply(
    [ $foo->get_bar_history() ],
    [ qw/FOO BAR BAZ/ ],
    '... got correct history for bar');

is_deeply(
    [ $foo->get_foo_history() ],
    [ 42, 43, 44, 45, 46 ],
    '... still have the correct history for foo');