File: multisig-gotonext.t

package info (click to toggle)
libtype-tiny-perl 2.004000-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: perl: 14,858; makefile: 2; sh: 1
file content (114 lines) | stat: -rw-r--r-- 2,148 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
=pod

=encoding utf-8

=head1 PURPOSE

Test L<Type::Params> C<multi> signatures work with C<goto_next>.

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENCE

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

BEGIN {
	package MyTest;
	use Types::Common -sigs, -types;
	signature_for f => (
		method   => Str,
		multiple => [
			{
				named => [
					x     => Num,
					y     => Num,
					note  => Str, { default => '(no note)' },
				],
				named_to_list => 1,
			},
			{
				positional => [ Num, Num, Str, { default => '(no note)' } ],
			},
			{
				positional => [ Tuple[ Num, Num ], Str, { default => '(no note)' } ],
				goto_next  => sub {
					my ( $class, $xy, $note ) = @_;
					my ( $x, $y ) = @{ $xy };
					return ( $class, $x, $y, $note );
				},
			},
		],
	);
	sub f {
		my ( $class, $x, $y, $note ) = @_;
		$class eq __PACKAGE__ or die;
		return {
			x     => $x,
			y     => $y,
			note  => $note,
		};
	}
}

is_deeply(
	MyTest->f( x => 1, y => 2, note => 'foo' ),
	{ x => 1, y => 2, note => 'foo' },
	"MyTest->f( x => 1, y => 2, note => 'foo' )",
);

is_deeply(
	MyTest->f( x => 3, y => 4 ),
	{ x => 3, y => 4, note => '(no note)' },
	"MyTest->f( x => 3, y => 4 )",
);

is_deeply(
	MyTest->f( { x => 1, y => 2, note => 'foo' } ),
	{ x => 1, y => 2, note => 'foo' },
	"MyTest->f( { x => 1, y => 2, note => 'foo' } )",
);

is_deeply(
	MyTest->f( { x => 3, y => 4 } ),
	{ x => 3, y => 4, note => '(no note)' },
	"MyTest->f( { x => 3, y => 4 } )",
);

is_deeply(
	MyTest->f( 1, 2, 'foo' ),
	{ x => 1, y => 2, note => 'foo' },
	"MyTest->f( 1, 2, 'foo' )",
);

is_deeply(
	MyTest->f( 3, 4 ),
	{ x => 3, y => 4, note => '(no note)' },
	"MyTest->f( 3, 4 )",
);

is_deeply(
	MyTest->f( [ 5, 6 ], 'foo' ),
	{ x => 5, y => 6, note => 'foo' },
	"MyTest->f( [ 5, 6 ], 'foo' )",
);

is_deeply(
	MyTest->f( [ 7, 8 ] ),
	{ x => 7, y => 8, note => '(no note)' },
	"MyTest->f( [ 7, 8 ] )",
);

done_testing;