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

=encoding utf-8

=head1 PURPOSE

Test L<Type::Params> C<goto_next> option.

=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 Test::More;
use Test::Fatal;

use Type::Params qw( compile_named_oo );
use Types::Standard -types;

{
	sub _foobar {
		$_ = my $arg = shift;
		wantarray
			? ( $arg->foo, $arg->bar )
			: [ $arg->foo, $arg->bar ];
	}
	
	my $sig;
	sub foobar {
		unshift @_, \&_foobar;
		goto( $sig ||= compile_named_oo { goto_next => 1 }, foo => Bool, bar => Int );
	}
}

subtest "goto_next => 1" => sub {
	
	is_deeply(
		[ foobar( foo => [], bar => 42 ) ],
		[ !!1, 42 ],
		'list context',
	);
	
	is_deeply(
		scalar( foobar( foo => [], bar => 42 ) ),
		[ !!1, 42 ],
		'scalar context',
	);
};

{
	sub _foobar2 {
		$_ = my $arg = shift;
		wantarray
			? ( $arg->foo, $arg->bar )
			: [ $arg->foo, $arg->bar ];
	}
	
	my $sig;
	sub foobar2 {
		goto( $sig ||= compile_named_oo { goto_next => \&_foobar2 }, foo => Bool, bar => Int );
	}
}

subtest "goto_next => CODEREF" => sub {
	
	is_deeply(
		[ foobar2( foo => [], bar => 42 ) ],
		[ !!1, 42 ],
		'list context',
	);
	
	is_deeply(
		scalar( foobar2( foo => [], bar => 42 ) ),
		[ !!1, 42 ],
		'scalar context',
	);
};

{
	my $_foobar3 = sub {
		$_ = my $arg = shift;
		wantarray
			? ( $arg->foo, $arg->bar )
			: [ $arg->foo, $arg->bar ];
	};
	
	*foobar3 = compile_named_oo { package => 'main', subname => 'foobar3', goto_next => $_foobar3 },
		foo => Bool, bar => Int;
}

subtest "goto_next => CODEREF (assign to glob)" => sub {
	
	is_deeply(
		[ foobar3( foo => [], bar => 42 ) ],
		[ !!1, 42 ],
		'list context',
	);
	
	is_deeply(
		scalar( foobar3( foo => [], bar => 42 ) ),
		[ !!1, 42 ],
		'scalar context',
	);
	
	is( $_->{'~~caller'}, 'main::foobar3', 'meta' );
};

done_testing;