--- /dev/null
+#!perl
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+{
+ package Foo;
+ use Function::Parameters qw(:strict);
+
+ fun foo { return @_ }
+ method bar { return @_ }
+}
+
+is_deeply [Foo::foo()], [];
+is_deeply [Foo::foo(23, 42)], [23, 42];
+is_deeply [Foo->bar()], [];
+is_deeply [Foo->bar(23, 42)], [23, 42];
+
+done_testing;
$self->bar($arg);
}
-# method no_invocant_named_param($arg) {
-# $self->bar($arg);
-# }
+ method no_invocant_named_param(:$arg) {
+ $self->bar($arg);
+ }
};
is $@, '', 'compiles without invocant';
my $stuff = Stuff->new;
is( $stuff->no_invocant_class_type(Foo::Bar->new), 'Foo::Bar' );
-#is( $stuff->no_invocant_named_param(arg => Foo->new), 'Foo' );
+is( $stuff->no_invocant_named_param(arg => Foo->new), 'Foo' );
--- /dev/null
+#!perl
+use warnings FATAL => 'all';
+use strict;
+
+use Test::More;
+
+{
+ package Foo;
+
+ use Test::More;
+ use Test::Fatal;;
+ use Function::Parameters qw(:strict);
+
+ method formalize($text, :$justify = "left", :$case = undef) {
+ my %params;
+ $params{text} = $text;
+ $params{justify} = $justify;
+ $params{case} = $case if defined $case;
+
+ return \%params;
+ }
+
+ is_deeply( Foo->formalize( "stuff" ), { text => "stuff", justify => "left" } );
+
+ like exception { Foo->formalize( "stuff", wibble => 23 ) }, qr/\bnamed\b.+\bwibble\b/;
+
+ method foo( :$arg ) {
+ return $arg;
+ }
+
+ is( Foo->foo( arg => 42 ), 42 );
+ like exception { foo() }, qr/Not enough arguments/;
+
+
+ # Compile time errors need internal refactoring before I can get file, line and method
+ # information.
+ eval q{
+ method wrong( :$named, $pos ) {}
+ };
+ like $@, qr/\bpositional\b.+\$pos\b.+\bnamed\b.+\$named\b/;
+
+ eval q{
+ method wrong( $foo, :$named, $bar ) {}
+ };
+ like $@, qr/\bpositional\b.+\$bar\b.+\bnamed\b.+\$named\b/;
+
+ eval q{
+ method wrong( $foo, $bar = undef, :$named ) {}
+ };
+ like $@, qr/\boptional positional\b.+\$bar\b.+\brequired named\b.+\$named\b/;
+}
+
+
+done_testing();
--- /dev/null
+#!perl
+
+package Foo;
+use warnings FATAL => 'all';
+use strict;
+
+use Test::More tests => 1;
+use Test::Fatal;
+
+use Function::Parameters qw(:strict);
+
+method foo(:$name, :$value) {
+ return $name, $value;
+}
+
+like exception { Foo->foo(name => 42, value =>) }, qr/Not enough arguments/;
is( Stuff->some_optional(18), 18 );
-# # are named parameters optional by default?
-# method named_params(:$this, :$that) {}
-#
-# lives_ok { Stuff->named_params(this => 0) } 'can leave out some named params';
-# lives_ok { Stuff->named_params( ) } 'can leave out all named params';
+ method named_params(:$this = undef, :$that = undef) {}
+
+ is exception { Stuff->named_params(this => 0) }, undef, 'can leave out some named params';
+ is exception { Stuff->named_params( ) }, undef, 'can leave out all named params';
# are slurpy parameters optional by default?
--- /dev/null
+#!perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+
+{
+ package Stuff;
+
+ use Test::More;
+ use Test::Fatal;
+ use Function::Parameters qw(:strict);
+
+ method whatever($this) {
+ return $this;
+ }
+
+ is( Stuff->whatever(23), 23 );
+
+ like exception { Stuff->whatever() }, qr/Not enough arguments/;
+
+ method some_optional($that, $this = 22) {
+ return $that + $this
+ }
+
+ is( Stuff->some_optional(18), 18 + 22 );
+
+ like exception { Stuff->some_optional() }, qr/Not enough arguments/;
+}
+
+
+done_testing();
like $@, qr{\bslurpy_middle\b};
}
-# ok !eval q[fun slurpy_positional(:@that) { return \@that; }];
-# like $@, qr{slurpy parameter \@that cannot be named, use a reference instead};
-#
-# TODO: {
-# local $TODO = "error message incorrect inside an eval";
-#
-# like $@, qr{Stuff::};
-# like $@, qr{slurpy_positional\(\)};
-# }
+ ok !eval q[fun slurpy_positional(:@that) { return \@that; }];
+ like $@, qr{\bnamed\b.+\@that\b.+\barray\b};
ok !eval q[fun slurpy_two($this, @that, @other) { return $this, \@that, \@other }];
like $@, qr{\@that\b.+\@other\b};
--- /dev/null
+#!perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+use Function::Parameters qw(:strict);
+
+fun no_sig { return @_ }
+fun no_args() { return @_ }
+fun one_arg($foo) { return $foo }
+fun two_args($foo, $bar) { return ($foo, $bar) }
+fun array_at_end($foo, @stuff) { return ($foo, @stuff) }
+fun one_named(:$foo) { return $foo; }
+fun one_named_one_positional($bar, :$foo) { return($foo, $bar) }
+
+note "too many arguments"; {
+ is_deeply [no_sig(42)], [42];
+
+ ok !eval { no_args(42); 1 }, "no args";
+ like $@, qr{Too many arguments};
+
+ ok !eval { one_arg(23, 42); 1 }, "one arg";
+ like $@, qr{Too many arguments};
+
+ ok !eval { two_args(23, 42, 99); 1 }, "two args";
+ like $@, qr{Too many arguments};
+
+ is_deeply [array_at_end(23, 42, 99)], [23, 42, 99], "array at end";
+}
+
+
+note "with positionals"; {
+ is one_named(foo => 42), 42;
+ is one_named(foo => 23, foo => 42), 42;
+
+
+ is_deeply [one_named_one_positional(23, foo => 42)], [42, 23];
+ is_deeply [one_named_one_positional(23, foo => 42, foo => 23)], [23, 23];
+}
+
+
+done_testing;
#!perl
use strict;
use warnings FATAL => 'all';
-use Test::More tests => 21;
+use Test::More tests => 23;
use Test::Fatal;
use Function::Parameters qw(:strict);
is(exception { $meth->($o, 'foo', 42, 23) }, undef);
}
-#{
-# eval 'my $meth = method (:$foo, :@bar) { }';
-# like $@, qr/arrays or hashes cannot be named/i;
-#
-# eval 'my $meth = method ($foo, @bar, :$baz) { }';
-# like $@, qr/named parameters can not be combined with slurpy positionals/i;
-#}
+{
+ eval 'my $meth = method (:$foo, :@bar) { }';
+ like $@, qr/\bnamed\b.+\bbar\b.+\barray\b/;
+
+ eval 'my $meth = method ($foo, @bar, :$baz) { }';
+ like $@, qr/\bbar\b.+\bbaz\b/;
+}
--- /dev/null
+#!perl
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+{
+ package Foo;
+
+ use Function::Parameters qw(:strict);
+
+ method new($class:) { bless {}, $class }
+ method bar (:$baz = 42) { $baz }
+}
+
+my $o = Foo->new;
+is($o->bar, 42);
+is($o->bar(baz => 0xaffe), 0xaffe);
+
+done_testing;
method new($class:) { bless {}, $class }
-# method m1(:$bar!) { }
-# method m2(:$bar?) { }
-# method m3(:$bar ) { }
+ method m1(:$bar ) { }
+ method m2(:$bar = undef) { }
+ method m3(:$bar ) { }
-# method m4( $bar!) { }
+ method m4( $bar ) { }
method m5( $bar = undef ) { }
method m6( $bar ) { }
}
my $foo = Foo->new;
-#is(exception { $foo->m1(bar => undef) }, undef, 'Explicitly pass undef to positional required arg');
-#is(exception { $foo->m2(bar => undef) }, undef, 'Explicitly pass undef to positional explicit optional arg');
-#is(exception { $foo->m3(bar => undef) }, undef, 'Explicitly pass undef to positional implicit optional arg');
+is(exception { $foo->m1(bar => undef) }, undef, 'Explicitly pass undef to positional required arg');
+is(exception { $foo->m2(bar => undef) }, undef, 'Explicitly pass undef to positional explicit optional arg');
+is(exception { $foo->m3(bar => undef) }, undef, 'Explicitly pass undef to positional implicit optional arg');
-#is(exception { $foo->m4(undef) }, undef, 'Explicitly pass undef to required arg');
+is(exception { $foo->m4(undef) }, undef, 'Explicitly pass undef to required arg');
is(exception { $foo->m5(undef) }, undef, 'Explicitly pass undef to explicit required arg');
is(exception { $foo->m6(undef) }, undef, 'Explicitly pass undef to implicit required arg');