Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 217 lines (177 sloc) 7.941 kb
349578d [t] move subst.t to spec/, changed fudging a bit and added smartlink
moritz authored
1 use v6;
2
3 use Test;
4
f4a2072 [t/spec] Test for RT #69044
kyle authored
5 plan *;
349578d [t] move subst.t to spec/, changed fudging a bit and added smartlink
moritz authored
6
7 # L<S05/Substitution/>
8
9 my $str = 'hello';
10
11 is $str.subst(/h/,'f'), 'fello', 'We can use subst';
12 is $str, 'hello', '.. withouth side effect';
13 #?rakudo skip "multiple adverbs not implemented"
14 is $str.subst(rx:g:i/L/,'p'), 'heppo', '.. with multiple adverbs';
15
16 is $str.subst('h','f'), 'fello', '.. or using Str as pattern';
17 is $str.subst('.','f'), 'hello', '.. with literal string matching';
18
19 my $i=0;
20 is $str.subst(/l/,{$i++}), 'he0lo', 'We can have a closure as replacement';
21 #?rakudo skip "multiple adverbs not implemented"
22 is $str.subst(rx:g/l/,{$i++}),'he12o', '.. which act like closure and can be called more then once';
23 is $str.=subst(/l/,'i'), 'heilo', '.. and with the .= modifier';
24 is $str, 'heilo', '.. it changes the receiver';
25
26 # not sure about this. Maybe '$1$0' should work.
27
28 #?rakudo 3 skip '$/ not involved in .subst yet (unspecced?)'
29 is 'a'.subst(/(.)/,"$1$0"), '', '.. and it can not access captures from strings';
30 is 'a'.subst(/(.)/,{$0~$0}),'aa', '.. you must wrap it in a closure';
54f8da8 [t/spec] fudge subst.t for rakudo
moritz authored
31 is '12'.subst(/(.)(.)/,{$()*2}),'24', '.. and do nifty things in closures';
349578d [t] move subst.t to spec/, changed fudging a bit and added smartlink
moritz authored
32
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
33 {
34 is 'a b c d'.subst(/\w/, 'x', :g), 'x x x x', '.subst and :g';
0bdfece [t/spec] test Str.subst(..., :global)
moritz authored
35 is 'a b c d'.subst(/\w/, 'x', :global), 'x x x x', '.subst and :global';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
36 is 'a b c d'.subst(/\w/, 'x', :x(0)), 'a b c d', '.subst and :x(0)';
37 is 'a b c d'.subst(/\w/, 'x', :x(1)), 'x b c d', '.subst and :x(1)';
38 is 'a b c d'.subst(/\w/, 'x', :x(2)), 'x x c d', '.subst and :x(2)';
39 is 'a b c d'.subst(/\w/, 'x', :x(3)), 'x x x d', '.subst and :x(3)';
40 is 'a b c d'.subst(/\w/, 'x', :x(4)), 'x x x x', '.subst and :x(4)';
99dbede additional substitution tests
zev authored
41 is 'a b c d'.subst(/\w/, 'x', :x(5)), 'a b c d', '.subst and :x(5)';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
42 #?rakudo skip ':x(*) in .subst'
43 is 'a b c d'.subst(/\w/, 'x', :x(*)), 'x x x x', '.subst and :x(*)';
99dbede additional substitution tests
zev authored
44
ca224d0 [t/spec] get rid of some passing TODO tests by skipping them until we…
moritz authored
45 #?rakudo 5 skip 'RT #61206'
99dbede additional substitution tests
zev authored
46 is 'a b c d'.subst(/\w/, 'x', :x(0..1)), 'x b c d', '.subst and :x(0..1)';
47 is 'a b c d'.subst(/\w/, 'x', :x(1..3)), 'x x x d', '.subst and :x(0..3)';
48 is 'a b c d'.subst(/\w/, 'x', :x(3..5)), 'x x x x', '.subst and :x(3..5)';
49 is 'a b c d'.subst(/\w/, 'x', :x(5..6)), 'a b c d', '.subst and :x(5..6)';
50 is 'a b c d'.subst(/\w/, 'x', :x(3..2)), 'a b c d', '.subst and :x(3..2)';
51
52 # string pattern versions
53 is 'a a a a'.subst('a', 'x', :g), 'x x x x', '.subst (str pattern) and :g';
54 is 'a a a a'.subst('a', 'x', :x(0)), 'a a a a', '.subst (str pattern) and :x(0)';
55 is 'a a a a'.subst('a', 'x', :x(1)), 'x a a a', '.subst (str pattern) and :x(1)';
56 is 'a a a a'.subst('a', 'x', :x(2)), 'x x a a', '.subst (str pattern) and :x(2)';
57 is 'a a a a'.subst('a', 'x', :x(3)), 'x x x a', '.subst (str pattern) and :x(3)';
58 is 'a a a a'.subst('a', 'x', :x(4)), 'x x x x', '.subst (str pattern) and :x(4)';
59 is 'a a a a'.subst('a', 'x', :x(5)), 'a a a a', '.subst (str pattern) and :x(5)';
b9475f1 [t/spec] Unfudge a couple of tests for Rakudo.
jnthn authored
60 #?rakudo 6 skip 'RT #61206'
99dbede additional substitution tests
zev authored
61 is 'a a a a'.subst('a', 'x', :x(*)), 'x x x x', '.subst (str pattern) and :x(*)';
62
63 is 'a a a a'.subst('a', 'x', :x(0..1)), 'x a a a', '.subst (str pattern) and :x(0..1)';
64 is 'a a a a'.subst('a', 'x', :x(1..3)), 'x x x a', '.subst (str pattern) and :x(0..3)';
65 is 'a a a a'.subst('a', 'x', :x(3..5)), 'x x x x', '.subst (str pattern) and :x(3..5)';
66 is 'a a a a'.subst('a', 'x', :x(5..6)), 'a a a a', '.subst (str pattern) and :x(5..6)';
67 is 'a a a a'.subst('a', 'x', :x(3..2)), 'a a a a', '.subst (str pattern) and :x(3..2)';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
68 }
349578d [t] move subst.t to spec/, changed fudging a bit and added smartlink
moritz authored
69
54f8da8 [t/spec] fudge subst.t for rakudo
moritz authored
70
71 #?rakudo skip ':nth'
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
72 {
99dbede additional substitution tests
zev authored
73 is 'a b c d'.subst(/\w/, 'x', :nth(0)), 'a b c d', '.subst and :nth(0)';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
74 is 'a b c d'.subst(/\w/, 'x', :nth(1)), 'x b c d', '.subst and :nth(1)';
75 is 'a b c d'.subst(/\w/, 'x', :nth(2)), 'a x c d', '.subst and :nth(2)';
76 is 'a b c d'.subst(/\w/, 'x', :nth(3)), 'a b x d', '.subst and :nth(3)';
77 is 'a b c d'.subst(/\w/, 'x', :nth(4)), 'a b c x', '.subst and :nth(4)';
78 is 'a b c d'.subst(/\w/, 'x', :nth(5)), 'a b c d', '.subst and :nth(5)';
99dbede additional substitution tests
zev authored
79
80 # string pattern versions
81 is 'a a a a'.subst('a', 'x', :nth(0)), 'a a a a', '.subst (str pattern) and :nth(0)';
82 is 'a a a a'.subst('a', 'x', :nth(1)), 'x a a a', '.subst (str pattern) and :nth(1)';
83 is 'a a a a'.subst('a', 'x', :nth(2)), 'a x a a', '.subst (str pattern) and :nth(2)';
84 is 'a a a a'.subst('a', 'x', :nth(3)), 'a a x a', '.subst (str pattern) and :nth(3)';
85 is 'a a a a'.subst('a', 'x', :nth(4)), 'a a a x', '.subst (str pattern) and :nth(4)';
86 is 'a a a a'.subst('a', 'x', :nth(5)), 'a a a a', '.subst (str pattern) and :nth(5)';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
87 }
88
54f8da8 [t/spec] fudge subst.t for rakudo
moritz authored
89 #?rakudo skip ':nth'
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
90 {
91 # combining :g and :nth:
54f8da8 [t/spec] fudge subst.t for rakudo
moritz authored
92 # #?rakudo 2 todo 'RT #61130'
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
93 is 'a b c d'.subst(/\w/, 'x', :nth(1), :g), 'x x x x', '.subst and :g, :nth(1)';
94 is 'a b c d'.subst(/\w/, 'x', :nth(2), :g), 'a x c x', '.subst and :g, :nth(2)';
95 is 'a b c d'.subst(/\w/, 'x', :nth(3), :g), 'a b x d', '.subst and :g, :nth(3)';
96 }
97
ca224d0 [t/spec] get rid of some passing TODO tests by skipping them until we…
moritz authored
98 #?rakudo skip 'RT #61130'
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
99 {
100 # combining :nth with :x
101 is 'a b c d e f g h'.subst(/\w/, 'x', :nth(1), :x(3)),
102 'x x x d e f g h',
103 '.subst with :nth(1) and :x(3)';
104
105 is 'a b c d e f g h'.subst(/\w/, 'x', :nth(2), :x(2)),
106 'a x c x e f g h',
107 '.subst with :nth(2) and :x(2)';
108
109 is 'a b c d e f g h'.subst(/\w/, 'x', :nth(2), :x(3)),
110 'a x c x e x g h',
43b8d4b typo in test description
zev authored
111 '.subst with :nth(2) and :x(3)';
2bb550b [t/spec] tests for Str.subst with :g, :x() and :nth(), both single an…
moritz authored
112 }
2c6f6e0 [t] merge subst.t into the subst.t under spec/
moritz authored
113
49a6e3f [t/spec] fudge subst.t for rakudo (again ;)
moritz authored
114 #?rakudo skip 's/.../../'
2c6f6e0 [t] merge subst.t into the subst.t under spec/
moritz authored
115 {
116 my $s = "ZBC";
117 my @a = ("A", 'ZBC');
118
119 $_ = q{Now I know my abc's};
120
121 s:global/Now/Wow/;
122 is($_, q{Wow I know my abc's}, 'Constant substitution');
123
124 s:global/abc/$s/;
125 is($_, q{Wow I know my ZBC's}, 'Scalar substitution');
126
127 s:g/BC/@a[]/;
128 is($_, q{Wow I know my ZA ZBC's}, 'List substitution');
3e790d8 [t/spec] tests for ss/// and s[]= forms
moritz authored
129
130 dies_ok { 'abc' ~~ s/b/g/ },
131 "can't modify string literal (only variables)";
132 }
133
134 # L<S05/Modifiers/The :s modifier is considered sufficiently important>
135 #?rakudo skip 'ss/.../.../'
136 {
137 given "a\nb\tc d" {
138 ok ss/a b c d/w x y z/, 'successful substitution returns True';
139 is $_, "w\nx\ty z", 'ss/.../.../ preserves whitespace';
140 }
141
142 ok !("abc" ~~ ss/a b c/ x y z/), 'ss/// implies :s (-)';
143 }
144
145 #L<S05/Substitution/As with Perl 5, a bracketing form is also supported>
146 #?rakudo skip 's[...] = RHS'
147 {
148 given 'abc' {
149 ok (s[b] = 'de'), 's[...] = ... returns true on success';
150 is $_, 'adec', 'substitution worked';
151 }
152
30282ee [t/spec] s[...] op= substitution forms
moritz authored
153 given 'abc' {
154 s[d] = 'foo';
155 is $_, 'abc', 'failed substitutions leaves string unchanged';
156 }
157
3e790d8 [t/spec] tests for ss/// and s[]= forms
moritz authored
158 my $x = 'foobar';
159 ok ($x ~~ s:g[o] = 'u'), 's:g[..] = returns True';
160 is $x, 'fuubar', 'and the substition worked';
b3102b4 [t/spec] test substitution pseudo-assignment forms and captures
moritz authored
161
162 given 'a b c' {
163 s[\w] = uc($/);
164 is $_, 'A b c', 'can use $/ on the RHS';
165 }
166 given 'a b c' {
167 s[(\w)] = uc($0);
168 is $_, 'A b c', 'can use $0 on the RHS';
169 }
170
171 given 'a b c' {
172 s:g[ (\w) ] = $0 x 2;
173 is $_, 'aa bb cc', 's:g[...] and captures work together well';
174 }
2c6f6e0 [t] merge subst.t into the subst.t under spec/
moritz authored
175 }
176
30282ee [t/spec] s[...] op= substitution forms
moritz authored
177 #L<S05/Substitution/Any scalar assignment operator may be used>
178 #?rakudo skip 's[...] op= RHS'
179 {
180 given 'a 2 3' {
181 ok (s[\d] += 5), 's[...] += 5 returns True';
182 is $_, 'a 7 3', 's[...] += 5 gave right result';
183 }
184 given 'a b c' {
185 s:g[\w] x= 2;
186 is $_, 'aa bb cc', 's:g[..] x= 2 worked';
187 }
188 }
189
190 #?rakudo skip 's:g[...] ='
191 {
192 multi sub infix:<fromplus>(Match $a, Int $b) {
193 $a.from + $b
194 }
195
196 given 'a b c' {
197 ok (s:g[\w] fromplus= 3), 's:g[...] customop= returned True';
198 is $_, '3 5 7', '... and got right result';
199 }
200 }
201
f4a2072 [t/spec] Test for RT #69044
kyle authored
202 # RT #69044
203 {
204 sub s { 'sub s' }
a2bbf3c [subst.t] fix parsefailing s,,, test
lwall authored
205 $_ = "foo";
f4a2072 [t/spec] Test for RT #69044
kyle authored
206 #?rakudo skip 'RT 69044'
a2bbf3c [subst.t] fix parsefailing s,,, test
lwall authored
207 ok s,foo,bar, , 'bare s is always substititution';
f4a2072 [t/spec] Test for RT #69044
kyle authored
208 is s(), 'sub s', 'can call sub s as "s()"';
a2bbf3c [subst.t] fix parsefailing s,,, test
lwall authored
209 #?rakudo skip 's () = RHS'
210 $_ = "foo";
211 ok s (foo) = 'bar', 'bare s is substitution before whitespace then parens';
f4a2072 [t/spec] Test for RT #69044
kyle authored
212 }
213
214 done_testing;
30282ee [t/spec] s[...] op= substitution forms
moritz authored
215
2c6f6e0 [t] merge subst.t into the subst.t under spec/
moritz authored
216 # vim: ft=perl6
Something went wrong with that request. Please try again.