root/t/packages/autoload.t

Revision 21734, 5.4 kB (checked in by lwall, 5 months ago)

various buglets, STD now parses 94% of t/

  • 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
5# various tests derived from L<S10/Autoloading>
6plan 1;
7skip_rest 'XXX - All of this is outdated - Need a total rewrite based on CANDO - Any takers?';
8exit;
9
10plan 38;
11
12if $?PUGS_BACKEND ne "BACKEND_PUGS" {
13  skip_rest "PIL2JS and PIL-Run do not support eval() yet.";
14  exit;
15}
16
17package OughtaLoad {
18    sub AUTOLOAD(*@args) {
19        "\&{$_}({ @args.map:{"'$_'"}.join(", ") })"
20    };
21}
22
23# currently, $_ is passed in as a global as per spec!
24$_ = "test";
25my $x = OughtaLoad::AUTOLOAD(1,2,3);
26is($x, q[&test('1', '2', '3')], "sanity");
27
28lives_ok { $x = OughtaLoad::test(2,3,4) },
29    "AUTOLOAD", :todo<feature>;
30
31package OughtaWork {
32    our $s = 0;
33    sub AUTOSCALAR($_) { my $v = "\$$_ number {++$s}";
34                     eval "our \$$_ := \$v";
35                     \$v }
36    our $a = 0;
37    sub AUTOARRAY($_)  { my @v = ( "auto", $_, ++$a );
38                     eval "our @$_ := @v";
39                     \@v }
40    our $h = 0;
41    sub AUTOHASH($_)   { my %v = ( auto => $_, num => ++$h );
42                     eval "our %$_ := %v";
43                     \%v }
44    our $u = 0;
45    sub AUTOSUB($_)    { my $x = "\&{$_} number {++$u}";
46                     my $sub = sub { $x };
47                     eval "our &$_ := \$sub";
48                     return $sub;
49                   }
50    our $m = 0;
51    method AUTOMETH($_) { my $x = "{self} {$_}.number {++$m}";
52                     my $method = sub ($self:) { "{$self}.$x" };
53                     eval "our &$_ := \$method";
54                     return $method;
55                   }
56}
57
58# however, the use of $_ in the above is because the first argument is
59# bound to $_ via "default invocants" (which seems to not be
60# working... hmm)
61$_ = "poison";
62
63# scalar
64$x = OughtaWork::AUTOSCALAR("test");
65is($x, q[$test number 1], "AUTOSCALAR sanity test");
66
67lives_ok { $x = $OughtaWork::foo }, "AUTOSCALAR - first";
68is($x, q[$foo number 2], "Returns correct var", :todo<feature>);
69
70$x="";
71lives_ok { $x = $OughtaWork::foo }, "AUTOSCALAR - repeat";
72is($x, q[$foo number 2], "AUTOSCALAR only called once", :todo<feature>);
73
74lives_ok { $x = $OughtaWork::bar }, "AUTOSCALAR - second";
75is($x, q[$bar number 3], "Returns correct var", :todo<feature>);
76
77
78# array
79my $a = OughtaWork::AUTOARRAY("test");
80is($a.join(","), q[auto,test,1], "AUTOARRAY sanity test");
81
82my @a;
83lives_ok { @a = @OughtaWork::foo }, "AUTOARRAY - first";
84is(@a.join(","), q[auto,foo,2], "Returns correct var", :todo<feature>);
85
86@a=();
87lives_ok { @a = @OughtaWork::foo }, "AUTOARRAY - repeat";
88is(@a.join(","), q[auto,foo,2], "AUTOARRAY only called once", :todo<feature>);
89
90lives_ok { @a = @OughtaWork::bar }, "AUTOARRAY - second";
91is(@a.join(","), q[auto,bar,3], "Returns correct var", :todo<feature>);
92
93
94# hash
95my %h = OughtaWork::AUTOHASH("test");
96is(%h.kv.sort.join(","), q[1,auto,num,test], "AUTOHASH sanity test");
97
98lives_ok { %h = %OughtaWork::foo }, "AUTOHASH - first";
99is(%h.kv.sort.join(","), q[2,auto,foo,num], "Returns correct var", :todo<feature>);
100
101%h=();
102lives_ok { %h = %OughtaWork::foo }, "AUTOHASH - repeat";
103is(%h.kv.sort.join(","), q[2,auto,foo,num], "AUTOHASH only called once", :todo<feature>);
104
105lives_ok { %h = %OughtaWork::bar }, "AUTOHASH - second";
106is(%h.kv.sort.join(","), q[3,auto,bar,num], "Returns correct var", :todo<feature>);
107
108
109# sub
110my $s = OughtaWork::AUTOSUB("test");
111my $v = $s();
112is($v, q[&test number 1], "AUTOSUB sanity test");
113
114$v="";
115ok eval(q{ $s = &OughtaWork::foo; $v = $s(); }),
116        "AUTOSUB - first", :todo<feature>;
117is($v, q[&foo number 2], "Returns correct var", :todo<feature>);
118
119$v="";
120ok eval(q{ $s = &OughtaWork::foo; $v = $s();  }),
121        "AUTOSUB - repeat", :todo<feature>;
122is($v, q[&foo number 2], "AUTOSUB only called once", :todo<feature>);
123
124$v="";
125ok eval(q{ $s = &OughtaWork::bar; $v = $s();  }),
126        "AUTOSUB - second", :todo<feature>;
127is($v, q[&bar number 3], "Returns correct var", :todo<feature>);
128
129
130# presumably AUTOMETH on packages only has any meaning to "package
131# methods"; they have to be classes or roles for AUTOMETH to be method
132# lookups.
133my $inv = ::OughtaWork;
134ok eval(q{ $s = OughtaWork.AUTOMETH("test"); $v = $s($inv:) }),
135        "AUTOMETH - sanity", :todo<bug>;
136is($v, q[OughtaWork.test number 1], "AUTOMETH sanity test", :todo<bug>);
137
138$v = "";
139ok eval(q{ $s = OughtaWork.foo; $v = $s($inv:) }),
140        "AUTOMETH - first", :todo<feature>;
141is($x, q[OughtaWork.foo number 2], "Returns correct var", :todo<feature>);
142
143$s = sub { };
144ok eval(q{ $s = OughtaWork.foo; $v = $s($inv:)  }),
145        "AUTOMETH - repeat", :todo<feature>;
146is($x, q[OughtaWork.foo number 2], "AUTOMETH only called once", :todo<feature>);
147ok eval(q{ $s = OughtaWork.bar; $v = $s($inv:)  }),
148        "AUTOMETH - second", :todo<feature>;
149is($x, q[OughtaWork.bar number 3], "Returns correct var", :todo<feature>);
150
151# discovered bugs (TODO: write tests to track down:)
152
153# 1. changing the is($v, ...) to is($s(), ...) causes:
154
155#  pugs: cannot cast from VInt 1 to Pugs.AST.Internals.VCode
156
157# seems to work interactively though;
158
159# pugs> package OughtaWork { our $u = 0;  sub AUTOSUB    { my $x = "\&{$_} number {++$u}"; sub { $x } } }
160# undef
161# pugs> my $s = OughtaWork::AUTOSUB
162# sub {...}
163# pugs> $s
164# sub {...}
165# pugs> $s()
166# '& number 1'
167
168# further investigation required...
169
170# 2. named subs don't seem to get implicit arguments via $_ (actually
171#    as per S04 this seems to be design behaviour)
172
173# 3. Various nonsense with using a Package as an invocant in a method
174#    call
175
176# 4. Couldn't take ref of a method via &Package.foo
Note: See TracBrowser for help on using the browser.