File: import-params.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 (113 lines) | stat: -rw-r--r-- 2,120 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
=pod

=encoding utf-8

=head1 PURPOSE

Checks C<< of >> and C<< where >> import options works.

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

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

BEGIN {
	package MyTypes;
	use Type::Library -base;
	$INC{'MyTypes.pm'} = __FILE__;
	
	__PACKAGE__->add_type(
		name                  => 'Ref',
		constraint            => sub { ref $_[0] },
		constraint_generator  => sub {
			my $x = shift;
			sub { ref $_[0] eq $x };
		},
	);
};

use MyTypes 'Ref';

should_pass([], Ref);
should_pass({}, Ref);
should_pass(sub {}, Ref);
should_fail(1, Ref);

should_pass([], Ref['ARRAY']);
should_fail({}, Ref['ARRAY']);
should_fail(sub {}, Ref['ARRAY']);
should_fail(1, Ref['ARRAY']);

should_pass({}, Ref['HASH']);
should_fail([], Ref['HASH']);
should_fail(sub {}, Ref['HASH']);
should_fail(1, Ref['HASH']);

use MyTypes Ref => { of => 'HASH', -as => 'HashRef' };

should_pass({}, HashRef);
should_fail([], HashRef);
should_fail(sub {}, HashRef);
should_fail(1, HashRef);

use MyTypes Ref => {
	where => sub { ref $_[0] eq 'ARRAY' or ref $_[0] eq 'HASH' },
	-as => 'ContainerRef',
};

should_pass({}, ContainerRef);
should_pass([], ContainerRef);
should_fail(sub {}, ContainerRef);
should_fail(1, ContainerRef);

use MyTypes is_Ref => { of => 'HASH', -as => 'is_HashRef' };

ok  is_HashRef({});
ok !is_HashRef([]);
ok !is_HashRef(sub {});
ok !is_HashRef(1);

BEGIN {
	package My::Types::Two;
	use Type::Library 1.011005
		-utils,
		-extends => [ 'Types::Standard' ],
		-declare => 'JSONCapable';
	
	declare JSONCapable,
		as Undef
		|  ScalarRef[ Enum[ 0..1 ] ]
		|  Num
		|  Str
		|  ArrayRef[ JSONCapable ]
		|  HashRef[ JSONCapable ]
		;
}

use My::Types::Two 'is_JSONCapable';

my $var = {
	foo => 1,
	bar => [ \0, "baz", [] ],
};

ok is_JSONCapable $var;

done_testing;