Skip to content

Commit 2e91c7c

Browse files
committed
Test nqp::setinvokespec with invocationHandler.
1 parent a86c520 commit 2e91c7c

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

t/nqp/70-invokespec.t

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
plan(4);
1+
plan(11);
22
class Foo {
33
has $!here_we_keep_the_code_ref;
44
has $!other_place_we_could_keep_the_code_ref_in;
@@ -30,3 +30,37 @@ class Baz {
3030

3131
ok(nqp::isinvokable(Foo) == 1, "nqp::isinvokable works on objects that have a set invokespec");
3232
ok(nqp::isinvokable(Baz) == 0, "nqp::isinvokable works on objects that don't have a set invokespec");
33+
34+
class Second {
35+
has $!attr;
36+
has $!ret;
37+
method attr() {$!attr}
38+
method ret() {$!ret}
39+
}
40+
41+
my $called;
42+
my $called_arg;
43+
my $called_name;
44+
45+
nqp::setinvokespec(Second, nqp::null(), nqp::null_s(), -> $invokee, $arg1, :$named {
46+
$called := $invokee;
47+
$called_arg := $arg1;
48+
$called_name := $named;
49+
$invokee.ret;
50+
});
51+
52+
my $second := Second.new(attr => "hello", ret => "peculiar");
53+
54+
ok($second("world", :named('brave new')) eq 'peculiar','correct value from invocation');
55+
56+
ok($called.attr eq 'hello', 'got right invokee to invocation handler');
57+
ok($called_arg eq 'world', 'got positional');
58+
ok($called_name eq 'brave new', 'got named');
59+
60+
ok(nqp::isinvokable($second) == 1, "nqp::isinvokable on object with 2nd form of setinvokespec");
61+
62+
my $differently := Second.new(attr => "hello", ret => "not yet again");
63+
64+
my @args := nqp::list("again");
65+
ok($differently(|@args) eq "not yet again", "calling with slurpy args");
66+
ok($called_arg eq 'again', "...and got right arg");

0 commit comments

Comments
 (0)