File: 50types.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 (119 lines) | stat: -rw-r--r-- 2,667 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
=pod

=encoding utf-8

=head1 PURPOSE

Check that type constraints 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->alias_type( 'Int' => 'Count' );
		t->add_type( t->Int->create_child_type(name => 'Int2', constraint => sub { 1 }) );
	};
	
	# We need to test a non-inlinable type constraint.
	::ok( not t->Int2->can_be_inlined );
	
	fun foo ( Int $x )   { return $x }
	fun bar ( Count $x ) { return $x }
	fun baz ( Int2 $x )  { return $x }
	
	fun foo_array ( Int @y )   { return \@y }
	fun bar_array ( Count @y ) { return \@y }
	fun baz_array ( Int2 @y )  { return \@y }

	fun foo_arrayref ( slurpy ArrayRef[Int] $z )   { return $z }
	fun bar_arrayref ( slurpy ArrayRef[Count] $z ) { return $z }
	fun baz_arrayref ( slurpy ArrayRef[Int2] $z )  { return $z }
}

is( Example::foo(42), 42 );

like(
	exception { Example::foo(3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int"},
);

is( Example::bar(42), 42 );

like(
	exception { Example::bar(3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int"},
);

is( Example::baz(42), 42 );

like(
	exception { Example::baz(3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int2"},
);

is_deeply( Example::foo_array(666,42), [666,42] );

like(
	exception { Example::foo_array(666,3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int"},
);

is_deeply( Example::bar_array(666,42), [666,42] );

like(
	exception { Example::bar_array(666,3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int"},
);

is_deeply( Example::baz_array(666,42), [666,42] );

like(
	exception { Example::baz_array(666,3.14159) },
	qr{^Value "3.14159" did not pass type constraint "Int2"},
);

is_deeply( Example::foo_arrayref(666,42), [666,42] );

like(
	exception { Example::foo_arrayref(666,3.14159) },
	qr{^Reference \[.+\] did not pass type constraint "ArrayRef\[Int\]"},
);

is_deeply( Example::bar_arrayref(666,42), [666,42] );

like(
	exception { Example::bar_arrayref(666,3.14159) },
	qr{^Reference \[.+\] did not pass type constraint "ArrayRef\[Int\]"},
);

is_deeply( Example::baz_arrayref(666,42), [666,42] );

like(
	exception { Example::baz_arrayref(666,3.14159) },
	qr{^Reference \[.+\] did not pass type constraint "ArrayRef\[Int2\]"},
);

done_testing;