File: any-does-isa.t

package info (click to toggle)
libspecio-perl 0.50-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 900 kB
  • sloc: perl: 5,165; sh: 23; makefile: 2
file content (229 lines) | stat: -rw-r--r-- 8,042 bytes parent folder | download | duplicates (5)
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
use strict;
use warnings;
use utf8;
use open ':encoding(UTF-8)', ':std';

use Test::Fatal;
use Test::More 0.96;

use Specio::Declare;

## no critic (Modules::ProhibitMultiplePackages)
{
    package Foo;
    sub quux { }
}

subtest(
    'object_can_type',
    sub {
        my $object_can = object_can_type( methods => [ 'foo', 'bar' ] );
        like(
            exception { $object_can->validate_or_die(undef) },
            qr/\QAn undef will never pass an ObjectCan check (wants bar and foo)/,
            'exception for undef'
        );
        like(
            exception { $object_can->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an ObjectCan check (wants bar and foo)/,
            'exception for empty string'
        );
        like(
            exception { $object_can->validate_or_die('Foo') },
            qr/\QA plain scalar ("Foo") will never pass an ObjectCan check (wants bar and foo)/,
            'exception for non-empty string'
        );
        like(
            exception { $object_can->validate_or_die(42) },
            qr/\QA number (42) will never pass an ObjectCan check (wants bar and foo)/,
            'exception for number'
        );
        like(
            exception { $object_can->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an ObjectCan check (wants bar and foo)/,
            'exception for arrayref'
        );
        like(
            exception { $object_can->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class is missing the 'bar' and 'foo' methods/,
            'exception for object without wanted methods'
        );
    }
);

subtest(
    'any_can_type',
    sub {
        my $any_can = any_can_type( methods => [ 'foo', 'bar' ] );
        like(
            exception { $any_can->validate_or_die(undef) },
            qr/\QAn undef will never pass an AnyCan check (wants bar and foo)/,
            'exception for undef'
        );
        like(
            exception { $any_can->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an AnyCan check (wants bar and foo)/,
            'exception for empty string'
        );
        like(
            exception { $any_can->validate_or_die('Baz') },
            qr/\QThe Baz class is missing the 'bar' and 'foo' methods/,
            'exception for non-empty string'
        );
        like(
            exception { $any_can->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an AnyCan check (wants bar and foo)/,
            'exception for arrayref'
        );
        like(
            exception { $any_can->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class is missing the 'bar' and 'foo' methods/,
            'exception for non-empty string'
        );
    }
);

subtest(
    'object_isa_type',
    sub {
        my $object_isa = object_isa_type( class => 'Foo' );
        like(
            exception { $object_isa->validate_or_die(undef) },
            qr/\QAn undef will never pass an ObjectIsa check (wants Foo)/,
            'exception for undef'
        );
        like(
            exception { $object_isa->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an ObjectIsa check (wants Foo)/,
            'exception for empty string'
        );
        like(
            exception { $object_isa->validate_or_die('Foo') },
            qr/\QA plain scalar ("Foo") will never pass an ObjectIsa check (wants Foo)/,
            'exception for non-empty string'
        );
        like(
            exception { $object_isa->validate_or_die(42) },
            qr/\QA number (42) will never pass an ObjectIsa check (wants Foo)/,
            'exception for number'
        );
        like(
            exception { $object_isa->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an ObjectIsa check (wants Foo)/,
            'exception for arrayref'
        );
        like(
            exception { $object_isa->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class is not a subclass of the Foo class/,
            'exception for object of the wrong class'
        );
    }
);

subtest(
    'any_isa_type',
    sub {
        my $any_isa = any_isa_type( class => 'Foo' );
        like(
            exception { $any_isa->validate_or_die(undef) },
            qr/\QAn undef will never pass an AnyIsa check (wants Foo)/,
            'exception for undef'
        );
        like(
            exception { $any_isa->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an AnyIsa check (wants Foo)/,
            'exception for empty string'
        );
        like(
            exception { $any_isa->validate_or_die('Baz') },
            qr/\QThe Baz class is not a subclass of the Foo class/,
            'exception for plain scalar'
        );
        like(
            exception { $any_isa->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an AnyIsa check (wants Foo)/,
            'exception for arrayref'
        );
        like(
            exception { $any_isa->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class is not a subclass of the Foo class/,
            'exception for object of the wrong class'
        );
    }
);

{
    package Role::Foo;
    use Role::Tiny;
}

subtest(
    'object_does_type',
    sub {
        my $object_does = object_does_type( role => 'Role::Foo' );
        like(
            exception { $object_does->validate_or_die(undef) },
            qr/\QAn undef will never pass an ObjectDoes check (wants Role::Foo)/,
            'exception for undef'
        );
        like(
            exception { $object_does->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an ObjectDoes check (wants Role::Foo)/,
            'exception for empty string'
        );
        like(
            exception { $object_does->validate_or_die('Role::Foo') },
            qr/\QA plain scalar ("Role::Foo") will never pass an ObjectDoes check (wants Role::Foo)/,
            'exception for non-empty string'
        );
        like(
            exception { $object_does->validate_or_die(42) },
            qr/\QA number (42) will never pass an ObjectDoes check (wants Role::Foo)/,
            'exception for number'
        );
        like(
            exception { $object_does->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an ObjectDoes check (wants Role::Foo)/,
            'exception for arrayref'
        );
        like(
            exception { $object_does->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class does not consume the Role::Foo role/,
            'exception for object that does not consume the wanted role'
        );
    }
);

subtest(
    'any_does_type',
    sub {
        my $any_does = any_does_type( role => 'Role::Foo' );
        like(
            exception { $any_does->validate_or_die(undef) },
            qr/\QAn undef will never pass an AnyDoes check (wants Role::Foo)/,
            'exception for undef'
        );
        like(
            exception { $any_does->validate_or_die(q{}) },
            qr/\QAn empty string will never pass an AnyDoes check (wants Role::Foo)/,
            'exception for empty string'
        );
        like(
            exception { $any_does->validate_or_die('Baz') },
            qr/\QThe Baz class does not consume the Role::Foo role/,
            'exception for plain scalar'
        );
        like(
            exception { $any_does->validate_or_die( [] ) },
            qr/\QAn unblessed reference ([  ]) will never pass an AnyDoes check (wants Role::Foo)/,
            'exception for arrayref'
        );
        like(
            exception { $any_does->validate_or_die( bless {}, 'Baz' ) },
            qr/\QThe Baz class does not consume the Role::Foo role/,
            'exception for object that does not consume the wanted role'
        );
    }
);

done_testing();