summaryrefslogtreecommitdiff
path: root/t/20-modules/Type-Params/wrap.t
blob: 16a2e238ce42ffad2d643659419deba0e099805f (plain)
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
=pod

=encoding utf-8

=head1 PURPOSE

Test C<wrap_subs> and C<wrap_methods> from L<Type::Params>.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2019-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;

{
	package Local::Test1;
	use Types::Standard qw( Str Int Num ArrayRef );
	use Type::Params qw( wrap_subs wrap_methods compile_named );
	sub abc {
		return @_;
	}
	sub xyz {
		return @_;
	}
	wrap_subs(
		abc => [Int, Int, Int],
		uvw => [Str],   # wraps sub {}
		xyz => compile_named({ subname => 'xyz' }, x => Int, y => Int, z => Int),
	);
}

subtest "simple use of wrap_subs" => sub {
	is_deeply(
		[ Local::Test1::abc(1, 2, 3) ],
		[ 1, 2, 3 ],
	);
	
	is_deeply(
		[Local::Test1::uvw('hello world')],
		[],
	);
	
	is_deeply(
		[ Local::Test1::xyz(x => 1, y => 2, z => 3) ],
		[{ x => 1, y => 2, z => 3 }],
	);
	
	my $e = exception {
		Local::Test1::abc(1, 2),
	};
	
	like($e, qr/Wrong number of parameters/);
	
	$e = exception {
		Local::Test1::uvw({}),
	};
	
	like($e, qr/Reference \{\} did not pass type constraint "Str" \(in \$_\[0]\)/);
	
	$e = exception {
		Local::Test1::xyz(x => 1, y => 2, z => []),
	};
	
	like($e, qr/Reference \[\] did not pass type constraint "Int" \(in \$_\{"z"\}\)/);
};

{
	package Local::Test2;
	use Types::Standard qw( Str Int Num ArrayRef );
	use Type::Params qw( wrap_subs wrap_methods compile_named );
	sub abc {
		return @_;
	}
	sub def {
		return @_;
	}
	sub xyz {
		return @_;
	}
	wrap_methods(
		abc => [Int, Int, Int],
		uvw => [Str],   # wraps sub {}
		xyz => compile_named({ subname => 'xyz' }, x => Int, y => Int, z => Int),
	);
}

subtest "simple use of wrap_methods" => sub {
	is_deeply(
		[ Local::Test2->abc(1, 2, 3) ],
		[ 'Local::Test2', 1, 2, 3 ],
	);
	
	is_deeply(
		[ Local::Test2->uvw('hello world') ],
		[],
	);
	
	is_deeply(
		[ Local::Test2->xyz(x => 1, y => 2, z => 3) ],
		[ 'Local::Test2', { x => 1, y => 2, z => 3 }],
	);
	
	my $e = exception {
		Local::Test2->abc(1, 2),
	};
	
	like($e, qr/Wrong number of parameters/);
	
	$e = exception {
		Local::Test2->uvw({}),
	};
	
	like($e, qr/Reference \{\} did not pass type constraint "Str" \(in \$_\[1]\)/);
	
	$e = exception {
		Local::Test2->xyz(x => 1, y => 2, z => []),
	};
	
	like($e, qr/Reference \[\] did not pass type constraint "Int" \(in \$_\{"z"\}\)/);
};

{
	package Local::Test3;
	our @ISA = 'Local::Test2';
	use Types::Standard qw( Str Int Num ArrayRef );
	use Type::Params qw( wrap_subs wrap_methods compile_named );
	my $Even = Int->where(q{ $_ % 2 == 0 });
	wrap_methods(
		abc => [$Even, $Even, $Even],
		def => [Num],   # inherited
	);
}

subtest "wrap_methods with inheritance" => sub {
	is_deeply(
		[ Local::Test3->abc(2, 4, 6) ],
		[ 'Local::Test3', 2, 4, 6 ],
	);
	
	is_deeply(
		[ Local::Test3->def(3.1) ],
		[ 'Local::Test3', 3.1 ],
	);
	
	is_deeply(
		[ Local::Test3->uvw('hello world') ],
		[],
	);
	
	is_deeply(
		[ Local::Test3->xyz(x => 1, y => 2, z => 3) ],
		[ 'Local::Test3', { x => 1, y => 2, z => 3 }],
	);
	
	my $e = exception {
		Local::Test3->abc(1, 2, 2),
	};
	
	like($e, qr/Value "1" did not pass type constraint \(in \$_\[1\]\)/);
	
	$e = exception {
		Local::Test3->def({}),
	};
	
	like($e, qr/Reference \{\} did not pass type constraint "Num" \(in \$_\[1]\)/);
	
	$e = exception {
		Local::Test3->uvw({}),
	};
	
	like($e, qr/Reference \{\} did not pass type constraint "Str" \(in \$_\[1]\)/);
	
	$e = exception {
		Local::Test3->xyz(x => 1, y => 2, z => []),
	};
	
	like($e, qr/Reference \[\] did not pass type constraint "Int" \(in \$_\{"z"\}\)/);
};

done_testing;