Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 181 lines (140 sloc) 4.517 kb
58135b3 [t/spec] changed 'use v6-alpha;' to 'use v6;'
moritz authored
1 use v6;
a69b36c Test suite refactoring.
cosimo authored
2 use Test;
3
ab1c153 [t/spec/S32-array] smartlinks
moritz authored
4 # L<S32::Containers/Array/"=item push">
a69b36c Test suite refactoring.
cosimo authored
5
8301c74 [t] named arg test for push
frew authored
6 =begin description
a69b36c Test suite refactoring.
cosimo authored
7
8 Push tests
9
8301c74 [t] named arg test for push
frew authored
10 =end description
a69b36c Test suite refactoring.
cosimo authored
11
08e6adb lizmat Add type check TODO tests for .push and .unshift
lizmat authored
12 plan 51;
a69b36c Test suite refactoring.
cosimo authored
13
14 # basic push tests
15 {
16 my @push = ();
17
18 is(+@push, 0, 'we have an empty array');
19
20 push(@push, 1);
21 is(+@push, 1, 'we have 1 element in the array');
22 is(@push[0], 1, 'we found the right element');
23
24 push(@push, 2);
25 is(+@push, 2, 'we have 2 elements in the array');
26 is(@push[1], 2, 'we found the right element');
27
28 push(@push, 3);
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
29 is(+@push, 3, 'we have 3 elements in the array');
a69b36c Test suite refactoring.
cosimo authored
30 is(@push[2], 3, 'we found the right element');
31
32 push(@push, 4);
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
33 is(+@push, 4, 'we have 4 elements in the array');
a69b36c Test suite refactoring.
cosimo authored
34 is(@push[3], 4, 'we found the right element');
35 }
36
4501b2e [t/spec] S32-array/push.t tests for .push returning modified array
kyle authored
37 {
5a878be [t/spec] Fudge for ng1.
colomon authored
38 my @p = ();
4501b2e [t/spec] S32-array/push.t tests for .push returning modified array
kyle authored
39 @p.push( 'bughunt' );
40
41 is( +@p, 1, 'single element array' );
42 ok( @p ~~ Array, '@p ~~ Array' );
43
44 my @push_result = @p.push( 'yo, check it' );
45
46 is( +@p, 2, 'array received second element' );
47 ok( @push_result ~~ @p, 'modified array, returned' );
48 is( ~@p, 'bughunt yo, check it', '~@p' );
49 is( ~@p.push('!'), 'bughunt yo, check it !', '~ on the push' );
50 }
51
a69b36c Test suite refactoring.
cosimo authored
52 # try other variations on calling push()
53 {
54 my @push = ();
55
56 my $val = 100;
57
58 push @push, $val;
59 is(+@push, 1, 'we have 1 element in the array');
60 is(@push[0], $val, 'push @array, $val worked');
61
62 @push.push(200);
63 is(+@push, 2, 'we have 2 elements in the array');
64 is(@push[1], 200, '@push.push(200) works');
65
66 @push.push(400);
67 is(+@push, 3, 'we have 3 elements in the array');
68 is(@push[2], 400, '@push.push(400) works');
69 }
70
71 # try pushing more than one element
72 {
73 my @push = ();
74
75 push @push, (1, 2, 3);
76 is(+@push, 3, 'we have 3 elements in the array');
77 is(@push[0], 1, 'got the expected element');
78 is(@push[1], 2, 'got the expected element');
79 is(@push[2], 3, 'got the expected element');
80
81 my @val2 = (4, 5);
8301c74 [t] named arg test for push
frew authored
82 push @push, @val2;
a69b36c Test suite refactoring.
cosimo authored
83 is(+@push, 5, 'we have 5 elements in the array');
84 is(@push[3], 4, 'got the expected element');
85 is(@push[4], 5, 'got the expected element');
8301c74 [t] named arg test for push
frew authored
86
a69b36c Test suite refactoring.
cosimo authored
87 push @push, 6, 7, 8; # push() should be slurpy
88 is(+@push, 8, 'we have 8 elements in the array');
89 is(@push[5], 6, 'got the expected element');
8301c74 [t] named arg test for push
frew authored
90 is(@push[6], 7, 'got the expected element');
91 is(@push[7], 8, 'got the expected element');
a69b36c Test suite refactoring.
cosimo authored
92 }
93
94 # now for the push() on an uninitialized array issue
95 {
96 my @push;
97
98 push @push, 42;
99 is(+@push, 1, 'we have 1 element in the array');
100 is(@push[0], 42, 'got the element expected');
101
102 @push.push(2000);
103 is(+@push, 2, 'we have 1 element in the array');
104 is(@push[0], 42, 'got the element expected');
105 is(@push[1], 2000, 'got the element expected');
106 }
107
108 # testing some edge cases
109 {
e7a1177 [t/spec] Change .. to ..., fudge two tests for ng.
colomon authored
110 my @push = 0 ... 5;
a69b36c Test suite refactoring.
cosimo authored
111 is(+@push, 6, 'starting length is 6');
8301c74 [t] named arg test for push
frew authored
112
a69b36c Test suite refactoring.
cosimo authored
113 push(@push);
114 is(+@push, 6, 'length is still 6');
8301c74 [t] named arg test for push
frew authored
115
a69b36c Test suite refactoring.
cosimo authored
116 @push.push();
117 is(+@push, 6, 'length is still 6');
118 }
119
120 # testing some error cases
121 {
27d699f Moritz Lenz move some errors into eval()
moritz authored
122 eval_dies_ok 'push()', 'push() requires arguments (1)';
a69b36c Test suite refactoring.
cosimo authored
123 # This one is okay, as push will push 0 elems to a rw arrayref.
124 lives_ok({ push([]) }, 'push() requires arguments (2)');
27d699f Moritz Lenz move some errors into eval()
moritz authored
125 eval_dies_ok '42.push(3)', '.push should not work on scalars';
a69b36c Test suite refactoring.
cosimo authored
126 }
127
128 # Push with Inf arrays (waiting on answers to perl6-compiler email)
f9002e6 [STD] now does reduce tokens correctly; we now use a variable length
lwall authored
129 # {
130 # my @push = 1 .. Inf;
131 # # best not to uncomment this it just go on forever
132 # todo_throws_ok { 'push @push, 10' }, '?? what should this error message be ??', 'cannot push onto a Inf array';
133 # }
a69b36c Test suite refactoring.
cosimo authored
134
135 # nested arrayref
136 {
5a878be [t/spec] Fudge for ng1.
colomon authored
137 my @push = ();
e7a1177 [t/spec] Change .. to ..., fudge two tests for ng.
colomon authored
138 push @push, [ 21 ... 25 ];
a69b36c Test suite refactoring.
cosimo authored
139
140 is(@push.elems, 1, 'nested arrayref, array length is 1');
141 is(@push[0].elems, 5, 'nested arrayref, arrayref length is 5');
142 is(@push[0][0], 21, 'nested arrayref, first value is 21');
2a55815 [spec] last item of an array is @a[*-1], not @[-1]
moritz authored
143 is(@push[0][*-1], 25, 'nested arrayref, last value is 25');
a69b36c Test suite refactoring.
cosimo authored
144 }
2a55815 [spec] last item of an array is @a[*-1], not @[-1]
moritz authored
145
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
146 # RT #69548
147 {
148 {
149 my $x = 1;
5a878be [t/spec] Fudge for ng1.
colomon authored
150 my @a = ();
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
151 push @a, $x;
152 ++$x;
963e8c7 [t/spec] tests for RT #69548 .push allows freaky action-at-a-distance in...
bbkr authored
153
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
154 is @a[0], 1, 'New element created by push(@a, $x) isn\'t affected by changes to $x';
155 }
963e8c7 [t/spec] tests for RT #69548 .push allows freaky action-at-a-distance in...
bbkr authored
156 {
157 my $x = 1;
158 my @a = ();
159 push @a, $x;
160 ++@a[0];
161
162 is $x, 1, '$x isn\'t affected by changes to new element created by push(@a, $x)';
163 }
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
164 }
165
4214a48 Moritz Lenz RT #109476
moritz authored
166 # RT #109476
167 {
168 my %h = ( <foo> => []);
169 push %h<foo>, my $foo = 'bar';
170 is %h<foo>, 'bar', 'pushing assignment to array-in-hash';
171 }
172
08e6adb lizmat Add type check TODO tests for .push and .unshift
lizmat authored
173 # RT 119061
5604403 Will Coleda niecza fudge
coke authored
174 #?niecza todo "https://github.com/sorear/niecza/issues/184"
08e6adb lizmat Add type check TODO tests for .push and .unshift
lizmat authored
175 {
176 my Int @a;
177 dies_ok( { @a.push: "a" }, "cannot push strings onto in Int array" );
178 }
1bc2bdb [t/spec/S32-array/push.t] Added a test for RT #69548
Kodi authored
179
2a55815 [spec] last item of an array is @a[*-1], not @[-1]
moritz authored
180 # vim: syn=perl6
Something went wrong with that request. Please try again.