File: 02named-functions.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 (161 lines) | stat: -rw-r--r-- 2,990 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
=pod

=encoding utf-8

=head1 PURPOSE

Create named functions with C<fun>.

=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;
	
	fun foo {
		return { '@_' => \@_ };
	}

	fun bar () {
		return { '@_' => \@_ };
	}

	fun Example2::baz (my $x, $y) {
		return { '@_' => \@_, '$x' => \$x, '$y' => \$y };
	}

	fun ::quux ($x, $y, ...) {
		return { '@_' => \@_, '$x' => \$x, '$y' => \$y };
	}
	
	fun my $xyzzy ($x) {
		return { '$x' => \$x };
	}
	
	fun XYZZY ($x) {
		return $xyzzy->($x);
	}
	
	::ok(
		::exception { $xyzzy = 42 },
		'cannot rebind the lexical function'
	);
	
	{
		fun my $xyzzy () { 42 };
		::is($xyzzy->(), 42, 'can redefine lexical function in another scope');
	}
}

is_deeply(
	Example::foo(),
	{ '@_' => [] },
	'named function with no signature; called with empty list',
);

is_deeply(
	Example::foo(1..4),
	{ '@_' => [1..4] },
	'named function with no signature; called with arguments',
);

is_deeply(
	Example::bar(),
	{ '@_' => [] },
	'named function with empty signature',
);

#line 68
like(
	exception { Example::bar(1..4) },
	qr{\AExpected 0 parameters at \S+ line 69},
	'named function with empty signature throws exception if passed arguments',
);

is_deeply(
	Example2::baz(1..2),
	{ '@_' => [1..2], '$x' => \1, '$y' => \2 },
	'named function with positional parameters',
);

#line 81
like(
	exception { Example2::baz(1..4) },
	qr{\AExpected 2 parameters at \S+ line 82},
	'named function with positional parameters throws exception if passed too many arguments',
);

#line 88
like(
	exception { Example2::baz(1) },
	qr{\AExpected 2 parameters at \S+ line 89},
	'named function with positional parameters throws exception if passed too few arguments',
);

#line 95
is(
	exception { Example2::baz(undef, undef) },
	undef,
	'an explicit undef satisfies positional parameters',
);

is_deeply(
	quux(1..2),
	{ '@_' => [1..2], '$x' => \1, '$y' => \2 },
	'named function with positional parameters and yadayada',
);

#line 108
is(
	exception { quux(1..4) },
	undef,
	'named function with positional parameters and yadayada throws no exception if passed too many arguments',
);

#line 115
like(
	exception { quux(1) },
	qr{\AExpected at least 2 parameters at \S+ line 116},
	'named function with positional parameters and yadayada throws exception if passed too few arguments',
);

#line 121
is(
	exception { quux(undef, undef) },
	undef,
	'an explicit undef satisfies positional parameters with yadayada',
);

is_deeply(
	Example::XYZZY(42),
	{ '$x' => \42 },
	'lexical subs',
);

{ package Example3; use Kavorka; fun xxx { } };

is_deeply(
	[ Example3::xxx(1..3) ],
	[],
	'an empty function body returns nothing',
);

done_testing;