Author: ruoso
Date: 2009-09-19 15:45:20 +0200 (Sat, 19 Sep 2009)
New Revision: 28306
Added:
t/spec/S06-other/introspection.t
Modified:
docs/Perl6/Spec/S06-routines.pod
Log:
[S06] adds $multi.push($candidate)
[spectest] adds a spec test for the introspection S06 section
Modified: docs/Perl6/Spec/S06-routines.pod
===================================================================
--- docs/Perl6/Spec/S06-routines.pod 2009-09-19 13:38:22 UTC (rev 28305)
+++ docs/Perl6/Spec/S06-routines.pod 2009-09-19 13:45:20 UTC (rev 28306)
@@ -2947,6 +2947,14 @@
This method returns a (potentially lazy) list of the candidates that match the
given
capture.
+=item .push($candidate)
+
+Adds $candidate to the list of candidates for this multi, calling this
+method in an only routine should result in a failure. It is also
+accepted for multis declared in the source code to finalize the list
+of candidates and also return a failure here. But multis created by
+calling Multi.new() should be able add candidates at run-time.
+
=back
=head3 Signature
Added: t/spec/S06-other/introspection.t
===================================================================
--- t/spec/S06-other/introspection.t (rev 0)
+++ t/spec/S06-other/introspection.t 2009-09-19 13:45:20 UTC (rev 28306)
@@ -0,0 +1,65 @@
+use v6;
+
+use Test;
+
+plan 20;
+
+#?rakudo: 20 skip "routine introspection NYI"
+
+# L<S06/Other matters/Introspection>
+
+# introspecting only subs
+only sub only-sub($a, $b) { "only" };
+
+# .candidates
+is(&only-sub.candidates.elems,1,"an only subs lists itself in the
.candidates");
+is(&only-sub.candidates[0].(1,2),"only","an only subs lists itself in the
.candidates");
+
+# .cando
+is(&only-sub.cando(\(1,2)).elems,1,"an only sub implements .cando");
+is(&only-sub.cando(\(1,2)).[0].(1,2),"only","an only sub implements .cando");
+
+# .signature
+ok(&only-sub.signature ~~ \(1,2),"an only sub implements .signature");
+
+# introspecting multi subs
+multi sub multi-sub(1,2) { "m1" };
+multi sub multi-sub(1) { "m2" };
+multi sub multi-sub() { "m3" };
+
+# .candidates
+is(&multi-sub.candidates.elems,3,"a multi sub returns all its candidates");
+
+# .cando
+is(&multi-sub.cando(\(1,2)).[0].(1,2),"m1","you can invoke through
introspection");
+is(&multi-sub.cando(\(1)).[0].(1),"m2","you can invoke through introspection");
+is(&multi-sub.cando(\()).[0].(),"m3","you can invoke through introspection");
+
+# .signature
+my $sig = &multi-sub.signature;
+ok($sig ~~ \(1,2),"junction sig matches first candidate");
+ok($sig ~~ \(1),"junction sig matches second candidate");
+ok($sig ~~ \(), "junction sig matches third candidate");
+
+# creating a multi in runtime
+my $multi = Multi.new();
+ok($multi,"One can create a new multi at run time");
+
+# let's populate this runtime multi.
+$multi.push(sub (1,2) { "m1" });
+$multi.push(sub (1) { "m2" });
+$multi.push(sub () { "m3" });
+
+# .candidates
+is($multi.candidates.elems,3,"runtime multi sub returns all its candidates");
+
+# .cando
+is($multi.cando(\(1,2)).[0].(1,2),"m1","you can invoke through introspection");
+is($multi.cando(\(1)).[0].(1),"m2","you can invoke through introspection");
+is($multi.cando(\()).[0].(),"m3","you can invoke through introspection");
+
+# .signature
+my $sig = $multi.signature;
+ok($sig ~~ \(1,2),"junction sig matches first candidate");
+ok($sig ~~ \(1),"junction sig matches second candidate");
+ok($sig ~~ \(), "junction sig matches third candidate");