This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Disallow omitting % and @ on hash and array names
[perl5.git] / t / op / push.t
CommitLineData
8d063cd8
LW
1#!./perl
2
e589c1fb
CK
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
79a0689e
LW
9@tests = split(/\n/, <<EOF);
100 3, 0 1 2, 3 4 5 6 7
110 0 a b c, , a b c 0 1 2 3 4 5 6 7
128 0 a b c, , 0 1 2 3 4 5 6 7 a b c
137 0 6.5, , 0 1 2 3 4 5 6 6.5 7
141 0 a b c d e f g h i j,, 0 a b c d e f g h i j 1 2 3 4 5 6 7
150 1 a, 0, a 1 2 3 4 5 6 7
161 6 x y z, 1 2 3 4 5 6, 0 x y z 7
170 7 x y z, 0 1 2 3 4 5 6, x y z 7
181 7 x y z, 1 2 3 4 5 6 7, 0 x y z
194, 4 5 6 7, 0 1 2 3
20-4, 4 5 6 7, 0 1 2 3
21EOF
22
b5adc3e5 23plan tests => 14 + @tests*4;
79a0689e 24die "blech" unless @tests;
8d063cd8
LW
25
26@x = (1,2,3);
27push(@x,@x);
e589c1fb 28is( join(':',@x), '1:2:3:1:2:3', 'push array onto array');
a60c0954 29push(@x,4);
e589c1fb 30is( join(':',@x), '1:2:3:1:2:3:4', 'push integer onto array');
79a0689e 31
d401967c 32no warnings 'experimental::autoderef';
0953b66b 33
cba5a3b0
DG
34# test for push/pop on arrayref
35push(\@x,5);
e589c1fb 36is( join(':',@x), '1:2:3:1:2:3:4:5', 'push arrayref');
cba5a3b0 37pop(\@x);
e589c1fb 38is( join(':',@x), '1:2:3:1:2:3:4', 'pop arrayref');
cba5a3b0
DG
39
40# test autovivification
41push @$undef1, 1, 2, 3;
e589c1fb 42is( join(':',@$undef1), '1:2:3', 'autovivify array');
d4fc4415
FC
43
44# test push on undef (error)
45eval { push $undef2, 1, 2, 3 };
e589c1fb 46like( $@, qr/Not an ARRAY/, 'push on undef generates an error');
cba5a3b0
DG
47
48# test constant
49use constant CONST_ARRAYREF => [qw/a b c/];
50push CONST_ARRAYREF(), qw/d e f/;
e589c1fb 51is( join(':',@{CONST_ARRAYREF()}), 'a:b:c:d:e:f', 'test constant');
cba5a3b0
DG
52
53# test implicit dereference errors
54eval "push 42, 0, 1, 2, 3";
e589c1fb 55like ( $@, qr/must be array/, 'push onto a literal integer');
cba5a3b0
DG
56
57$hashref = { };
58eval { push $hashref, 0, 1, 2, 3 };
e589c1fb 59like( $@, qr/Not an ARRAY reference/, 'push onto a hashref');
cba5a3b0 60
d4fc4415 61eval { push bless([]), 0, 1, 2, 3 };
e589c1fb 62like( $@, qr/Not an unblessed ARRAY reference/, 'push onto a blessed array ref');
d4fc4415
FC
63
64$test = 13;
cba5a3b0
DG
65
66# test context
67{
68 my($first, $second) = ([1], [2]);
69 sub two_things { return +($first, $second) }
70 push two_things(), 3;
e589c1fb
CK
71 is( join(':',@$first), '1', "\$first = [ @$first ];");
72 is( join(':',@$second), '2:3', "\$second = [ @$second ]");
cba5a3b0
DG
73
74 push @{ two_things() }, 4;
e589c1fb
CK
75 is( join(':',@$first), '1', "\$first = [ @$first ];");
76 is( join(':',@$second), '2:3:4', "\$second = [ @$second ]");
cba5a3b0
DG
77}
78
79a0689e
LW
79foreach $line (@tests) {
80 ($list,$get,$leave) = split(/,\t*/,$line);
79072805 81 ($pos, $len, @list) = split(' ',$list);
79a0689e
LW
82 @get = split(' ',$get);
83 @leave = split(' ',$leave);
84 @x = (0,1,2,3,4,5,6,7);
cba5a3b0 85 $y = [0,1,2,3,4,5,6,7];
79072805
LW
86 if (defined $len) {
87 @got = splice(@x, $pos, $len, @list);
cba5a3b0 88 @got2 = splice($y, $pos, $len, @list);
79072805
LW
89 }
90 else {
91 @got = splice(@x, $pos);
cba5a3b0 92 @got2 = splice($y, $pos);
79072805 93 }
e589c1fb
CK
94 is(join(':',@got), join(':',@get), "got: @got == @get");
95 is(join(':',@x), join(':',@leave), "left: @x == @leave");
96 is(join(':',@got2), join(':',@get), "ref got: @got2 == @get");
97 is(join(':',@$y), join(':',@leave), "ref left: @$y == @leave");
79a0689e
LW
98}
99
a60c0954 1001; # this file is require'd by lib/tie-stdpush.t