| 1 | use v6; |
|---|
| 2 | |
|---|
| 3 | use Test; |
|---|
| 4 | |
|---|
| 5 | # various tests derived from L<S10/Autoloading> |
|---|
| 6 | plan 1; |
|---|
| 7 | skip_rest 'XXX - All of this is outdated - Need a total rewrite based on CANDO - Any takers?'; |
|---|
| 8 | exit; |
|---|
| 9 | |
|---|
| 10 | plan 38; |
|---|
| 11 | |
|---|
| 12 | if $?PUGS_BACKEND ne "BACKEND_PUGS" { |
|---|
| 13 | skip_rest "PIL2JS and PIL-Run do not support eval() yet."; |
|---|
| 14 | exit; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | package 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"; |
|---|
| 25 | my $x = OughtaLoad::AUTOLOAD(1,2,3); |
|---|
| 26 | is($x, q[&test('1', '2', '3')], "sanity"); |
|---|
| 27 | |
|---|
| 28 | lives_ok { $x = OughtaLoad::test(2,3,4) }, |
|---|
| 29 | "AUTOLOAD", :todo<feature>; |
|---|
| 30 | |
|---|
| 31 | package 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"); |
|---|
| 65 | is($x, q[$test number 1], "AUTOSCALAR sanity test"); |
|---|
| 66 | |
|---|
| 67 | lives_ok { $x = $OughtaWork::foo }, "AUTOSCALAR - first"; |
|---|
| 68 | is($x, q[$foo number 2], "Returns correct var", :todo<feature>); |
|---|
| 69 | |
|---|
| 70 | $x=""; |
|---|
| 71 | lives_ok { $x = $OughtaWork::foo }, "AUTOSCALAR - repeat"; |
|---|
| 72 | is($x, q[$foo number 2], "AUTOSCALAR only called once", :todo<feature>); |
|---|
| 73 | |
|---|
| 74 | lives_ok { $x = $OughtaWork::bar }, "AUTOSCALAR - second"; |
|---|
| 75 | is($x, q[$bar number 3], "Returns correct var", :todo<feature>); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | # array |
|---|
| 79 | my $a = OughtaWork::AUTOARRAY("test"); |
|---|
| 80 | is($a.join(","), q[auto,test,1], "AUTOARRAY sanity test"); |
|---|
| 81 | |
|---|
| 82 | my @a; |
|---|
| 83 | lives_ok { @a = @OughtaWork::foo }, "AUTOARRAY - first"; |
|---|
| 84 | is(@a.join(","), q[auto,foo,2], "Returns correct var", :todo<feature>); |
|---|
| 85 | |
|---|
| 86 | @a=(); |
|---|
| 87 | lives_ok { @a = @OughtaWork::foo }, "AUTOARRAY - repeat"; |
|---|
| 88 | is(@a.join(","), q[auto,foo,2], "AUTOARRAY only called once", :todo<feature>); |
|---|
| 89 | |
|---|
| 90 | lives_ok { @a = @OughtaWork::bar }, "AUTOARRAY - second"; |
|---|
| 91 | is(@a.join(","), q[auto,bar,3], "Returns correct var", :todo<feature>); |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | # hash |
|---|
| 95 | my %h = OughtaWork::AUTOHASH("test"); |
|---|
| 96 | is(%h.kv.sort.join(","), q[1,auto,num,test], "AUTOHASH sanity test"); |
|---|
| 97 | |
|---|
| 98 | lives_ok { %h = %OughtaWork::foo }, "AUTOHASH - first"; |
|---|
| 99 | is(%h.kv.sort.join(","), q[2,auto,foo,num], "Returns correct var", :todo<feature>); |
|---|
| 100 | |
|---|
| 101 | %h=(); |
|---|
| 102 | lives_ok { %h = %OughtaWork::foo }, "AUTOHASH - repeat"; |
|---|
| 103 | is(%h.kv.sort.join(","), q[2,auto,foo,num], "AUTOHASH only called once", :todo<feature>); |
|---|
| 104 | |
|---|
| 105 | lives_ok { %h = %OughtaWork::bar }, "AUTOHASH - second"; |
|---|
| 106 | is(%h.kv.sort.join(","), q[3,auto,bar,num], "Returns correct var", :todo<feature>); |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | # sub |
|---|
| 110 | my $s = OughtaWork::AUTOSUB("test"); |
|---|
| 111 | my $v = $s(); |
|---|
| 112 | is($v, q[&test number 1], "AUTOSUB sanity test"); |
|---|
| 113 | |
|---|
| 114 | $v=""; |
|---|
| 115 | ok eval(q{ $s = &OughtaWork::foo; $v = $s(); }), |
|---|
| 116 | "AUTOSUB - first", :todo<feature>; |
|---|
| 117 | is($v, q[&foo number 2], "Returns correct var", :todo<feature>); |
|---|
| 118 | |
|---|
| 119 | $v=""; |
|---|
| 120 | ok eval(q{ $s = &OughtaWork::foo; $v = $s(); }), |
|---|
| 121 | "AUTOSUB - repeat", :todo<feature>; |
|---|
| 122 | is($v, q[&foo number 2], "AUTOSUB only called once", :todo<feature>); |
|---|
| 123 | |
|---|
| 124 | $v=""; |
|---|
| 125 | ok eval(q{ $s = &OughtaWork::bar; $v = $s(); }), |
|---|
| 126 | "AUTOSUB - second", :todo<feature>; |
|---|
| 127 | is($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. |
|---|
| 133 | my $inv = ::OughtaWork; |
|---|
| 134 | ok eval(q{ $s = OughtaWork.AUTOMETH("test"); $v = $s($inv:) }), |
|---|
| 135 | "AUTOMETH - sanity", :todo<bug>; |
|---|
| 136 | is($v, q[OughtaWork.test number 1], "AUTOMETH sanity test", :todo<bug>); |
|---|
| 137 | |
|---|
| 138 | $v = ""; |
|---|
| 139 | ok eval(q{ $s = OughtaWork.foo; $v = $s($inv:) }), |
|---|
| 140 | "AUTOMETH - first", :todo<feature>; |
|---|
| 141 | is($x, q[OughtaWork.foo number 2], "Returns correct var", :todo<feature>); |
|---|
| 142 | |
|---|
| 143 | $s = sub { }; |
|---|
| 144 | ok eval(q{ $s = OughtaWork.foo; $v = $s($inv:) }), |
|---|
| 145 | "AUTOMETH - repeat", :todo<feature>; |
|---|
| 146 | is($x, q[OughtaWork.foo number 2], "AUTOMETH only called once", :todo<feature>); |
|---|
| 147 | ok eval(q{ $s = OughtaWork.bar; $v = $s($inv:) }), |
|---|
| 148 | "AUTOMETH - second", :todo<feature>; |
|---|
| 149 | is($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 |
|---|