root/t/perl5/method.t

Revision 22090, 2.0 kB (checked in by lwall, 4 months ago)

[t] clean up various calls to try, system, etc

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1use v6;
2
3use Test;
4
5plan(13);
6
7unless (try { eval("1", :lang<perl5>) }) {
8    skip_rest;
9    exit;
10}
11
12eval(q/
13#line 16 method.t
14package FooBar;
15our $VERSION = '6.0';
16print '';
17
18sub new {
19    bless {}, __PACKAGE__;
20}
21
22sub foo {
23    return 'foo';
24}
25
26sub echo {
27    my ($self, $what) = @_;
28#print "==> echo got $what\n";
29    return $what;
30}
31
32sub callcode {
33    my ($self, $code) = @_;
34#print "==> callcode got $code\n";
35    return eval { $code->($self) };
36}
37
38sub asub {
39    return sub { return "asub" };
40}
41
42sub submany {
43    return sub { ("many", "return") };
44}
45
46sub many {
47    return ("many", "return") ;
48}
49
50sub modify_array {
51    my ($class, $val) = @_;
52    $val->[0] = 99;
53}
54
55# takes an object and invoke me on that
56sub invoke {
57    my ($class, $obj) = @_;
58    $obj->me ('invoking');
59}
60
61/, :lang<perl5>);
62
63{
64    my $r = eval("FooBar->VERSION", :lang<perl5>);
65    is($r, '6.0', "class method");
66}
67
68my $obj;
69
70{
71    $obj = eval("FooBar->new", :lang<perl5>);
72    isa_ok($obj, 'FooBar', "blessed");
73    like($obj, rx:Perl5/FooBar/, "blessed");
74}
75
76{
77    is($obj.foo, 'foo', 'invoke method');
78}
79
80{
81    my $r = $obj.echo("bar");
82    is($r, 'bar', 'invoke method with pugs arg');
83}
84
85{
86    my $r = $obj.asub;
87
88    flunk('isa_ok vs Perl5 not yet defined');
89    # isa_ok($r, 'CODE', "returning a coderef");
90
91    is($r.(), 'asub', 'invoking p5 coderef');
92    my $rr = $obj.callcode($r);
93    is($rr, 'asub', 'invoke with p5 coderef');
94}
95
96{
97    my @r = $obj.many;
98    is(@r.elems, 2);
99}
100
101{
102    my $r = $obj.submany;
103    my @r = $r.();
104    is(@r.elems, 2);
105}
106
107{
108    my $callback = { "baz" };
109    my $r = $obj.callcode($callback);
110    is($r, 'baz', 'invoke method with callback');
111}
112
113{
114    class Foo6 {
115        method me (Class|Foo6 $class: $arg) { 'Foo6'~$arg };
116    };
117    my $obj6 = Foo6.new;
118    $obj = eval("FooBar->new", :lang<perl5>);
119    is($obj.invoke($obj6), 'Foo6invoking', 'invoke pugs method from p5');
120}
121
122{
123    my @rw = (1, 2, 3);
124    $obj.modify_array(VAR @rw);
125    is(@rw[0], 99, 'modify a scalar ref');
126}
Note: See TracBrowser for help on using the browser.