File: 51coerce.t

package info (click to toggle)
libkavorka-perl 0.039-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 696 kB
  • sloc: perl: 2,850; makefile: 6; sh: 6
file content (198 lines) | stat: -rw-r--r-- 4,310 bytes parent folder | download
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
=pod

=encoding utf-8

=head1 PURPOSE

Check that type coercions work.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2013-2014, 2017 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 Example;
	use Kavorka;
	use Type::Registry qw(t);
	
	BEGIN {
		t->add_types( -Standard );
		t->add_type( t->Int->create_child_type(name => 'RoundedInt1') );
		t->add_type( t->Int->create_child_type(name => 'RoundedInt2') );
		
		t->RoundedInt1->coercion->add_type_coercions(t->Num, q   { int($_) });
		t->RoundedInt2->coercion->add_type_coercions(t->Num, sub { int($_) });
	};
	
	# We need to test a non-inlinable coercion.
	::ok( not t->RoundedInt2->coercion->can_be_inlined );
	
	fun foo ( RoundedInt1 $x )              { return $x }
	fun bar ( RoundedInt1 $x does coerce )  { return $x }
	fun baz ( RoundedInt2 $x does coerce )  { return $x }
	
	fun foo_array ( RoundedInt1 @y )              { return \@y }
	fun bar_array ( RoundedInt1 @y does coerce )  { return \@y }
	fun baz_array ( RoundedInt2 @y does coerce )  { return \@y }

	fun foo_arrayref ( slurpy ArrayRef[RoundedInt1] $z )              { return $z }
	fun bar_arrayref ( slurpy ArrayRef[RoundedInt1] $z does coerce )  { return $z }
	fun baz_arrayref ( slurpy ArrayRef[RoundedInt2] $z does coerce )  { return $z }
}

is(
	Example::foo(42),
	42,
	'type constraint with coercion, but parameter does not coerce - valid value'
);

ok(
	exception { Example::foo(42.1) },
	'type constraint with coercion, but parameter does not coerce - invalid value'
);

is(
	Example::bar(42),
	42,
	'type constraint with coercion - valid value'
);

is(
	Example::bar(42.2),
	42,
	'type constraint with coercion - coercible value'
);

ok(
	exception { Example::bar("Non-numeric") },
	'type constraint with coercion - invalid value'
);

is(
	Example::baz(42),
	42,
	'type constraint with non-inlinable coercion - valid value'
);

is(
	Example::baz(42.2),
	42,
	'type constraint with non-inlinable coercion - coercible value'
);

ok(
	exception { Example::baz("Non-numeric") },
	'type constraint with non-inlinable coercion - invalid value'
);

note "arrays...";

is_deeply(
	Example::foo_array(123, 42),
	[123, 42],
	'type constraint with coercion, but parameter does not coerce - valid value'
);

ok(
	exception { Example::foo_array(123, 42.1) },
	'type constraint with coercion, but parameter does not coerce - invalid value'
);

is_deeply(
	Example::bar_array(123, 42),
	[123, 42],
	'type constraint with coercion - valid value'
);

is_deeply(
	Example::bar_array(123, 42.2),
	[123, 42],
	'type constraint with coercion - coercible value'
);

ok(
	exception { Example::bar_array("Non-numeric") },
	'type constraint with coercion - invalid value'
);

is_deeply(
	Example::baz_array(123, 42),
	[123, 42],
	'type constraint with non-inlinable coercion - valid value'
);

is_deeply(
	Example::baz_array(123, 42.2),
	[123, 42],
	'type constraint with non-inlinable coercion - coercible value'
);

ok(
	exception { Example::baz_array("Non-numeric") },
	'type constraint with non-inlinable coercion - invalid value'
);

note "arrayrefs...";

is_deeply(
	Example::foo_arrayref(123, 42),
	[123, 42],
	'type constraint with coercion, but parameter does not coerce - valid value'
);

ok(
	exception { Example::foo_arrayref(123, 42.1) },
	'type constraint with coercion, but parameter does not coerce - invalid value'
);

is_deeply(
	Example::bar_arrayref(123, 42),
	[123, 42],
	'type constraint with coercion - valid value'
);

is_deeply(
	Example::bar_arrayref(123, 42.2),
	[123, 42],
	'type constraint with coercion - coercible value'
);

ok(
	exception { Example::bar_arrayref("Non-numeric") },
	'type constraint with coercion - invalid value'
);

is_deeply(
	Example::baz_arrayref(123, 42),
	[123, 42],
	'type constraint with non-inlinable coercion - valid value'
);

is_deeply(
	Example::baz_arrayref(123, 42.2),
	[123, 42],
	'type constraint with non-inlinable coercion - coercible value'
);

ok(
	exception { Example::baz_arrayref("Non-numeric") },
	'type constraint with non-inlinable coercion - invalid value'
);

done_testing;