Codebase list libfunction-parameters-perl / 98478af
import all tests from Fun Lukas Mai 11 years ago
12 changed file(s) with 310 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2020 t/eating_strict_error.t
2121 t/eating_strict_error_2.fail
2222 t/elsewhere.t
23 t/foreign/Fun/anon.t
24 t/foreign/Fun/basic.t
25 t/foreign/Fun/closure-proto.t
26 t/foreign/Fun/compile-time.t
27 t/foreign/Fun/defaults.t
28 t/foreign/Fun/name.t
29 t/foreign/Fun/package.t
30 t/foreign/Fun/recursion.t
31 t/foreign/Fun/slurpy-syntax-errors.t
32 t/foreign/Fun/slurpy.t
33 t/foreign/Fun/state.t
2334 t/foreign/Method-Signatures-Simple/02-use.t
2435 t/foreign/Method-Signatures-Simple/03-config.t
2536 t/foreign/Method-Signatures/anon.t
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 my $fun = fun ($x, $y) { $x * $y };
8
9 is($fun->(3, 4), 12);
10
11 my $fun2 = fun ($z, $w = 10) { $z / $w };
12
13 is($fun2->(60), 6);
14
15 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 fun mul ($x, $y) {
8 return $x * $y;
9 }
10
11 is(mul(3, 4), 12);
12
13 fun sum (@nums) {
14 my $sum;
15 for my $num (@nums) {
16 $sum += $num;
17 }
18 return $sum;
19 }
20
21 is(sum(1, 2, 3, 4), 10);
22
23 {
24 package Foo;
25 use Function::Parameters;
26 fun foo { }
27 }
28
29 ok(exists $Foo::{foo});
30
31 done_testing;
0 #!perl
1
2 use strict;
3 use warnings FATAL => 'all';
4 use Test::More;
5
6 use Function::Parameters;
7
8 {
9 my $x = 10;
10
11 fun bar ($y) {
12 $x * $y
13 }
14 }
15
16 is(bar(3), 30);
17
18 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 is(foo(), "FOO");
8
9 fun foo { "FOO" }
10
11 done_testing;
0 #!perl
1 use strict;
2 use warnings;
3 use Test::More;
4
5 use Function::Parameters;
6
7 fun foo ($x, $y = 5) {
8 return $x + $y;
9 }
10
11 is(foo(3, 4), 7);
12 is(foo(3), 8);
13 {
14 my $warning;
15 local $SIG{__WARN__} = sub { $warning = $_[0] };
16 is(foo, 5);
17 like($warning, qr/Use of uninitialized value \$x in addition \(\+\)/);
18 }
19
20 fun bar ($baz, $quux = foo(1) * 2, $blorg = sub { return "ran sub, got " . $_[0] }) {
21 $blorg->($baz + $quux);
22 }
23
24 is(bar(3, 4, sub { $_[0] }), 7);
25 is(bar(5, 6), "ran sub, got 11");
26 is(bar(7), "ran sub, got 19");
27 {
28 my $warning;
29 local $SIG{__WARN__} = sub { $warning = $_[0] };
30 is(bar, "ran sub, got 12");
31 like($warning, qr/Use of uninitialized value \$baz in addition \(\+\)/);
32 }
33
34 fun baz ($a, $b = our $FOO) {
35 return "$a $b";
36 }
37
38 {
39 no warnings 'misc'; # 'not imported' warning because we use $FOO later
40 eval '$FOO';
41 like($@, qr/Global symbol "\$FOO" requires explicit package name/, "doesn't leak scope");
42 }
43
44 our $FOO = "abc";
45 is(baz("123"), "123 abc");
46
47 fun goorch ($x, $y = []) {
48 return $y
49 }
50
51 my $goorch_y_1 = goorch( 10 );
52 my $goorch_y_2 = goorch( 10 );
53
54 isnt($goorch_y_1, $goorch_y_2, '... not the same reference');
55
56 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Carp;
6
7 my $file = __FILE__;
8 my $line = __LINE__;
9
10 {
11 package Foo;
12 use Function::Parameters;
13 fun foo ($x, $y) {
14 Carp::confess "$x $y";
15 }
16
17 eval {
18 foo("abc", "123");
19 };
20
21 my $line_confess = $line + 6;
22 my $line_foo = $line + 10;
23
24 ::like($@, qr/^abc 123 at $file line $line_confess\.?\n\tFoo::foo\('abc', 123\) called at $file line $line_foo/);
25 }
26
27 SKIP: { skip "Sub::Name required", 1 unless eval { require Sub::Name };
28
29 {
30 package Bar;
31 use Function::Parameters;
32 *bar = Sub::Name::subname(bar => fun ($a, $b) { Carp::confess($a + $b) });
33
34 eval {
35 bar(4, 5);
36 };
37
38 my $line_confess = $line + 24;
39 my $line_bar = $line + 27;
40
41 ::like($@, qr/^9 at $file line $line_confess\.?\n\tBar::bar\(4, 5\) called at $file line $line_bar/);
42 }
43
44 }
45
46 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 fun Foo::foo ($x, $y) {
8 $x + $y;
9 }
10
11 ok(!main->can('foo'));
12 ok(Foo->can('foo'));
13 is(Foo::foo(1, 2), 3);
14
15 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 BEGIN {
6 if (!eval { require 5.016; 1 }) {
7 plan skip_all => "This test requires 5.16";
8 }
9 }
10
11 use 5.016;
12
13 use Function::Parameters;
14
15 fun fact ($n) {
16 if ($n < 2) {
17 return 1;
18 }
19 return $n * __SUB__->($n - 1);
20 }
21
22 is(fact(5), 120);
23
24 is(fun ($n = 8) { $n < 2 ? 1 : $n * __SUB__->($n - 1) }->(), 40320);
25
26 fun fact2 ($n) {
27 if ($n < 2) {
28 return 1;
29 }
30 return $n * fact2($n - 1);
31 }
32
33 is(fact2(5), 120);
34
35 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 {
8 eval 'fun ( $foo, @bar, $baz ) { return [] }';
9 ok $@, '... got an error';
10 }
11
12 {
13 eval 'fun ( $foo, %bar, $baz ) { return {} }';
14 ok $@, '... got an error';
15 }
16
17 {
18 eval 'fun ( $foo, @bar, %baz ) { return [] }';
19 ok $@, '... got an error';
20 }
21
22 {
23 eval 'fun ( $foo, %bar, @baz ) { return {} }';
24 ok $@, '... got an error';
25 }
26
27 done_testing;
0 #!perl
1 use strict;
2 use warnings FATAL => 'all';
3 use Test::More;
4
5 use Function::Parameters;
6
7 fun test_array ( $foo, @bar ) {
8 return [ $foo, @bar ];
9 }
10
11 fun test_hash ( $foo, %bar ) {
12 return { foo => $foo, %bar };
13 }
14
15 is_deeply( test_array( 1, 2 .. 10 ), [ 1, 2 .. 10 ], '... slurpy array worked' );
16 is_deeply( test_hash( 1, ( two => 2, three => 3 ) ), { foo => 1, two => 2, three => 3 }, '... slurpy hash worked' );
17
18 done_testing;
0 #!perl
1
2 use strict;
3 use warnings FATAL => 'all';
4 use Test::More;
5
6 use 5.10.0;
7 use Function::Parameters;
8
9 fun bar ($y) {
10 state $x = 10;
11 $x * $y;
12 }
13
14 is(bar(3), 30);
15
16 done_testing;