2
2
3
3
use Test ;
4
4
5
- plan 62 ;
5
+ plan 31 ;
6
6
7
7
= begin description
8
8
@@ -12,199 +12,199 @@ This test tests the C<squish> builtin and .squish method on Any/List.
12
12
13
13
{
14
14
my @ array = <a b b c d e f f a >;
15
- is-deeply @ array . squish, <a b c d e f a >,
16
- " method form of squish works" ;
17
- is-deeply squish(@ array ), <a b c d e f a >,
18
- " subroutine form of squish works" ;
19
- is-deeply @ array .= squish, [<a b c d e f a >],
20
- " inplace form of squish works" ;
21
- is-deeply @ array , [<a b c d e f a >],
22
- " final result of in place" ;
23
- } # 4
15
+ is-deeply @ array . squish, <a b c d e f a >. Seq , ' method form' ;
16
+ is-deeply squish(@ array ), <a b c d e f a >. Seq , ' subroutine form' ;
17
+ is-deeply @ array .= squish, [<a b c d e f a >], ' meta-assign form (return)' ;
18
+ is-deeply @ array , [<a b c d e f a >], ' meta-assign form (result)' ;
19
+ }
24
20
25
- {
26
- is squish(Any ,' a' , ' b' , ' b' , ' c' , ' d' , ' e' , ' f' , ' f' , ' a' ),
27
- (Any , <a b c d e f a >),
28
- ' slurpy subroutine form of squish works' ;
29
- } # 1
21
+ is-deeply squish(Any , ' a' , ' b' , ' b' , ' c' , ' d' , ' e' , ' f' , ' f' , ' a' ),
22
+ (Any , | <a b c d e f a >). Seq , ' slurpy subroutine form of squish' ;
30
23
31
- {
32
- is 42 . squish, 42 , " .squish can work on scalars" ;
33
- is (42 ,). squish, 42 , " .squish can work on one-elem arrays" ;
34
- } # 2
24
+ is-deeply 42 . squish, (42 ,). Seq , ' .squish with Int' ;
25
+ is-deeply (42 ,). squish, (42 ,). Seq , ' .squish with one-elem list' ;
35
26
36
27
{
37
- my class A { method Str { ' ' } };
38
- is (A. new , A. new ). squish. elems , 2 , ' squish has === semantics for objects' ;
39
- } # 1
28
+ my class A { }
29
+ my @ dif := A. new , 1 , <1 >, <1 >, " 1" , $ _ , $ _ with A. new ;
30
+ is-deeply @ dif . squish, @ dif [0 ,1 ,2 ,4 ,5 ]. Seq , ' squish has === semantics' ;
31
+ }
40
32
41
33
{
42
- my @ list = 1 , " 1 " ;
43
- my @ squish = squish( @ list ) ;
44
- is @ squish , @ list , " squish has === semantics for containers " ;
45
- } # 1
34
+ my \a := squish 1 .. Inf ;
35
+ is-deeply a . is-lazy , True , ' squish propagates is-lazy ' ;
36
+ is-deeply a[ 3 ], 4 , ' can access elements from infinite squish ' ;
37
+ }
46
38
47
- {
48
- my \a := squish( 1 .. Inf );
49
- ok a. is-lazy , ' squish knows itself to be lazy' ;
50
- is a[3 ], 4 , ' ... can access elements from lazy iterator' ;
51
- } # 1
39
+ subtest ' with :as' => {
40
+ plan 4 ;
52
41
53
- {
54
- my @ array = <a b bb c d e f f a >;
55
- my $ as = *. substr : 0 ,1 ;
56
- is-deeply @ array . squish(: $ as ), <a b c d e f a >,
57
- " method form of squish with :as works" ;
58
- is-deeply squish(@ array ,: $ as ), <a b c d e f a >,
59
- " subroutine form of squish with :as works" ;
60
- is-deeply @ array .= squish(: $ as ), [<a b c d e f a >],
61
- " inplace form of squish with :as works" ;
62
- is-deeply @ array , [<a b c d e f a >],
63
- " final result with :as in place" ;
64
- } # 4
42
+ my @ a = <a b bb c d e f f a >;
43
+ my $ as = *. substr : 0 , 1 ;
44
+ is-deeply @ a . squish(: $ as ), <a b c d e f a >. Seq , ' method form' ;
45
+ is-deeply squish(@ a ,: $ as ), <a b c d e f a >. Seq , ' subroutine form' ;
46
+ is-deeply @ a .= squish(: $ as ), [<a b c d e f a >], ' meta-assign form (return)' ;
47
+ is-deeply @ a , [<a b c d e f a >], ' meta-assign form (result)' ;
48
+ }
65
49
66
- {
67
- my @ rt124204 = (' ' , ' ' , Any , Any );
68
- is-deeply @ rt124204 . squish(: as(-> $ x {$ x })), (' ' , Any ),
69
- " method form of squish with :as does not needlessly stringify" ;
70
- is-deeply @ rt124204 . squish, (' ' , Any ),
71
- " method form of squish without :as does not needlessly stringify" ;
72
- is-deeply @ rt124204 . squish(: as(-> $ x {$ x }), : with({$ ^ b === $ ^ a })), (' ' , Any ),
73
- " method form of squish with :as and :with does not needlessly stringify" ;
74
- is-deeply @ rt124204 . squish(: with({$ ^ b === $ ^ a })), (' ' , Any ),
75
- " method form of squish with :with does not needlessly stringify" ;
76
- } # 4
50
+ # RT #124204
51
+ subtest ' method form of squish does not stringify' => {
52
+ plan 4 ;
53
+ my @ a = ' ' , ' ' , Any , Any ;
54
+ my $ with := {$ ^ b === $ ^ a };
55
+ is-deeply @ a . squish, (' ' , Any ). Seq , ' no args' ;
56
+ is-deeply @ a . squish(: as{$ _ }), (' ' , Any ). Seq , ' with :as' ;
57
+ is-deeply @ a . squish(: $ with ), (' ' , Any ). Seq , ' with :with' ;
58
+ is-deeply @ a . squish(: as{$ _ }, : $ with ), (' ' , Any ). Seq , ' with :as and :with' ;
59
+ }
77
60
78
- {
61
+ subtest ' method form of squish returns at least the first element' => {
62
+ plan 4 ;
79
63
my @ rt124205 = <a a >;
80
-
81
- is @ rt124205 . squish(: as(-> $ x {1 }), : with(-> $ a , $ b {1 })), <a >,
82
- " method form of squish with :as and :with always returns at least the first element" ;
83
- is @ rt124205 . squish(: with(-> $ a , $ b {1 })), <a >,
84
- " method form of squish with :with always returns at least the first element" ;
64
+ is-deeply @ rt124205 . squish(: as{1 }, : with(-> $ a , $ b {1 })), <a >. Seq ,
65
+ ' with :as and :with' ;
66
+ is-deeply @ rt124205 . squish(: with(-> $ a , $ b {1 })), <a >. Seq ,
67
+ ' with :with only' ;
85
68
86
69
# somewhat more real-world examples:
87
-
88
70
my @ rt124205_b = ' ' , ' ' , | <b b B B >;
71
+ is-deeply @ rt124205_b . squish(: with(*. Str eq *. Str )), (' ' , ' b' , ' B' ). Seq ,
72
+ " with :with, first element stringifies to ''" ;
89
73
90
- is-deeply @ rt124205_b . squish(: with(*. Str eq *. Str )), (' ' , ' b' , ' B' ),
91
- " method form of squish with :with preserves the first element even if it stringifies to ''" ;
92
-
93
- is-deeply @ rt124205_b . squish(: as(*. Str ), : with(& infix : <eq >)), (' ' , ' b' , ' B' ),
94
- " method form of squish with :as and :with preserves the first element even if it stringifies to ''" ;
95
-
96
- } # 4
74
+ is-deeply @ rt124205_b . squish(: as(*. Str ), : with(& infix : <eq >)),
75
+ (' ' , ' b' , ' B' ). Seq ,
76
+ " with :with and :as, first element stringifies to ''" ;
77
+ }
97
78
98
- {
79
+ subtest ' with :with' => {
80
+ plan 4 ;
99
81
my @ array = <a aa b bb c d e f f a >;
100
82
my $ with = { substr ($ ^ a ,0 ,1 ) eq substr ($ ^ b ,0 ,1 ) }
101
- is-deeply @ array . squish(: $ with ), <a b c d e f a >,
102
- " method form of squish with :with works" ;
103
- is-deeply squish(@ array ,: $ with ), <a b c d e f a >,
104
- " subroutine form of squish with :with works" ;
83
+ is-deeply @ array . squish(: $ with ), <a b c d e f a >. Seq , ' method form' ;
84
+ is-deeply squish(@ array ,: $ with ), <a b c d e f a >. Seq , ' subroutine form' ;
105
85
is-deeply @ array .= squish(: $ with ), [<a b c d e f a >],
106
- " inplace form of squish with :with works" ;
107
- is-deeply @ array , [<a b c d e f a >],
108
- " final result with :with in place" ;
109
- } # 4
86
+ ' meta-assign form (return)' ;
87
+ is-deeply @ array , [<a b c d e f a >], ' meta-assign form (result)' ;
88
+ }
110
89
111
- {
90
+ subtest ' with :with and :as' => {
91
+ plan 4 ;
112
92
my @ array = <a aa b bb c d e f f a >;
113
93
my $ as = *. substr (0 ,1 ). ord ;
114
94
my $ with = &[== ];
115
- is-deeply @ array . squish(: $ as , : $ with ), <a b c d e f a >,
116
- " method form of squish with :as and :with works" ;
117
- is-deeply squish(@ array ,: $ as , : $ with ), <a b c d e f a >,
118
- " subroutine form of squish with :as and :with works" ;
95
+ is-deeply @ array . squish(: $ as , : $ with ), <a b c d e f a >. Seq , ' method form' ;
96
+ is-deeply squish(@ array ,: $ as , : $ with ), <a b c d e f a >. Seq , ' sub form' ;
119
97
is-deeply @ array .= squish(: $ as , : $ with ), [<a b c d e f a >],
120
- " inplace form of squish with :as and :with works" ;
121
- is-deeply @ array , [<a b c d e f a >],
122
- " final result with :as and :with in place" ;
123
- } # 4
98
+ ' meta-assign form (return)' ;
99
+ is-deeply @ array , [<a b c d e f a >], ' meta-assign form (result)' ;
100
+ }
124
101
125
102
126
103
{
127
- my @ a ;
128
- my $ as = { @ a . push ($ _ ); $ _ };
129
- my @ w ;
130
- my $ with = { @ w . push (" $ ^ a $ ^ b" ); $ ^ a + 1 == $ ^ b };
131
- is (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ as ,: $ with ), (1 ,2 ,1 ,0 ),
132
- ' Order of :with operands, and first one of each run (:as)' ;
133
- is bag(@ a ), bag(1 ,2 ,3 ,2 ,1 ,0 ), ' :as callbacks called once per element' ;
134
- is bag(@ w ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
135
- ' :with callbacks called minimumish number of times (:as)' ;
136
- @ w = ();
137
- is (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ with ), (1 ,2 ,1 ,0 ),
138
- ' Order of :with operands, and first one of each run' ;
139
- is bag(@ w ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
140
- ' :with callbacks called minimumish number of times.' ;
141
- @ a = (); @ w = ();
142
- is (1 ). squish(: $ with , : $ as ), (1 ),
143
- ' Single element list with :as and :with' ;
144
- ok so all ((@ a . elems ,@ w . elems ) Z < 2 , 1 ),
145
- ' at most one :as and no :with callbacks with single element list' ;
146
- @ a = (); @ w = ();
147
- is (1 ). squish(: $ with ), (1 ),
148
- ' Single element list with :with' ;
149
- is @ w , [], " No callbacks with single element list" ;
150
- @ w = ();
151
- my $ i = (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ as ,: $ with ). iterator ;
152
- is $ i . pull-one , 1 , ' .pull-one with :with and :as' ;
153
- ok so all ((@ a . elems ,@ w . elems ) Z < 2 , 1 ),
154
- ' .pull-one called at most one :as and no :with on first pull' ;
155
- is $ i . pull-one , 2 , ' second .pull-one with :with and :as' ;
156
- ok so all ((@ a . elems ,@ w . elems ) Z == 4 , 3 ),
157
- ' second .pull-one called 4 :as and three :with' ;
158
- my @ c ;
159
- $ i . push-all (@ c );
160
- is @ c , (1 ,0 ), ' push-all after a couple pull-ones works (:as)' ;
161
- is bag(@ a ), bag(1 ,2 ,3 ,2 ,1 ,0 ), ' :as callbacks called once per element (fragged)' ;
162
- is bag(@ w ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
163
- ' :with callbacks called minimumish number of times (:as, fragged)' ;
164
- @ w = ();
165
- $ i = (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ with ). iterator ;
166
- is $ i . pull-one , 1 , ' .pull-one with :with' ;
167
- nok @ w . elems , ' no :with called on first pull' ;
168
- is $ i . pull-one , 2 , ' second .pull-one with :with' ;
169
- ok @ w . elems == 3 , ' second .pull-one called three :with' ;
170
- @ c = ();
171
- $ i . push-all (@ c );
172
- is @ c , (1 ,0 ), ' push-all after a couple pull-ones works' ;
173
- is bag(@ w ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
174
- ' :with callbacks called minimumish number of times (:as, fragged)' ;
104
+ my @ as ;
105
+ my $ as = { @ as . push : $ _ ; $ _ };
106
+ my @ with ;
107
+ my $ with = { @ with . push : " $ ^ a $ ^ b" ; $ ^ a + 1 == $ ^ b };
108
+ is-deeply (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ as ,: $ with ), (1 ,2 ,1 ,0 ). Seq ,
109
+ ' order of :with operands, and first one of each run (:as)' ;
110
+ is bag(@ as ), bag(1 ,2 ,3 ,2 ,1 ,0 ), ' :as callbacks called once per element' ;
111
+ is bag(@ with ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
112
+ ' :with callbacks called minimum-ish number of times (:as)' ;
113
+
114
+ @ with = ();
115
+ is-deeply (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ with ), (1 ,2 ,1 ,0 ). Seq ,
116
+ ' order of :with operands, and first one of each run' ;
117
+ is bag(@ with ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
118
+ ' :with callbacks called minimum-ish number of times.' ;
119
+
120
+ subtest ' squish with single element list with :as and :with' => {
121
+ plan 3 ;
122
+ @ as = (); @ with = ();
123
+ is-deeply (1 ). squish(: $ with , : $ as ), 1 . Seq , ' result' ;
124
+ cmp-ok @ as , ' <=' , 1 , ' at most one :as callback call' ;
125
+ cmp-ok @ with , ' ==' , 0 , ' no :with callback calls' ;
126
+ }
127
+
128
+ @ as = (); @ with = ();
129
+ is-deeply (1 ). squish(: $ with ), 1 . Seq , ' single element list with :with' ;
130
+ cmp-ok @ with , ' ==' , 0 , ' no callback calls with single element list' ;
131
+
132
+ subtest ' :as + :with, iterator use' => {
133
+ plan 5 ;
134
+ my $ i := (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ as , : $ with ). iterator ;
135
+
136
+ subtest ' first .pull-one' => {
137
+ plan 3 ;
138
+ @ with = (); @ as = ();
139
+ is-deeply $ i . pull-one , 1 , ' value' ;
140
+ cmp-ok @ as , ' <=' , 1 , ' at most 1 :as callback call' ;
141
+ cmp-ok @ with , ' ==' , 0 , ' no :with callback calls' ;
142
+ }
143
+
144
+ subtest ' second .pull-one' => {
145
+ plan 3 ;
146
+ is-deeply $ i . pull-one , 2 , ' value' ;
147
+ cmp-ok @ as , ' ==' , 4 , ' 4 total :as callback calls so far' ;
148
+ cmp-ok @ with , ' ==' , 3 , ' 3 total :with callback calls so far' ;
149
+ }
150
+
151
+ $ i . push-all : my @ c ;
152
+ is-deeply @ c , [1 , 0 ], ' .push-all values' ;
153
+
154
+ is bag(@ as ), bag(1 ,2 ,3 ,2 ,1 ,0 ),
155
+ ' :as callbacks called once per element (fragged)' ;
156
+ is bag(@ with ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
157
+ ' :with callbacks called minimumish number of times (:as, fragged)' ;
158
+ }
159
+
160
+ subtest ' :with + iterator use' => {
161
+ plan 6 ;
162
+ @ with = ();
163
+ my $ i := (1 ,2 ,3 ,2 ,1 ,0 ). squish(: $ with ). iterator ;
164
+ is-deeply $ i . pull-one , 1 , ' first .pull-one value' ;
165
+ cmp-ok @ with , ' ==' , 0 , ' no :with called on first pull' ;
166
+
167
+ is-deeply $ i . pull-one , 2 , ' second .pull-one value' ;
168
+ cmp-ok @ with , ' ==' , 3 , ' second .pull-one called three :with' ;
169
+
170
+ $ i . push-all : my @ c ;
171
+ is-deeply @ c , [1 ,0 ], ' .push-all after a couple pull-ones' ;
172
+ is bag(@ with ), bag(" 1 2" ," 2 3" ," 3 2" ," 2 1" ," 1 0" ),
173
+ ' :with callbacks called minimum-ish number of times (:as, fragged)' ;
174
+ }
175
175
}
176
176
177
- {
177
+ subtest ' squish with [eqv] and objects' => {
178
+ plan 4 ;
178
179
my @ array = ({: a<1 >}, {: a<1 >}, {: b<1 >});
179
180
my $ with = &[eqv ];
180
- is-deeply @ array . squish(: $ with ), ({: a<1 >}, {: b<1 >}),
181
- " method form of squish with [eqv] and objects works" ;
182
- is-deeply squish(@ array ,: $ with ), ({: a<1 >}, {: b<1 >}),
183
- " subroutine form of squish with [eqv] and objects works" ;
181
+ is-deeply @ array . squish(: $ with ), ({: a<1 >}, {: b<1 >}). Seq , ' method form' ;
182
+ is-deeply squish(@ array ,: $ with ), ({: a<1 >}, {: b<1 >}). Seq , ' sub form' ;
184
183
is-deeply @ array .= squish(: $ with ), [{: a<1 >}, {: b<1 >}],
185
- " inplace form of squish with [eqv] and objects works" ;
186
- is-deeply @ array , [{: a<1 >}, {: b<1 >}],
187
- " final result with [eqv] and objects in place" ;
188
- } # 4
184
+ ' meta-assign form (return)' ;
185
+ is-deeply @ array , [{: a<1 >}, {: b<1 >}], ' meta-assign form (result)' ;
186
+ }
189
187
190
188
# RT #121434
191
189
{
192
- my $ a = <a b b c >;
193
- $ a .= squish;
194
- is-deeply ( $ a , <a b c >, ' .= squish in sink context works on $a' );
190
+ my $ a = <a b b c >; $ a .= squish;
191
+ is-deeply $ a , <a b c >. Seq , ' .= squish in sink context works on $foo' ;
195
192
my @ a = <a b b c >;
196
193
@ a .= squish;
197
- is-deeply ( @ a , [<a b c >], ' .= squish in sink context works on @a ' ) ;
198
- } # 2
194
+ is-deeply @ a , [<a b c >], ' .= squish in sink context works on @foo ' ;
195
+ }
199
196
200
- my @ a := (1 , 2 );
201
- is ((3 ,3 ,1 ),@ a ,@ a ). squish. Str , ' 3 3 1 1 2' , " .squish doesn't flatten" ;
197
+ {
198
+ my @ a := (1 , 2 );
199
+ is-deeply ((3 ,3 ,1 ), @ a , @ a ). squish, ((3 , 3 , 1 ), (1 , 2 )). Seq ,
200
+ " .squish doesn't flatten" ;
201
+ }
202
202
203
203
# RT #126293
204
204
{
205
- is <a a b b c c >. squish, <a b c >, ' do we squish at all?' ;
206
- my $ as = *. lc ;
207
- is < a A b B c > . squish( : $ as ), < a b c >, ' do we squish at all with :as?' ;
205
+ is-deeply <a a b b c c >. squish, <a b c >. Seq , ' do we squish at all?' ;
206
+ is-deeply < a A b B c > . squish( : as{ . lc }), < a b c > . Seq ,
207
+ ' do we squish at all with :as?' ;
208
208
}
209
209
210
210
# vim: ft=perl6
0 commit comments