File: slurpy.t

package info (click to toggle)
libtype-tiny-perl 2.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,948 kB
  • sloc: perl: 14,610; makefile: 2; sh: 1
file content (276 lines) | stat: -rw-r--r-- 6,228 bytes parent folder | download | duplicates (2)
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
=pod

=encoding utf-8

=head1 PURPOSE

Test L<Type::Params> usage with slurpy parameters.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017-2023 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut

use strict;
use warnings;

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

use Type::Params qw( compile signature );
use Types::Standard -types, "slurpy";

my $chk = compile(Str, slurpy HashRef[Int]);

is_deeply(
	[ $chk->("Hello", foo => 1, bar => 2) ],
	[ "Hello", { foo => 1, bar => 2 } ],
	'simple test',
);

is_deeply(
	[ $chk->("Hello", { foo => 1, bar => 2 }) ],
	[ "Hello", { foo => 1, bar => 2 } ],
	'simple test with ref',
);

like(
	exception { $chk->("Hello", foo => 1, bar => 2.1) },
	qr{did not pass type constraint "HashRef\[Int\]" \(in \$SLURPY\)},
	'simple test failing type check',
);

subtest "Different styles of slurpy work" => sub {
	for my $compile_this (
		[ 'Str, slurpy HashRef'           => Str, slurpy HashRef ],
		[ 'Str, Slurpy[HashRef]'          => Str, Slurpy[HashRef] ],
		[ 'Str, HashRef, { slurpy => 1 }' => Str, HashRef, { slurpy => 1 } ],
		[ 'Str, { slurpy => HashRef }'    => Str, { 'slurpy' => HashRef } ],
	) {
		my ( $desc, @args ) = @$compile_this;
		subtest "Compiling: $desc" => sub {
			my $chk2 = compile @args;

			is_deeply(
				[ $chk2->("Hello", foo => 1, bar => 2) ],
				[ "Hello", { foo => 1, bar => 2 } ]
			);

			is_deeply(
				[ $chk2->("Hello", { foo => 1, bar => 2 }) ],
				[ "Hello", { foo => 1, bar => 2 } ]
			);

			like(
				exception { $chk2->("Hello", foo => 1, "bar") },
				qr{^Odd number of elements in HashRef},
			);
		};
	}
};

subtest "slurpy Map works" => sub {
	my $chk3 = compile(Str, slurpy Map);
	
	is_deeply(
		[ $chk3->("Hello", foo => 1, "bar" => 2) ],
		[ Hello => { foo => 1, bar => 2 } ],
	);
	
	like(
		exception { $chk3->("Hello", foo => 1, "bar") },
		qr{^Odd number of elements in Map},
	);
};

subtest "slurpy Tuple works" => sub {
	my $chk4 = compile(Str, slurpy Tuple[Str, Int, Str]);
	
	is_deeply(
		[ $chk4->("Hello", foo => 1, "bar") ],
		[ Hello => [ qw/ foo 1 bar / ] ],
	);
};

{
	my $check;
	sub xyz {
		$check ||= compile( Int, Slurpy[HashRef] );
		my ($num, $hr) = $check->(@_);
		return [ $num, $hr ];
	}
	
	subtest "Slurpy[HashRef] works" => sub {
		is_deeply( xyz( 5,   foo => 1, bar => 2   ), [ 5, { foo => 1, bar => 2 } ] );
		is_deeply( xyz( 5, { foo => 1, bar => 2 } ), [ 5, { foo => 1, bar => 2 } ] );
		
		note compile( { want_source => 1 }, Int, Slurpy[HashRef] );
	};
}

{
	my $check;
	sub xyz2 {
		$check ||= compile( Int, HashRef, { slurpy => 1 } );
		my ($num, $hr) = $check->(@_);
		return [ $num, $hr ];
	}
	
	subtest "HashRef { slurpy => 1 } works" => sub {
		is_deeply( xyz2( 5,   foo => 1, bar => 2   ), [ 5, { foo => 1, bar => 2 } ] );
		is_deeply( xyz2( 5, { foo => 1, bar => 2 } ), [ 5, { foo => 1, bar => 2 } ] );
	};
}

{
	my $check;
	sub xyz3 {
		$check ||= compile( Int, { slurpy => HashRef } );
		my ($num, $hr) = $check->(@_);
		return [ $num, $hr ];
	}
	
	subtest "{ slurpy => HashRef } works" => sub {
		is_deeply( xyz3( 5,   foo => 1, bar => 2   ), [ 5, { foo => 1, bar => 2 } ] );
		is_deeply( xyz3( 5, { foo => 1, bar => 2 } ), [ 5, { foo => 1, bar => 2 } ] );
	};
}

{
	my $check;
	sub xyz4 {
		$check ||= compile( Int, ( Slurpy[HashRef] )->where( '1' ) );
		my ($num, $hr) = $check->(@_);
		return [ $num, $hr ];
	}
	
	subtest "Subtype of Slurpy[HashRef] works" => sub {
		is_deeply( xyz4( 5,   foo => 1, bar => 2   ), [ 5, { foo => 1, bar => 2 } ] );
		is_deeply( xyz4( 5, { foo => 1, bar => 2 } ), [ 5, { foo => 1, bar => 2 } ] );
		
		note compile( { want_source => 1 }, Int, ( Slurpy[HashRef] )->where( '1' ) );
	};
}

{
	my $e = exception {
		signature(
			positional => [ Slurpy[ArrayRef], ArrayRef ],
		);
	};
	like(
		$e,
		qr/Parameter following slurpy parameter/,
		'Exception thrown for parameter after a slurpy in positional signature',
	);
}

{
	my $e = exception {
		signature(
			positional => [ Slurpy[ArrayRef], Slurpy[ArrayRef] ],
		);
	};
	like(
		$e,
		qr/Parameter following slurpy parameter/,
		'Exception thrown for slurpy parameter after a slurpy in positional signature',
	);
}

{
	my $e = exception {
		signature(
			named => [ foo => Slurpy[ArrayRef], bar => Slurpy[ArrayRef] ],
		);
	};
	like(
		$e,
		qr/Found multiple slurpy parameters/i,
		'Exception thrown for named signature with two slurpies',
	);
}

{
	my $e = exception {
		signature(
			named => [ foo => Slurpy[ArrayRef] ],
		);
	};
	like(
		$e,
		qr/Signatures with named parameters can only have slurpy parameters which are a subtype of HashRef/i,
		'Exception thrown for named signature with ArrayRef slurpy',
	);
}

{
	my $check;
	my $e = exception {
		$check = signature(
			named => [ bar => Slurpy[HashRef], foo => ArrayRef ],
			bless => 0,
		);
	};
	is(
		$e,
		undef,
		'Named signature may have slurpy parameter before others',
	);
	is_deeply(
		[ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ],
		[ { foo => [ 1..4 ], bar => { abc => 1, def => 2 } } ],
		'... and expected behaviour',
	) or diag explain [ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ];
}

{
	my $check;
	my $e = exception {
		$check = signature(
			named => [ bar => Slurpy[HashRef], foo => ArrayRef ],
			named_to_list => 1,
		);
	};
	is(
		$e,
		undef,
		'Named-to-list => 1 signature may have slurpy parameter before others',
	);
	is_deeply(
		[ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ],
		[ { abc => 1, def => 2 }, [ 1..4 ] ],
		'... and expected behaviour',
	) or diag explain [ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ];
}

{
	my $check;
	my $e = exception {
		$check = signature(
			named => [ bar => Slurpy[HashRef], foo => ArrayRef ],
			named_to_list => [ qw( foo bar ) ],
		);
	};
	is(
		$e,
		undef,
		'Named-to-list => ARRAY signature may have slurpy parameter before others',
	);
	is_deeply(
		[ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ],
		[ [ 1..4 ], { abc => 1, def => 2 } ],
		'... and expected behaviour',
	) or diag explain [ $check->( foo => [ 1..4 ], abc => 1, def => 2 ) ];
}

done_testing;