File: type_default.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 (147 lines) | stat: -rw-r--r-- 2,355 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
=pod

=encoding utf-8

=head1 PURPOSE

Checks Type::Tiny's C<type_default> attribute works.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

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

use Types::Standard -types;

is(
	Any->type_default->(),
	undef,
	'Any->type_default',
);

is(
	Item->type_default->(),
	undef,
	'Item->type_default (inherited from Any)',
);

is(
	Defined->type_default,
	undef,
	'Defined->type_default (not inherited from Item)',
);

is(
	Str->type_default->(),
	'',
	'Str->type_default',
);

is(
	$_->type_default->(),
	0,
	"$_\->type_default",
) for Int, Num, StrictNum, LaxNum;

is(
	Bool->type_default->(),
	!!0,
	'Bool->type_default',
);

is(
	Undef->type_default->(),
	undef,
	'Undef->type_default',
);

is(
	Maybe->type_default->(),
	undef,
	'Maybe->type_default',
);

is(
	Maybe->of( Str )->type_default->(),
	'',
	'Maybe[Str]->type_default generated for parameterized type',
);

is_deeply(
	ArrayRef->type_default->(),
	[],
	'ArrayRef->type_default',
);

is_deeply(
	ArrayRef->of( Str )->type_default->(),
	[],
	'ArrayRef[Str]->type_default generated for parameterized type',
);

is(
	ArrayRef->of( Str, 1, 2 )->type_default,
	undef,
	'ArrayRef[Str, 1, 2]->type_default not generated',
);

is_deeply(
	HashRef->type_default->(),
	{},
	'HashRef->type_default',
);

is_deeply(
	HashRef->of( Str )->type_default->(),
	{},
	'HashRef[Str]->type_default generated for parameterized type',
);

is_deeply(
	Map->type_default->(),
	{},
	'Map->type_default',
);

is_deeply(
	Map->of( Str, Int )->type_default->(),
	{},
	'Map[Str, Int]->type_default generated for parameterized type',
);

subtest "quasi-curry" => sub {
	my @got;
	my $type = ArrayRef->create_child_type(
		name          => 'MyArrayRef',
		type_default  => sub { @got = @_; return $_ },
	);
	my $td = $type->type_default( 1 .. 5 );
	is( ref($td), 'CODE', 'quasi-curry worked' );
	is_deeply(
		$td->( bless {}, 'Local::Dummy' ),
		[ 1 .. 5 ],
		'quasi-curried arguments',
	);
	is_deeply(
		\@got,
		[ bless {}, 'Local::Dummy' ],
		'regular arguments',
	);
};

done_testing;