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