/
trans.t
272 lines (204 loc) · 8.52 KB
/
trans.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use v6;
use Test;
=begin pod
String transliteration
=end pod
# L<S05/Transliteration>
plan 60;
is("ABC".trans( ('A'=>'a'), ('B'=>'b'), ('C'=>'c') ),
"abc",
"Each side can be individual characters");
is("XYZ".trans( ('XYZ' => 'xyz') ),
"xyz",
"The two sides of the any pair can be strings interpreted as tr/// would multichar");
is("ABC".trans( ('A..C' => 'a..c') ),
"abc",
"The two sides of the any pair can be strings interpreted as tr/// would range");
is("ABC-DEF".trans(("- AB..Z" => "_ a..z")),
"abc_def",
"If the first character is a dash it isn't part of a range");
is("ABC-DEF".trans(("A..YZ-" => "a..z_")),
"abc_def",
"If the last character is a dash it isn't part of a range");
is("ABCDEF".trans( ('AB..E' => 'ab..e') ),
"abcdeF",
"The two sides can consists of both chars and ranges");
is("ABCDEFGH".trans( ('A..CE..G' => 'a..ce..g') ),
"abcDefgH",
"The two sides can consist of multiple ranges");
is("ABCXYZ".trans( (['A'..'C'] => ['a'..'c']), (<X Y Z> => <x y z>) ),
"abcxyz",
"The two sides of each pair may also be array references" );
is("abcde".trans( ('a..e' => 'A'..'E') ), "ABCDE",
"Using string range on one side and array reference on the other");
is("ABCDE".trans( (['A' .. 'E'] => "a..e") ), "abcde",
"Using array reference on one side and string range on the other");
is(" <>&".trans( ([' ', '<', '>', '&'] =>
[' ', '<', '>', '&' ])),
" <>&","The array version can map one characters to one-or-more characters");
is(" <>&".trans( ([' ', '<', '>', '&' ] =>
[' ', '<', '>', '&' ])),
" <>&",
"The array version can map one-or-more characters to one-or-more characters");
is(" <>&".trans( ([' ', ' <', '<', '>', '&'] =>
[' ', 'AB', '<', '>', '&' ])),
"AB>&",
"The array version can map one characters to one-or-more characters, using leftmost longest match");
is("Whfg nabgure Crey unpxre".trans('a'..'z' => ['n'..'z','a'..'m'], 'A'..'Z' => ['N'..'Z','A'..'M']),
"Just another Perl hacker",
"Ranges can be grouped");
is("Whfg nabgure Crey unpxre".trans('a..z' => 'n..za..m', 'A..Z' => 'N..ZA..M'),
"Just another Perl hacker",
"Multiple ranges interpreted in string");
# Per S05 changes
{
is("Whfg nabgure Crey unpxre".trans(' a..z' => '_n..za..m', 'A..Z' => 'N..ZA..M'),
"Just_another_Perl_hacker",
"Spaces in interpreted ranges are not skipped (all spaces are important)");
is("Whfg nabgure Crey unpxre".trans('a .. z' => 'n .. za .. m', 'A .. Z' => 'N .. ZA .. M'),
"Whfg nnbgure Crey unpxre",
"Spaces in interpreted ranges are not skipped (all spaces are important)");
};
my $a = "abcdefghijklmnopqrstuvwxyz";
my $b = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
is($a.trans('a..z' => 'A..Z'), $b);
is($b.trans('A..Z' => 'a..z'), $a);
is($a.trans('b..y' => 'B..Y'), 'aBCDEFGHIJKLMNOPQRSTUVWXYz');
is("I\xcaJ".trans('I..J' => 'i..j'), "i\xcaj");
is("\x12c\x190".trans("\x12c" => "\x190"), "\x190\x190");
# should these be combined?
#?rakudo todo 'disambiguate ranges'
#?niecza todo
is($b.trans('A..H..Z' => 'a..h..z'), $a,
'ambiguous ranges combined');
is($b.trans('..H..Z' => '__h..z'),
'ABCDEFGhijklmnopqrstuvwxyz',
'leading ranges interpreted as string');
is($b.trans('A..H..' => 'a..h__'), 'abcdefghIJKLMNOPQRSTUVWXYZ',
'trailing ranges interpreted as string');
is($b.trans('..A..H..' => '__a..h__'), 'abcdefghIJKLMNOPQRSTUVWXYZ',
'leading, trailing ranges interpreted as string');
# added as a consequence of RT #76720
is("hello".trans("l" => ""), "heo", "can replace with empty string");
# complement, squeeze/squash, delete
#?niecza 2 skip 'trans flags NYI'
#?rakudo todo 'flags'
is('bookkeeper'.trans(:s, 'a..z' => 'a..z'), 'bokeper',
':s flag (squash)');
is('bookkeeper'.trans(:d, 'ok' => ''), 'beeper',
':d flag (delete)');
is('ABC123DEF456GHI'.trans('A..Z' => 'x'), 'xxx123xxx456xxx',
'no flags');
#?rakudo 4 todo 'flags'
#?niecza 4 skip 'trans flags NYI'
is('ABC123DEF456GHI'.trans(:c, 'A..Z' => 'x'),'ABCxxxDEFxxxGHI',
'... with :c');
is('ABC111DEF222GHI'.trans(:s, '0..9' => 'x'),'ABCxDEFxGHI',
'... with :s');
is('ABC111DEF222GHI'.trans(:c, :s, 'A..Z' => 'x'),'ABCxDEFxGHI',
'... with :s and :c');
is('ABC111DEF222GHI'.trans(:c, :d, 'A..Z' => ''),'ABCDEFGHI',
'... with :d and :c');
is('Good&Plenty'.trans('len' => 'x'), 'Good&Pxxxty',
'no flags');
#?rakudo 5 todo 'flags'
#?niecza 5 skip 'trans flags NYI'
is('Good&Plenty'.trans(:s, 'len' => 'x',), 'Good&Pxty',
'squashing depends on replacement repeat, not searchlist repeat');
is('Good&Plenty'.trans(:s, 'len' => 't'), 'Good&Ptty',
'squashing depends on replacement repeat, not searchlist repeat');
# also checks that :c uses the first element in array (or first char in string)
is(" <>&".trans(:c, ([' ', '>', '&'] =>
['???', 'AB', '>', '&' ])),
' ????????????>&',
'array, many-to-many transliteration, complement');
# fence-post issue with complement
is(" <>&".trans(:c, ([' ', '>'] =>
['???', 'AB'])),
' ????????????>???????????????',
'fence-post issue (make sure to replace end bits as well)');
is(" <>&".trans(:c, :s, ([' ', '>', '&'] =>
['???'])),
' ???>&',
'... and now complement and squash');
{
# remove vowel and character after
is('abcdefghij'.trans(/<[aeiou]> \w/ => ''), 'cdgh', 'basic regex works');
is( # vowels become 'y' and whitespace becomes '_'
"ab\ncd\tef gh".trans(/<[aeiou]>/ => 'y', /\s/ => '_'),
'yb_cd_yf_gh',
'regexes pairs work',
);
my $i = 0;
is('ab_cd_ef_gh'.trans('_' => {$i++}), 'ab0cd1ef2gh', 'basic closure');
$i = 0;
my $j = 0;
is(
'a_b/c_d/e_f'.trans('_' => {$i++}, '/' => {$j++}),
'a0b0c1d1e2f',
'closure pairs work',
);
};
#?rakudo todo 'closures and regexes'
#?niecza skip 'closures and regexes'
{
# closures and regexes!
is(
'[36][38][43]'.trans(/\[(\d+)\]/ => {chr($0)}),
'$&+',
'closure and regex'
);
is(
'"foo & bar"'.trans(
/(' '+)/ => {' ' ~ (' ' x ($0.chars - 1))},
/\W/ => sub {"&#{ord($0)};"}
),
'"foo & bar"',
'pairs of regexes and closures',
);
}
#?niecza skip 'Action method quote:tr NYI'
{
#?rakudo skip 'feed operator NYI'
is(EVAL('"abc".trans(<== "a" => "A")'), "Abc",
"you're allowed to leave off the (...) named arg parens when you use <==");
# Make sure the tr/// version works, too.
$_ = "ABC";
tr/ABC/abc/;
is($_, 'abc', 'tr/// on $_ with explicit character lists');
$_ = "abc";
tr|a..c|A..C|;
is($_, 'ABC', 'tr||| on $_ with character range');
my $japh = "Whfg nabgure Crey unpxre";
$japh ~~ tr[a..zA..Z][n..za..mN..ZA..M];
is($japh, "Just another Perl hacker", 'tr[][] on lexical var via ~~');
$_ = '$123';
tr/$123/X\x20\o40\t/;
is($_, "X \t", 'tr/// on $_ with explicit character lists');
}
# y/// is dead
eval_dies_ok('$_ = "axbycz"; y/abc/def/', 'y/// does not exist any longer');
# RT #71088
{
lives_ok { "".subst(/x/, "").trans() },
'trans on subst output lives';
}
is('aaaaabbbbb'.trans(['aaa', 'aa', 'bb', 'bbb'] => ['1', '2', '3', '4']),
'1243',
'longest constant token preferred, regardless of declaration order');
is('foobar'.trans(/\w+/ => 'correct', /foo/ => 'RONG'), 'correct',
'longest regex token preferred, regardless of declaration order');
is('aaaa'.trans(/a/ => '1', /\w/ => '2', /./ => '3'), '1111',
'in case of a tie between regex lengths, prefer the first one');
is('ababab'.trans([/ab/, 'aba', 'bab', /baba/] =>
['1', '2', '3', '4' ]),
'23',
'longest token still holds, even between constant strings and regexes');
# RT #83674
#?niecza todo 'Not sure what is supposed to be going on here'
lives_ok { my @a = 1..2; @a>>.trans((1..2) => (14..15,1..2)); }, 'trans works with Cool signature';
# RT #83766
#?niecza 2 skip "Nominal type check failed for scalar store; got Int, needed Str or subtype"
is((1, 2)>>.trans((1..26) => (14..26,1..13)), <14 15>, '.trans with a pair of parcels using postfix hypermetaoperator works');
is ("!$_!" for (1, 2)>>.trans((1..26) => (14..26,1..13))), <!14! !15!>, "same with explicit for";
# vim: ft=perl6