File: basic.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 (287 lines) | stat: -rw-r--r-- 7,907 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
277
278
279
280
281
282
283
284
285
286
287
=pod

=encoding utf-8

=head1 PURPOSE

Checks Type::Parser works.

=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 lib qw( ./lib ./t/lib ../inc ./inc );

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

use Type::Parser qw( _std_eval parse extract_type );
use Types::Standard qw( -types slurpy );
use Type::Utils;

sub types_equal
{
	my ($a, $b) = map { ref($_) ? $_ : _std_eval($_) } @_[0, 1];
	my ($A, $B) = map { $_->inline_check('$X') } ($a, $b);
	my $msg = "$_[0] eq $_[1]";
	$msg = "$msg - $_[2]" if $_[2];
	@_ = ($A, $B, $msg);
	goto \&Test::More::is;
}

note "Basics";
types_equal("Int", Int);
types_equal("(Int)", Int, "redundant parentheses");
types_equal("((((Int))))", Int, "many redundant parentheses");

note "Class types";
types_equal("DateTime::", InstanceOf["DateTime"]);
types_equal("InstanceOf['DateTime']", InstanceOf["DateTime"]);
types_equal("Tied[Foo::]", Tied["Foo"]);
types_equal("Tied['Foo']", Tied["Foo"]);

note "Parameterization";
types_equal("Int[]", Int, "empty parameterization against non-parameterizable type");
types_equal("Tuple[]", Tuple[], "empty parameterization against parameterizble type");
types_equal("ArrayRef[]", ArrayRef, "empty parameterization against parameterizable type");
types_equal("ArrayRef[Int]", ArrayRef[Int], "parameterized type");
types_equal("Overload[15]", Overload[15], "numeric parameter (decimal integer)");
types_equal("Overload[0x0F]", Overload[15], "numeric parameter (hexadecimal integer)");
types_equal("Overload[0x0f]", Overload[15], "numeric parameter (hexadecimal integer, lowercase)");
types_equal("Overload[-0xF]", Overload[-15], "numeric parameter (hexadecimal integer, negative)");
types_equal("Overload[1.5]", Overload[1.5], "numeric parameter (float)");
types_equal("Ref['HASH']", Ref['HASH'], "string parameter (singles)");
types_equal("Ref[\"HASH\"]", Ref['HASH'], "string parameter (doubles)");
types_equal("Ref[q(HASH)]", Ref['HASH'], "string parameter (q)");
types_equal("Ref[qq(HASH)]", Ref['HASH'], "string parameter (qq)");
types_equal("StrMatch[qr{foo}]", StrMatch[qr{foo}], "regexp parameter");

# No, Overload[15] doesn't make much sense, but it's one of the few types in
# Types::Standard that accept pretty much any list of strings as parameters.

note "Unions";
types_equal("Int|HashRef", Int|HashRef);
types_equal("Int|HashRef|ArrayRef", Int|HashRef|ArrayRef);
types_equal("ArrayRef[Int|HashRef]", ArrayRef[Int|HashRef], "union as a parameter");
types_equal("ArrayRef[Int|HashRef[Int]]", ArrayRef[Int|HashRef[Int]]);
types_equal("ArrayRef[HashRef[Int]|Int]", ArrayRef[HashRef([Int]) | Int]);

note "Intersections";
types_equal("Int&Num", Int & Num);
types_equal("Int&Num&Defined", Int & Num & Defined);
types_equal("ArrayRef[Int]&Defined", (ArrayRef[Int]) & Defined);

note "Union + Intersection";
types_equal("Int&Num|ArrayRef", (Int & Num) | ArrayRef);
types_equal("(Int&Num)|ArrayRef", (Int & Num) | ArrayRef);
types_equal("Int&(Num|ArrayRef)", Int & (Num | ArrayRef));
types_equal("Int&Num|ArrayRef&Ref", intersection([Int, Num]) | intersection([ArrayRef, Ref]));

note "Complementary types";
types_equal("~Int", ~Int);
types_equal("~ArrayRef[Int]", ArrayRef([Int])->complementary_type);
types_equal("~Int|CodeRef", (~Int)|CodeRef);
types_equal("~(Int|CodeRef)", ~(Int|CodeRef), 'precedence of "~" versus "|"');

note "Comma";
types_equal("Map[Num,Int]", Map[Num,Int]);
types_equal("Map[Int,Num]", Map[Int,Num]);
types_equal("Map[Int,Int|ArrayRef[Int]]", Map[Int,Int|ArrayRef[Int]]);
types_equal("Map[Int,ArrayRef[Int]|Int]", Map[Int,ArrayRef([Int])|Int]);
types_equal("Dict[foo=>Int,bar=>Num]", Dict[foo=>Int,bar=>Num]);
types_equal("Dict['foo'=>Int,'bar'=>Num]", Dict[foo=>Int,bar=>Num]);
types_equal("Dict['foo',Int,'bar',Num]", Dict[foo=>Int,bar=>Num]);

note "Slurpy";
types_equal("Dict[slurpy=>Int,bar=>Num]", Dict[slurpy=>Int,bar=>Num]);
types_equal("Tuple[Str, Int, slurpy ArrayRef[Int]]", Tuple[Str, Int, slurpy ArrayRef[Int]]);
types_equal("Tuple[Str, Int, slurpy(ArrayRef[Int])]", Tuple[Str, Int, slurpy ArrayRef[Int]]);

note "Complexity";
types_equal(
	"ArrayRef[DateTime::]|HashRef[Int|DateTime::]|CodeRef",
	ArrayRef([InstanceOf["DateTime"]]) | HashRef([Int|InstanceOf["DateTime"]]) | CodeRef
);
types_equal(
	"ArrayRef   [DateTime::]  |HashRef[ Int|\tDateTime::]|CodeRef ",
	ArrayRef([InstanceOf["DateTime"]]) | HashRef([Int|InstanceOf["DateTime"]]) | CodeRef,
	"gratuitous whitespace",
);

note "Bad expressions";
like(
	exception { _std_eval('%hello') },
	qr{^Unexpected token in primary type expression; got '%hello'},
	'weird token'
);
like(
	exception { _std_eval('Str Int') },
	qr{^Unexpected tail on type expression:  Int},
	'weird stuff 1'
);
like(
	exception { _std_eval('ArrayRef(Int)') },
	qr{^Unexpected tail on type expression: .Int.},
	'weird stuff 2'
);

note "Tail retention";
my ($ast, $remaining) = parse("ArrayRef   [DateTime::]  |HashRef[ Int|\tDateTime::]|CodeRef monkey nuts ");
is($remaining, " monkey nuts ", "remainder is ok");

($ast, $remaining) = parse("Int, Str");
is($remaining, ", Str", "comma can indicate beginning of remainder");

require Type::Registry;
my $type;
my $reg = Type::Registry->new;
$reg->add_types( -Standard );
($type, $remaining) = extract_type('ArrayRef [ Int ] yah', $reg);
types_equal($type, ArrayRef[Int], 'extract_type works');
like($remaining, qr/\A\s?yah\z/, '... and provides proper remainder too');

note "Parsing edge cases";
is_deeply(
	scalar parse('Xyzzy[Foo]'),
	{
		'type' => 'parameterized',
		'base' => {
			'type' => 'primary',
			'token' => bless( [
				'TYPE',
				'Xyzzy'
			], 'Type::Parser::Token' ),
		},
		'params' => {
			'type' => 'list',
			'list' => [
				{
					'type' => 'primary',
					'token' => bless( [
						'TYPE',
						'Foo'
					], 'Type::Parser::Token' ),
				}
			],
		},
	},
	'Xyzzy[Foo] - parameter is treated as a type constraint'
);
is_deeply(
	scalar parse('Xyzzy["Foo"]'),
	{
		'type' => 'parameterized',
		'base' => {
			'type' => 'primary',
			'token' => bless( [
				'TYPE',
				'Xyzzy'
			], 'Type::Parser::Token' ),
		},
		'params' => {
			'type' => 'list',
			'list' => [
				{
					'type' => 'primary',
					'token' => bless( [
						'QUOTELIKE',
						'"Foo"'
					], 'Type::Parser::Token' ),
				}
			],
		},
	},
	'Xyzzy["Foo"] - parameter is treated as a string'
);
is_deeply(
	scalar parse('Xyzzy[-100]'),
	{
		'type' => 'parameterized',
		'base' => {
			'type' => 'primary',
			'token' => bless( [
				'TYPE',
				'Xyzzy'
			], 'Type::Parser::Token' ),
		},
		'params' => {
			'type' => 'list',
			'list' => [
				{
					'type' => 'primary',
					'token' => bless( [
						'STRING',
						'-100'
					], 'Type::Parser::Token' ),
				}
			],
		},
	},
	'Xyzzy[-100] - parameter is treated as a string'
);
is_deeply(
	scalar parse('Xyzzy[200]'),
	{
		'type' => 'parameterized',
		'base' => {
			'type' => 'primary',
			'token' => bless( [
				'TYPE',
				'Xyzzy'
			], 'Type::Parser::Token' ),
		},
		'params' => {
			'type' => 'list',
			'list' => [
				{
					'type' => 'primary',
					'token' => bless( [
						'STRING',
						'200'
					], 'Type::Parser::Token' ),
				}
			],
		},
	},
	'Xyzzy[200] - parameter is treated as a string'
);
is_deeply(
	scalar parse('Xyzzy[+20.0]'),
	{
		'type' => 'parameterized',
		'base' => {
			'type' => 'primary',
			'token' => bless( [
				'TYPE',
				'Xyzzy'
			], 'Type::Parser::Token' ),
		},
		'params' => {
			'type' => 'list',
			'list' => [
				{
					'type' => 'primary',
					'token' => bless( [
						'STRING',
						'+20.0'
					], 'Type::Parser::Token' ),
				}
			],
		},
	},
	'Xyzzy[+20.0] - parameter is treated as a string'
);

done_testing;