File: with-specio.t

package info (click to toggle)
libmoose-perl 2.2207-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 7,416 kB
  • sloc: perl: 21,345; ansic: 291; makefile: 10
file content (300 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download | duplicates (3)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use strict;
use warnings;

use Test::Needs {
    'perl' => '5.010',
    'Specio::Declare'           => '0.10',
    'Specio::Library::Builtins' => '0.10',
};

use Test::Fatal;
use Test::Moose qw( with_immutable );
use Test::More 0.96;

my @array_delegations = qw(
    accessor
    clear
    count
    delete
    elements
    first_index
    first
    get
    grep
    insert
    is_empty
    join
    map
    natatime
    pop
    push
    reduce
    set
    shallow_clone
    shift
    shuffle
    sort_in_place
    sort
    splice
    uniq
    unshift
);

my @hash_delegations = qw(
    accessor
    clear
    count
    defined
    delete
    elements
    exists
    get
    is_empty
    keys
    kv
    set
    shallow_clone
    values
);

{
    is(
        exception {
            package TestInlining;

            use Moose;
            use Specio::Library::Builtins;

            has plain_array => (
                is     => 'ro',
                isa    => t('ArrayRef'),
                traits => ['Array'],
                handles =>
                    { map { $_ . '_plain_array' => $_ } @array_delegations }
            );

            has array_of_str => (
                is     => 'ro',
                isa    => t( 'ArrayRef', of => t('Str') ),
                traits => ['Array'],
                handles =>
                    { map { $_ . '_array_of_str' => $_ } @array_delegations }
            );

            has plain_hash => (
                is     => 'ro',
                isa    => t('HashRef'),
                traits => ['Hash'],
                handles =>
                    { map { $_ . '_plain_hash' => $_ } @hash_delegations }
            );

            has hash_of_str => (
                is     => 'ro',
                isa    => t( 'HashRef', of => t('Str') ),
                traits => ['Hash'],
                handles =>
                    { map { $_ . '_hash_of_str' => $_ } @hash_delegations }
            );
        },
        undef,
        'Type::Tiny is usable with native traits',
    );
}

{
    package Foo;

    use Moose;
    use Specio::Library::Builtins;

    has int => (
        is  => 'ro',
        isa => t('Int'),
    );

    has array_of_ints => (
        is  => 'ro',
        isa => t( 'ArrayRef', of => t('Int') ),
    );

    has hash_of_ints => (
        is  => 'ro',
        isa => t( 'HashRef', of => t('Int') ),
    );
}

with_immutable(
    sub {
        my $is_immutable = shift;
        subtest(
            'Foo class' . ( $is_immutable ? ' (immutable)' : q{} ),
            sub {

                is(
                    exception { Foo->new( int => 42 ) },
                    undef,
                    '42 is an acceptable int'
                );

                my $exception_class = 'Moose::Exception::'
                    . (
                    $is_immutable
                    ? 'ValidationFailedForInlineTypeConstraint'
                    : 'ValidationFailedForTypeConstraint'
                    );

                isa_ok(
                    exception { Foo->new( int => 42.4 ) },
                    $exception_class,
                );

                is(
                    exception { Foo->new( array_of_ints => [ 42, 84 ] ) },
                    undef,
                    '[ 42, 84 ] is an acceptable array of ints'
                );

                isa_ok(
                    exception { Foo->new( array_of_ints => [ 42.4, 84 ] ) },
                    $exception_class,
                );

                is(
                    exception {
                        Foo->new( hash_of_ints => { foo => 42, bar => 84 } );
                    },
                    undef,
                    '{ foo => 42, bar => 84 } is an acceptable array of ints'
                );

                isa_ok(
                    exception {
                        Foo->new(
                            hash_of_ints => { foo => 42.4, bar => 84 } );
                    },
                    $exception_class,
                );
            }
        );
    },
    'Foo'
);

{
    package Bar;

    use Moose;
    use Specio::Declare;
    use Specio::Library::Builtins;

    my $array_of_ints = anon( parent => t( 'ArrayRef', of => t('Int') ) );

    coerce(
        $array_of_ints,
        from  => t('Int'),
        using => sub {
            return [ $_[0] ];
        }
    );

    has array_of_ints => (
        is     => 'ro',
        isa    => $array_of_ints,
        coerce => 1,
    );

    my $hash_of_ints = anon( parent => t( 'HashRef', of => t('Int') ) );

    coerce(
        $hash_of_ints,
        from  => t('Int'),
        using => sub {
            return { foo => $_[0] };
        }
    );

    has hash_of_ints => (
        is     => 'ro',
        isa    => $hash_of_ints,
        coerce => 1,
    );
}

with_immutable(
    sub {
        my $is_immutable = shift;
        subtest(
            'Bar class' . ( $is_immutable ? ' (immutable)' : q{} ),
            sub {

                is(
                    exception { Bar->new( array_of_ints => [ 42, 84 ] ) },
                    undef,
                    '[ 42, 84 ] is an acceptable array of ints'
                );

                my $exception_class = 'Moose::Exception::'
                    . (
                    $is_immutable
                    ? 'ValidationFailedForInlineTypeConstraint'
                    : 'ValidationFailedForTypeConstraint'
                    );

                isa_ok(
                    exception { Bar->new( array_of_ints => [ 42.4, 84 ] ) },
                    $exception_class,
                );

                {
                    my $bar;
                    is(
                        exception { $bar = Bar->new( array_of_ints => 42 ) },
                        undef,
                        '42 is an acceptable array of ints with coercion'
                    );

                    is_deeply(
                        $bar->array_of_ints(),
                        [42],
                        'int is coerced to single element arrayref'
                    );
                }

                is(
                    exception {
                        Bar->new( hash_of_ints => { foo => 42, bar => 84 } );
                    },
                    undef,
                    '{ foo => 42, bar => 84 } is an acceptable array of ints'
                );

                isa_ok(
                    exception {
                        Bar->new(
                            hash_of_ints => { foo => 42.4, bar => 84 } );
                    },
                    $exception_class,
                );

                {
                    my $bar;
                    is(
                        exception { $bar = Bar->new( hash_of_ints => 42 ) },
                        undef,
                        '42 is an acceptable hash of ints with coercion'
                    );

                    is_deeply(
                        $bar->hash_of_ints(),
                        { foo => 42 },
                        'int is coerced to single element hashref'
                    );
                }
            }
        );
    },
    'Bar'
);

done_testing();