File: 11_children.t

package info (click to toggle)
libclass-accessor-children-perl 0.02-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 108 kB
  • sloc: perl: 64; sh: 22; makefile: 2
file content (91 lines) | stat: -rw-r--r-- 2,688 bytes parent folder | download | duplicates (4)
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
# ----------------------------------------------------------------
    use strict;
    use Test::More tests => 29;
    use_ok qw( Class::Accessor::Children );
# ----------------------------------------------------------------
{
    package Foo;
    use base qw( Class::Accessor::Children );
    __PACKAGE__->mk_child_accessors(
        Odd     =>  [qw( one   seven )],
        Even    =>  [qw( two   eight )],
    );
    __PACKAGE__->mk_child_ro_accessors(
        Odd     =>  [qw( three nine )],
        Even    =>  [qw( four  ten  )],
    );
    __PACKAGE__->mk_child_wo_accessors(
        Odd     =>  [qw( five  eleven )],
        Even    =>  [qw( six   twelve )],
    );
}
# ----------------------------------------------------------------
{
    my $odd  = Foo::Odd->new( {one=>1,three=>3,five=>5} );
    ok( ref $odd, 'odd - new' );
    ok( $odd->isa( 'Class::Accessor' ), 'odd - isa' );
    ok( ! $odd->can( 'zero' ), 'odd - zero' );

    my $even = Foo::Even->new( {two=>2,four=>4,six=>6} );
    ok( ref $even, 'even - new' );
    ok( $even->isa( 'Class::Accessor' ), 'even - isa' );
    ok( ! $even->can( 'zero' ), 'even - zero' );

    # read

    is( $odd->one,    1,      'read one' );
    is( $even->two,   2,      'read two' );
    is( $odd->three,  3,      'read three' );
    is( $even->four,  4,      'read four' );
    {
        local $@;
        eval { $odd->five; };
        ok( $@, 'read five [write-only]' );
    }
    {
        local $@;
        eval { $even->six; };
        ok( $@, 'read six [write-only]' );
    }
    is( $odd->seven,  undef,  'read three' );
    is( $even->eight, undef,  'read eight' );
    is( $odd->nine,   undef,  'read nine' );
    is( $even->ten,   undef,  'read ten' );

    # write

    ok( $odd->seven( 7 ),    'write seven' );
    ok( $even->eight( 8 ),   'write eight' );
    {
        local $@;
        eval { $odd->nine( 9 ); };
        ok( $@, 'write nine [read-only]' );
    }
    {
        local $@;
        eval { $odd->ten( 10 ); };
        ok( $@, 'write ten [read-only]' );
    }
    ok( $odd->eleven( 11 ),  'write eleven' );
    ok( $even->twelve( 12 ), 'write twelve' );

    # read

    is( $odd->seven,  7,      'read three' );
    is( $even->eight, 8,      'read eight' );
    is( $odd->nine,   undef,  'read nine' );
    is( $even->ten,   undef,  'read ten' );
    {
        local $@;
        eval { $odd->eleven; };
        ok( $@, 'read eleven [write-only]' );
    }
    {
        local $@;
        eval { $even->twelve; };
        ok( $@, 'read twelve [write-only]' );
    }
}
# ----------------------------------------------------------------
;1;
# ----------------------------------------------------------------