File: basic.t

package info (click to toggle)
libtype-tiny-perl 1.000004-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,836 kB
  • ctags: 583
  • sloc: perl: 7,602; makefile: 26
file content (127 lines) | stat: -rw-r--r-- 2,990 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
=pod

=encoding utf-8

=head1 PURPOSE

Checks Type::Registry works.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

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

{
	package Local::Pkg1;
	use Type::Registry "t";
	
	::is(t(), Type::Registry->for_me, 'Type::Registry->for_me works');
	::is(t(), Type::Registry->for_class(__PACKAGE__), 'Type::Registry->for_class works');
	
	t->add_types(-Standard);
	
	::like(
		::exception { t->add_types(-MonkeyNutsAndChimpanzeeRaisins) },
		qr{^Types::MonkeyNutsAndChimpanzeeRaisins is not a type library},
		'cannot add non-existant type library to registry',
	);
	
	t->alias_type(Int => "Integer");
	
	::like(
		::exception { t->alias_type(ChimpanzeeRaisins => "ChimpSultanas") },
		qr{^Expected existing type constraint name},
		'cannot alias non-existant type in registry',
	);
	
	::ok(t->Integer == Types::Standard::Int(), 'alias works');
	::ok(t("Integer") == Types::Standard::Int(), 'alias works via simple_lookup');
	::ok(t("Integer[]") == Types::Standard::Int(), 'alias works via lookup');
}

{
	package Local::Pkg2;
	use Type::Registry "t";
	t->add_types(-Standard => [ -types => { -prefix => 'XYZ_' } ]);
	
	::ok(t->XYZ_Int == Types::Standard::Int(), 'prefix works');
}

ok(
	exception { Local::Pkg2::t->lookup("Integer") },
	'type registries are separate',
);

my $r = Type::Registry->for_class("Local::Pkg1");

should_pass([1, 2, 3], $r->lookup("ArrayRef[Integer]"));
should_fail([1, 2, 3.14159], $r->lookup("ArrayRef[Integer]"));

like(
	exception { $r->lookup('%foo') },
	qr{^Unexpected token in primary type expression; got '\%foo'},
	'type constraint invalid syntax',
);

like(
	exception { $r->lookup('MonkeyNuts') },
	qr{^MonkeyNuts is not a known type constraint },
	'type constraint unknown type',
);

like(
	exception { $r->MonkeyNuts },
	qr{^Can't locate object method "MonkeyNuts" via package},
	'type constraint unknown type (as method call)',
);

is(
	$r->lookup('MonkeyNuts::')->class,
	'MonkeyNuts',
	'class type',
);

require Type::Tiny::Enum;
$r->add_type('Type::Tiny::Enum'->new(values => [qw/Monkey Nuts/]), 'MonkeyNuts');
my $mn = $r->lookup('MonkeyNuts');
should_pass('Monkey', $mn);
should_pass('Nuts', $mn);
should_fail('Cashews', $mn);

use Type::Utils qw(dwim_type role_type class_type);

is(
	dwim_type('MonkeyNuts')->class,
	'MonkeyNuts',
	'DWIM - class type',
);

is(
	dwim_type('MonkeyNuts', does => 1)->role,
	'MonkeyNuts',
	'DWIM - role type',
);

is(
	dwim_type('ArrayRef[MonkeyNuts | Foo::]', does => 1)->inline_check('$X'),
	Types::Standard::ArrayRef()->parameterize(role_type({role=>"MonkeyNuts"}) | class_type({class=>"Foo"}))->inline_check('$X'),
	'DWIM - complex type',
);

done_testing;