File: 012_misc_attribute_tests.t

package info (click to toggle)
libmouse-perl 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,156 kB
  • sloc: perl: 14,569; ansic: 218; makefile: 8
file content (279 lines) | stat: -rw-r--r-- 7,142 bytes parent folder | download | duplicates (8)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 43;
use Test::Exception;

use lib 't/lib';
use Test::Mouse;

{
    {
        package Test::Attribute::Inline::Documentation;
        use Mouse;

        has 'foo' => (
            documentation => q{
                The 'foo' attribute is my favorite
                attribute in the whole wide world.
            },
            is => 'bare',
        );
    }

    my $foo_attr = Test::Attribute::Inline::Documentation->meta->get_attribute('foo');

    ok($foo_attr->has_documentation, '... the foo has docs');
    is($foo_attr->documentation,
            q{
                The 'foo' attribute is my favorite
                attribute in the whole wide world.
            },
    '... got the foo docs');
}

{
    {
        package Test::For::Lazy::TypeConstraint;
        use Mouse;
        use Mouse::Util::TypeConstraints;

        has 'bad_lazy_attr' => (
            is => 'rw',
            isa => 'ArrayRef',
            lazy => 1,
            default => sub { "test" },
        );

        has 'good_lazy_attr' => (
            is => 'rw',
            isa => 'ArrayRef',
            lazy => 1,
            default => sub { [] },
        );

    }

    my $test = Test::For::Lazy::TypeConstraint->new;
    isa_ok($test, 'Test::For::Lazy::TypeConstraint');

    dies_ok {
        $test->bad_lazy_attr;
    } '... this does not work';

    lives_ok {
        $test->good_lazy_attr;
    } '... this does work';
}

{
    {
        package Test::Arrayref::Attributes;
        use Mouse;

        has [qw(foo bar baz)] => (
            is => 'rw',
        );

    }

    my $test = Test::Arrayref::Attributes->new;
    isa_ok($test, 'Test::Arrayref::Attributes');
    can_ok($test, qw(foo bar baz));

}

{
    {
        package Test::Arrayref::RoleAttributes::Role;
        use Mouse::Role;

        has [qw(foo bar baz)] => (
            is => 'rw',
        );

    }
    {
        package Test::Arrayref::RoleAttributes;
        use Mouse;
        with 'Test::Arrayref::RoleAttributes::Role';
    }

    my $test = Test::Arrayref::RoleAttributes->new;
    isa_ok($test, 'Test::Arrayref::RoleAttributes');
    can_ok($test, qw(foo bar baz));

}

{
    {
        package Test::UndefDefault::Attributes;
        use Mouse;

        has 'foo' => (
            is      => 'ro',
            isa     => 'Str',
            default => sub { return }
        );

    }

    dies_ok {
        Test::UndefDefault::Attributes->new;
    } '... default must return a value which passes the type constraint';

}

{
    {
        package OverloadedStr;
        use Mouse;
        use overload '""' => sub { 'this is *not* a string' };

        has 'a_str' => ( isa => 'Str' , is => 'rw' );
    }

    my $moose_obj = OverloadedStr->new;

    is($moose_obj->a_str( 'foobar' ), 'foobar', 'setter took string');
    ok($moose_obj, 'this is a *not* a string');

    throws_ok {
        $moose_obj->a_str( $moose_obj )
    } qr/Attribute \(a_str\) does not pass the type constraint because\: Validation failed for 'Str' with value OverloadedStr=HASH\(0x.+?\)/,
    '... dies without overloading the string';

}

{
    {
        package OverloadBreaker;
        use Mouse;

        has 'a_num' => ( isa => 'Int' , is => 'rw', default => 7.5 );
    }

    throws_ok {
        OverloadBreaker->new;
    } qr/Attribute \(a_num\) does not pass the type constraint because\: Validation failed for 'Int' with value 7\.5/,
    '... this doesnt trip overload to break anymore ';

    lives_ok {
        OverloadBreaker->new(a_num => 5);
    } '... this works fine though';

}

{
    {
      package Test::Builder::Attribute;
        use Mouse;

        has 'foo'  => ( required => 1, builder => 'build_foo', is => 'ro');
        sub build_foo { return "works" };
    }

    my $meta = Test::Builder::Attribute->meta;
    my $foo_attr  = $meta->get_attribute("foo");

    ok($foo_attr->is_required, "foo is required");
    ok($foo_attr->has_builder, "foo has builder");
    is($foo_attr->builder, "build_foo",  ".. and it's named build_foo");

    my $instance = Test::Builder::Attribute->new;
    is($instance->foo, 'works', "foo builder works");
}

{
    {
        package Test::Builder::Attribute::Broken;
        use Mouse;

        has 'foo'  => ( required => 1, builder => 'build_foo', is => 'ro');
    }

    dies_ok {
        Test::Builder::Attribute::Broken->new;
    } '... no builder, wtf';
}


{
    {
      package Test::LazyBuild::Attribute;
        use Mouse;

        has 'foo'  => ( lazy_build => 1, is => 'ro');
        has '_foo' => ( lazy_build => 1, is => 'ro');
        has 'fool' => ( lazy_build => 1, is => 'ro');
        sub _build_foo { return "works" };
        sub _build__foo { return "works too" };
    }

    my $meta = Test::LazyBuild::Attribute->meta;
    my $foo_attr  = $meta->get_attribute("foo");
    my $_foo_attr = $meta->get_attribute("_foo");

    ok($foo_attr->is_lazy, "foo is lazy");
    ok($foo_attr->is_lazy_build, "foo is lazy_build");

    ok($foo_attr->has_clearer, "foo has clearer");
    is($foo_attr->clearer, "clear_foo",  ".. and it's named clear_foo");

    ok($foo_attr->has_builder, "foo has builder");
    is($foo_attr->builder, "_build_foo",  ".. and it's named build_foo");

    ok($foo_attr->has_predicate, "foo has predicate");
    is($foo_attr->predicate, "has_foo",  ".. and it's named has_foo");

    ok($_foo_attr->is_lazy, "_foo is lazy");
    ok(!$_foo_attr->is_required, "lazy_build attributes are no longer automatically required");
    ok($_foo_attr->is_lazy_build, "_foo is lazy_build");

    ok($_foo_attr->has_clearer, "_foo has clearer");
    is($_foo_attr->clearer, "_clear_foo",  ".. and it's named _clear_foo");

    ok($_foo_attr->has_builder, "_foo has builder");
    is($_foo_attr->builder, "_build__foo",  ".. and it's named _build_foo");

    ok($_foo_attr->has_predicate, "_foo has predicate");
    is($_foo_attr->predicate, "_has_foo",  ".. and it's named _has_foo");

    my $instance = Test::LazyBuild::Attribute->new;
    ok(!$instance->has_foo, "noo foo value yet");
    ok(!$instance->_has_foo, "noo _foo value yet");
    is($instance->foo, 'works', "foo builder works");
    is($instance->_foo, 'works too', "foo builder works too");
    dies_ok { $instance->fool }
#    throws_ok { $instance->fool }
#        qr/Test::LazyBuild::Attribute does not support builder method \'_build_fool\' for attribute \'fool\'/,
            "Correct error when a builder method is not present";

}

{
    package OutOfClassTest;

    use Mouse;
}

# Mouse::Exporter does not support 'with_meta'
#lives_ok { OutOfClassTest::has('foo', is => 'bare'); } 'create attr via direct sub call';
#lives_ok { OutOfClassTest->can('has')->('bar', is => 'bare'); } 'create attr via can';

#ok(OutOfClassTest->meta->get_attribute('foo'), 'attr created from sub call');
#ok(OutOfClassTest->meta->get_attribute('bar'), 'attr created from can');


{
    {
        package Foo;
        use Mouse;

        ::throws_ok { has 'foo' => ( 'ro', isa => 'Str' ) }
            qr/^Usage/, 'has throws error with odd number of attribute options';
    }

}