File: 002_attribute_writer_generation.t

package info (click to toggle)
libmoose-perl 1.09-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,004 kB
  • ctags: 1,472
  • sloc: perl: 25,387; makefile: 2
file content (119 lines) | stat: -rw-r--r-- 2,938 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
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Exception;

use Scalar::Util 'isweak';


{
    package Foo;
    use Moose;

    eval {
        has 'foo' => (
            reader => 'get_foo',
            writer => 'set_foo',
        );
    };
    ::ok(!$@, '... created the writer method okay');

    eval {
        has 'foo_required' => (
            reader   => 'get_foo_required',
            writer   => 'set_foo_required',
            required => 1,
        );
    };
    ::ok(!$@, '... created the required writer method okay');

    eval {
        has 'foo_int' => (
            reader => 'get_foo_int',
            writer => 'set_foo_int',
            isa    => 'Int',
        );
    };
    ::ok(!$@, '... created the writer method with type constraint okay');

    eval {
        has 'foo_weak' => (
            reader   => 'get_foo_weak',
            writer   => 'set_foo_weak',
            weak_ref => 1
        );
    };
    ::ok(!$@, '... created the writer method with weak_ref okay');
}

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

    # regular writer

    can_ok($foo, 'set_foo');
    is($foo->get_foo(), undef, '... got an unset value');
    lives_ok {
        $foo->set_foo(100);
    } '... set_foo wrote successfully';
    is($foo->get_foo(), 100, '... got the correct set value');

    ok(!isweak($foo->{foo}), '... it is not a weak reference');

    # required writer

    dies_ok {
        Foo->new;
    } '... cannot create without the required attribute';

    can_ok($foo, 'set_foo_required');
    is($foo->get_foo_required(), 'required', '... got an unset value');
    lives_ok {
        $foo->set_foo_required(100);
    } '... set_foo_required wrote successfully';
    is($foo->get_foo_required(), 100, '... got the correct set value');

    dies_ok {
        $foo->set_foo_required();
    } '... set_foo_required died successfully with no value';

    lives_ok {
        $foo->set_foo_required(undef);
    } '... set_foo_required did accept undef';

    ok(!isweak($foo->{foo_required}), '... it is not a weak reference');

    # with type constraint

    can_ok($foo, 'set_foo_int');
    is($foo->get_foo_int(), undef, '... got an unset value');
    lives_ok {
        $foo->set_foo_int(100);
    } '... set_foo_int wrote successfully';
    is($foo->get_foo_int(), 100, '... got the correct set value');

    dies_ok {
        $foo->set_foo_int("Foo");
    } '... set_foo_int died successfully';

    ok(!isweak($foo->{foo_int}), '... it is not a weak reference');

    # with weak_ref

    my $test = [];

    can_ok($foo, 'set_foo_weak');
    is($foo->get_foo_weak(), undef, '... got an unset value');
    lives_ok {
        $foo->set_foo_weak($test);
    } '... set_foo_weak wrote successfully';
    is($foo->get_foo_weak(), $test, '... got the correct set value');

    ok(isweak($foo->{foo_weak}), '... it is a weak reference');
}

done_testing;