|
| 1 | +use v6; |
| 2 | +use Test; |
| 3 | + |
| 4 | +plan 1; |
| 5 | + |
| 6 | +=begin pod |
| 7 | +
|
| 8 | +=head1 DESCRIPTION |
| 9 | +
|
| 10 | +Miscellaneous metaop tests that don't fit into other files in this directory. |
| 11 | +
|
| 12 | +=end pod |
| 13 | + |
| 14 | +# Covers opts in https://github.com/rakudo/rakudo/commit/b9b0838dd8 |
| 15 | +subtest 'cover metaop call simplification optimization' => { |
| 16 | + plan 7; |
| 17 | + subtest '(//=) +=' => { |
| 18 | + plan 2; |
| 19 | + my $a; |
| 20 | + ($a //= 42) += 10; |
| 21 | + is-deeply $a, 52, '(1)'; |
| 22 | + ($a //= 42) += 10; |
| 23 | + is-deeply $a, 62, '(2)'; |
| 24 | + } |
| 25 | + subtest 'R+= (||=)' => { |
| 26 | + plan 2; |
| 27 | + my $a; |
| 28 | + 10 R+= ($a ||= 42); |
| 29 | + is-deeply $a, 52, '(1)'; |
| 30 | + 10 R+= ($a ||= 42); |
| 31 | + is-deeply $a, 62, '(2)'; |
| 32 | + } |
| 33 | + subtest '(((R+= ([R-]=)) //=) +=)' => { |
| 34 | + plan 1; |
| 35 | + my $a = 12; |
| 36 | + ((10 R+= ($a [R-]= 42)) //= 100) += 1000; |
| 37 | + is-deeply $a, 1040, '(1)'; |
| 38 | + } |
| 39 | + subtest '(((R+= ([R-]=)) //=) +=)' => { |
| 40 | + plan 1; |
| 41 | + my $a = 12; |
| 42 | + ((10 R+= ($a [R-]= 42)) //= 100) += 1000; |
| 43 | + is-deeply $a, 1040, '(1)'; |
| 44 | + } |
| 45 | + subtest 'array var with ((||=) += )' => { |
| 46 | + my @a = 20; (@a ||= 42) += 10; dd @a |
| 47 | + } |
| 48 | + |
| 49 | + # https://github.com/rakudo/rakudo/issues/1989 |
| 50 | + subtest 'metassign to array/hash returned from a method' => { |
| 51 | + plan 2; |
| 52 | + my class Foo { has @.a is rw; has %.h is rw }; |
| 53 | + my $o := Foo.new: :h{:42foo}; |
| 54 | + is-deeply ($o.a += <a b c>), [3], '+= to array'; |
| 55 | + is-deeply ($o.h ,= :100bar), {:42foo, :100bar}, ',= to hash'; |
| 56 | + } |
| 57 | + subtest 'failure modes' => { |
| 58 | + plan 5; |
| 59 | + throws-like 「my $a := 42; ($a //= 42) += 10」, X::Assignment::RO, '(1)'; |
| 60 | + throws-like 「my $a := 42; ($a ||= 42) R+= 10」, X::Assignment::RO, '(2)'; |
| 61 | + throws-like 「my $a = 42; ($a ||= 42) R+= 10」, X::Assignment::RO, '(3)'; |
| 62 | + throws-like 「my $a = 42; 1000 += ((10 R+= ($a [R-]= 42)) //= 100)」, X::Assignment::RO, |
| 63 | + '(4)'; |
| 64 | + # https://github.com/rakudo/rakudo/issues/1987 |
| 65 | + throws-like '42.abs += 42', X::Assignment::RO, '(5)'; |
| 66 | + } |
| 67 | +} |
0 commit comments