Debian Codegraph @

tests/by-util/test_tr.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore lowre punct aabbaa aabbcc aabc abbb abbbcddd abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase fooclass Gzabcdefg PQRST upcase wxyzz xdigit XXXYYY xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz Cdefghijkl Cdefghijklmn asdfqqwweerr qwerr asdfqwer qwer aassddffqwer asdfqwer
7
8 use uutests::at_and_ucmd;
9 use uutests::new_ucmd;
10
11 #[cfg(unix)]
12 use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
13
14 #[test]
15 fn test_invalid_arg() {
16 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
17 }
18
19 #[test]
20 fn test_invalid_input() {
21 new_ucmd!()
22 .args(&["1", "1", "<", "."])
23 .fails_with_code(1)
24 .stderr_contains("tr: extra operand '<'");
25 #[cfg(unix)]
26 {
27 let cmd = new_ucmd!()
28 .args(&["1", "1"])
29 // will test "tr 1 1 < ."
30 .set_stdin(std::process::Stdio::from(std::fs::File::open(".").unwrap()))
31 .fails_with_code(1);
32 if std::env::var("UUTESTS_WASM_RUNNER").is_ok() {
33 // On WASI the fluent translation key may appear instead of the
34 // translated text, but the OS error string is still present.
35 cmd.stderr_contains("Is a directory");
36 } else {
37 cmd.stderr_contains("tr: read error: Is a directory");
38 }
39 }
40 }
41
42 #[test]
43 fn test_to_upper() {
44 new_ucmd!()
45 .args(&["a-z", "A-Z"])
46 .pipe_in("!abcd!")
47 .succeeds()
48 .stdout_is("!ABCD!");
49 }
50
51 #[test]
52 fn test_small_set2() {
53 new_ucmd!()
54 .args(&["0-9", "X"])
55 .pipe_in("@0123456789")
56 .succeeds()
57 .stdout_is("@XXXXXXXXXX");
58 }
59
60 #[test]
61 fn test_invalid_unicode() {
62 new_ucmd!()
63 .args(&["-dc", "abc"])
64 .pipe_in([0o200, b'a', b'b', b'c'])
65 .succeeds()
66 .stdout_is("abc");
67 }
68
69 #[test]
70 fn test_delete() {
71 new_ucmd!()
72 .args(&["-d", "a-z"])
73 .pipe_in("aBcD")
74 .succeeds()
75 .stdout_is("BD");
76 }
77
78 #[test]
79 fn test_delete_graph_and_print_match_gnu() {
80 let input = *b" A!\t\n";
81 new_ucmd!()
82 .args(&["-d", "[:graph:]"])
83 .pipe_in(input)
84 .succeeds()
85 .stdout_is_bytes(*b" \t\n");
86
87 new_ucmd!()
88 .args(&["-d", "[:print:]"])
89 .pipe_in(input)
90 .succeeds()
91 .stdout_is_bytes(*b"\t\n");
92 }
93
94 #[test]
95 fn test_delete_complement_graph_and_print_match_gnu() {
96 let input = *b" A!\t\n";
97 new_ucmd!()
98 .args(&["-d", "-c", "[:graph:]"])
99 .pipe_in(input)
100 .succeeds()
101 .stdout_is_bytes(*b"A!");
102
103 new_ucmd!()
104 .args(&["-d", "-c", "[:print:]"])
105 .pipe_in(input)
106 .succeeds()
107 .stdout_is_bytes(*b" A!");
108 }
109
110 #[test]
111 fn test_delete_afterwards_is_not_flag() {
112 new_ucmd!()
113 .args(&["a-z", "-d"])
114 .pipe_in("aBcD")
115 .succeeds()
116 .stdout_is("-BdD");
117 }
118
119 #[test]
120 fn test_delete_multi() {
121 new_ucmd!()
122 .args(&["-d", "-d", "a-z"])
123 .pipe_in("aBcD")
124 .succeeds()
125 .stdout_is("BD");
126 }
127
128 #[test]
129 fn test_delete_late() {
130 new_ucmd!()
131 .args(&["-d", "a-z", "-d"])
132 .fails()
133 .stderr_contains("extra operand '-d'");
134 }
135
136 #[test]
137 fn test_delete_complement() {
138 new_ucmd!()
139 .args(&["-d", "-c", "a-z"])
140 .pipe_in("aBcD")
141 .succeeds()
142 .stdout_is("ac");
143 }
144
145 #[test]
146 fn test_delete_complement_2() {
147 new_ucmd!()
148 .args(&["-d", "-C", "0-9"])
149 .pipe_in("Phone: 01234 567890")
150 .succeeds()
151 .stdout_is("01234567890");
152 new_ucmd!()
153 .args(&["-d", "--complement", "0-9"])
154 .pipe_in("Phone: 01234 567890")
155 .succeeds()
156 .stdout_is("01234567890");
157 }
158
159 #[test]
160 fn test_complement1() {
161 new_ucmd!()
162 .args(&["-c", "a", "X"])
163 .pipe_in("ab")
164 .succeeds()
165 .stdout_is("aX");
166 }
167
168 #[test]
169 fn test_complement_afterwards_is_not_flag() {
170 new_ucmd!()
171 .args(&["a", "X", "-c"])
172 .fails()
173 .stderr_contains("extra operand '-c'");
174 }
175
176 #[test]
177 fn test_complement2() {
178 new_ucmd!()
179 .args(&["-c", "0-9", "x"])
180 .pipe_in("Phone: 01234 567890")
181 .succeeds()
182 .stdout_is("xxxxxxx01234x567890");
183 }
184
185 #[test]
186 fn test_complement3() {
187 new_ucmd!()
188 .args(&["-c", "abcdefgh", "123"])
189 .pipe_in("the cat and the bat")
190 .succeeds()
191 .stdout_is("3he3ca33a3d33he3ba3");
192 }
193
194 #[test]
195 fn test_complement4() {
196 // $ echo -n '0x1y2z3' | tr -c '0-@' '*-~'
197 // 0~1~2~3
198 new_ucmd!()
199 .args(&["-c", "0-@", "*-~"])
200 .pipe_in("0x1y2z3")
201 .succeeds()
202 .stdout_is("0~1~2~3");
203 }
204
205 #[test]
206 fn test_complement5() {
207 // $ echo -n '0x1y2z3' | tr -c '\0-@' '*-~'
208 // 0a1b2c3
209 new_ucmd!()
210 .args(&["-c", r"\0-@", "*-~"])
211 .pipe_in("0x1y2z3")
212 .succeeds()
213 .stdout_is("0a1b2c3");
214 }
215
216 #[test]
217 fn test_complement_multi_early() {
218 new_ucmd!()
219 .args(&["-c", "-c", "a", "X"])
220 .pipe_in("ab")
221 .succeeds()
222 .stdout_is("aX");
223 }
224
225 #[test]
226 fn test_complement_multi_middle() {
227 new_ucmd!()
228 .args(&["-c", "a", "-c", "X"])
229 .fails()
230 .stderr_contains("tr: extra operand 'X'");
231 }
232
233 #[test]
234 fn test_complement_multi_late() {
235 new_ucmd!()
236 .args(&["-c", "a", "X", "-c"])
237 .fails()
238 .stderr_contains("tr: extra operand '-c'");
239 }
240
241 #[test]
242 fn test_squeeze() {
243 new_ucmd!()
244 .args(&["-s", "a-z"])
245 .pipe_in("aaBBcDcc")
246 .succeeds()
247 .stdout_is("aBBcDc");
248 }
249
250 #[test]
251 fn test_squeeze_multi() {
252 new_ucmd!()
253 .args(&["-ss", "-s", "a-z"])
254 .pipe_in("aaBBcDcc")
255 .succeeds()
256 .stdout_is("aBBcDc");
257 }
258
259 #[test]
260 fn test_squeeze_complement() {
261 new_ucmd!()
262 .args(&["-sc", "a-z"])
263 .pipe_in("aaBBcDcc")
264 .succeeds()
265 .stdout_is("aaBcDcc");
266 }
267
268 #[test]
269 fn test_squeeze_complement_multi() {
270 new_ucmd!()
271 .args(&["-scsc", "a-z"]) // spell-checker:disable-line
272 .pipe_in("aaBBcDcc")
273 .succeeds()
274 .stdout_is("aaBcDcc");
275 }
276
277 #[test]
278 fn test_squeeze_complement_two_sets() {
279 new_ucmd!()
280 .args(&["-sc", "a", "_"])
281 .pipe_in("test a aa with 3 ___ spaaaces +++") // spell-checker:disable-line
282 .succeeds()
283 .stdout_is("_a_aa_aaa_");
284 }
285
286 #[test]
287 fn test_translate_and_squeeze() {
288 new_ucmd!()
289 .args(&["-s", "x", "y"])
290 .pipe_in("xx")
291 .succeeds()
292 .stdout_is("y");
293 }
294
295 #[test]
296 fn test_translate_and_squeeze_multiple_lines() {
297 new_ucmd!()
298 .args(&["-s", "x", "y"])
299 .pipe_in("xxaax\nxaaxx") // spell-checker:disable-line
300 .succeeds()
301 .stdout_is("yaay\nyaay"); // spell-checker:disable-line
302 }
303
304 #[test]
305 fn test_delete_and_squeeze_one_set() {
306 new_ucmd!()
307 .args(&["-ds", "a-z"])
308 .fails()
309 .stderr_contains("missing operand after 'a-z'")
310 .stderr_contains("Two strings must be given when deleting and squeezing.");
311 }
312
313 #[test]
314 fn test_delete_and_squeeze() {
315 new_ucmd!()
316 .args(&["-ds", "a-z", "A-Z"])
317 .pipe_in("abBcB")
318 .succeeds()
319 .stdout_is("B");
320 }
321
322 #[test]
323 fn test_delete_and_squeeze_complement() {
324 new_ucmd!()
325 .args(&["-dsc", "a-z", "A-Z"])
326 .pipe_in("abBcB")
327 .succeeds()
328 .stdout_is("abc");
329 }
330
331 #[test]
332 fn test_delete_and_squeeze_complement_squeeze_set2() {
333 new_ucmd!()
334 .args(&["-dsc", "abX", "XYZ"])
335 .pipe_in("abbbcdddXXXYYY")
336 .succeeds()
337 .stdout_is("abbbX");
338 }
339
340 #[test]
341 fn test_set1_longer_than_set2() {
342 new_ucmd!()
343 .args(&["abc", "xy"])
344 .pipe_in("abcde")
345 .succeeds()
346 .stdout_is("xyyde"); // spell-checker:disable-line
347 }
348
349 #[test]
350 fn test_set1_shorter_than_set2() {
351 new_ucmd!()
352 .args(&["ab", "xyz"])
353 .pipe_in("abcde")
354 .succeeds()
355 .stdout_is("xycde");
356 }
357
358 #[test]
359 fn test_truncate() {
360 // echo -n "abcde" | tr -t "abc" "xy"
361 new_ucmd!()
362 .args(&["-t", "abc", "xy"])
363 .pipe_in("abcde")
364 .succeeds()
365 .stdout_is("xycde");
366 }
367
368 #[test]
369 fn test_truncate_multi() {
370 new_ucmd!()
371 .args(&["-tt", "-t", "abc", "xy"])
372 .pipe_in("abcde")
373 .succeeds()
374 .stdout_is("xycde");
375 }
376
377 #[test]
378 fn test_truncate_with_set1_shorter_than_set2() {
379 new_ucmd!()
380 .args(&["-t", "ab", "xyz"])
381 .pipe_in("abcde")
382 .succeeds()
383 .stdout_is("xycde");
384 }
385
386 #[test]
387 fn test_truncate_applies_before_complement_with_class() {
388 new_ucmd!()
389 .args(&["-ct", "[:digit:]", "X"])
390 .ignore_stdin_write_error()
391 .pipe_in("A")
392 .fails()
393 .stderr_contains("when translating with complemented character classes,\nstring2 must map all characters in the domain to one");
394 }
395
396 #[test]
397 fn missing_args_fails() {
398 let (_, mut ucmd) = at_and_ucmd!();
399 ucmd.fails().stderr_contains("missing operand");
400 }
401
402 #[test]
403 fn missing_required_second_arg_fails() {
404 let (_, mut ucmd) = at_and_ucmd!();
405 ucmd.args(&["foo"])
406 .fails()
407 .stderr_contains("missing operand after");
408 }
409
410 #[test]
411 fn test_interpret_backslash_escapes() {
412 new_ucmd!()
413 .args(&["abfnrtv", r"\a\b\f\n\r\t\v"]) // spell-checker:disable-line
414 .pipe_in("abfnrtv") // spell-checker:disable-line
415 .succeeds()
416 .stdout_is("\u{7}\u{8}\u{c}\n\r\t\u{b}");
417 }
418
419 #[test]
420 fn test_interpret_unrecognized_backslash_escape_as_character() {
421 new_ucmd!()
422 .args(&["qcz+=~-", r"\q\c\z\+\=\~\-"])
423 .pipe_in("qcz+=~-")
424 .succeeds()
425 .stdout_is("qcz+=~-");
426 }
427
428 #[test]
429 fn test_interpret_single_octal_escape() {
430 new_ucmd!()
431 .args(&["X", r"\015"])
432 .pipe_in("X")
433 .succeeds()
434 .stdout_is("\r");
435 }
436
437 #[test]
438 fn test_interpret_one_and_two_digit_octal_escape() {
439 new_ucmd!()
440 .args(&["XYZ", r"\0\11\77"])
441 .pipe_in("XYZ")
442 .succeeds()
443 .stdout_is("\0\t?");
444 }
445
446 #[test]
447 fn test_octal_escape_is_at_most_three_digits() {
448 new_ucmd!()
449 .args(&["XY", r"\0156"])
450 .pipe_in("XY")
451 .succeeds()
452 .stdout_is("\r6");
453 }
454
455 #[test]
456 fn test_non_octal_digit_ends_escape() {
457 new_ucmd!()
458 .args(&["rust", r"\08\11956"])
459 .pipe_in("rust")
460 .succeeds()
461 .stdout_is("\08\t9");
462 }
463
464 #[test]
465 fn test_interpret_backslash_at_eol_literally() {
466 new_ucmd!()
467 .args(&["X", r"\"])
468 .pipe_in("X")
469 .succeeds()
470 .stdout_is("\\");
471 }
472
473 #[test]
474 fn test_more_than_2_sets() {
475 new_ucmd!().args(&["'abcdef'", "'a'", "'b'"]).fails();
476 }
477
478 #[test]
479 fn basic_translation_works() {
480 // echo -n "abcdefabcdef" | tr "dabcdef" "xyz"
481 new_ucmd!()
482 .args(&["abcdef", "xyz"])
483 .pipe_in("abcdefabcdef")
484 .succeeds()
485 .stdout_is("xyzzzzxyzzzz");
486 }
487
488 #[test]
489 fn alnum_overrides_translation_to_fallback_1() {
490 // echo -n "abcdefghijklmnopqrstuvwxyz" | tr "abc[:alpha:]" "xyz"
491 new_ucmd!()
492 .args(&["abc[:alpha:]", "xyz"])
493 .pipe_in("abcdefghijklmnopqrstuvwxyz")
494 .succeeds()
495 .stdout_is("zzzzzzzzzzzzzzzzzzzzzzzzzz");
496 }
497
498 #[test]
499 fn alnum_overrides_translation_to_fallback_2() {
500 // echo -n "abcdefghijklmnopqrstuvwxyz" | tr "[:alpha:]abc" "xyz"
501 new_ucmd!()
502 .args(&["[:alpha:]abc", "xyz"])
503 .pipe_in("abcdefghijklmnopqrstuvwxyz")
504 .succeeds()
505 .stdout_is("zzzzzzzzzzzzzzzzzzzzzzzzzz");
506 }
507
508 #[test]
509 fn overrides_translation_pair_if_repeats() {
510 // echo -n 'aaa' | tr "aaa" "xyz"
511 new_ucmd!()
512 .args(&["aaa", "xyz"])
513 .pipe_in("aaa")
514 .succeeds()
515 .stdout_is("zzz");
516 }
517
518 #[test]
519 fn uppercase_conversion_works_1() {
520 // echo -n 'abcdefghijklmnopqrstuvwxyz' | tr "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
521 new_ucmd!()
522 .args(&["abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"])
523 .pipe_in("abcdefghijklmnopqrstuvwxyz")
524 .succeeds()
525 .stdout_is("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
526 }
527
528 #[test]
529 fn uppercase_conversion_works_2() {
530 // echo -n 'abcdefghijklmnopqrstuvwxyz' | tr "a-z" "A-Z"
531 new_ucmd!()
532 .args(&["a-z", "A-Z"])
533 .pipe_in("abcdefghijklmnopqrstuvwxyz")
534 .succeeds()
535 .stdout_is("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
536 }
537
538 #[test]
539 fn uppercase_conversion_works_3() {
540 // echo -n 'abcdefghijklmnopqrstuvwxyz' | tr "[:lower:]" "[:upper:]"
541 new_ucmd!()
542 .args(&["[:lower:]", "[:upper:]"])
543 .pipe_in("abcdefghijklmnopqrstuvwxyz")
544 .succeeds()
545 .stdout_is("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
546 }
547
548 #[test]
549 fn translate_complement_set_in_order() {
550 // echo -n '01234' | tr -c '@-~' ' -^'
551 new_ucmd!()
552 .args(&["-c", "@-~", " -^"])
553 .pipe_in("01234")
554 .succeeds()
555 .stdout_is("PQRST");
556 }
557
558 #[test]
559 fn alpha_expands_uppercase_lowercase() {
560 // echo -n "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | tr "[:alpha:]" " -_"
561 new_ucmd!()
562 .args(&["[:alpha:]", " -_"])
563 .pipe_in("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
564 .succeeds()
565 .stdout_is(r##" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS"##);
566 }
567
568 #[test]
569 fn alnum_expands_number_uppercase_lowercase() {
570 // echo -n "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | tr "[:alnum:]" " -_"
571 new_ucmd!()
572 .args(&["[:alnum:]", " -_"])
573 .pipe_in("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
574 .succeeds()
575 .stdout_is(r##" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]"##);
576 }
577
578 #[test]
579 fn tr_translate_range_to_repeat_class_zero() {
580 new_ucmd!()
581 .args(&["abcd", "[]*]"])
582 .pipe_in("abcd")
583 .succeeds()
584 .stdout_is("]]]]");
585 }
586
587 #[test]
588 fn tr_translate_range_to_repeat_class() {
589 new_ucmd!()
590 .args(&["rst", "[%*]uvw"])
591 .pipe_in("rst")
592 .succeeds()
593 .stdout_is("uvw");
594 }
595
596 #[test]
597 fn tr_translate_empty_set1_noop() {
598 new_ucmd!()
599 .args(&["", "[.*]"])
600 .pipe_in("rst")
601 .succeeds()
602 .stdout_is("rst");
603 }
604
605 #[test]
606 fn tr_truncate_set1_longer_than_set2() {
607 // # Test --truncate-set1 behavior when string1 is longer than string2
608 new_ucmd!()
609 .args(&["-t", "mnop", "pq"])
610 .pipe_in("mnopq")
611 .succeeds()
612 .stdout_is("pqopq");
613 }
614
615 #[test]
616 fn tr_bsd_set1_longer_extends_last_char() {
617 // # Test bsd behavior (the default) when string1 is longer than string2
618 new_ucmd!()
619 .args(&["mnop", "pq"])
620 .pipe_in("mnopq")
621 .succeeds()
622 .stdout_is("pqqqq");
623 }
624
625 #[test]
626 fn tr_posix_repeat_class_extension() {
627 // # Do it the posix way
628 new_ucmd!()
629 .args(&["mnop", "p[q*]"])
630 .pipe_in("mnopq")
631 .succeeds()
632 .stdout_is("pqqqq");
633 }
634
635 #[test]
636 fn tr_squeeze_range_to_special_chars() {
637 new_ucmd!()
638 .args(&["-s", "a-p", "%[.*]$"])
639 .pipe_in("abcdefghijklmnop")
640 .succeeds()
641 .stdout_is("%.$");
642 }
643
644 #[test]
645 fn tr_squeeze_range_to_repeat_class() {
646 new_ucmd!()
647 .args(&["-s", "a-p", "[.*]$"])
648 .pipe_in("abcdefghijklmnop")
649 .succeeds()
650 .stdout_is(".$");
651 }
652
653 #[test]
654 fn tr_squeeze_range_to_class_prefix() {
655 new_ucmd!()
656 .args(&["-s", "a-p", "%[.*]"])
657 .pipe_in("abcdefghijklmnop")
658 .succeeds()
659 .stdout_is("%.");
660 }
661
662 #[test]
663 fn tr_squeeze_char_class_alnum() {
664 new_ucmd!()
665 .args(&["-s", "[p-z]"])
666 .pipe_in("ppqqrr")
667 .succeeds()
668 .stdout_is("pqr");
669 }
670
671 #[test]
672 fn tr_squeeze_explicit_range() {
673 new_ucmd!()
674 .args(&["-s", "[p-r]"])
675 .pipe_in("ppqqrr")
676 .succeeds()
677 .stdout_is("pqr");
678 }
679
680 #[test]
681 fn tr_squeeze_partial_range() {
682 new_ucmd!()
683 .args(&["-s", "[p-q]"])
684 .pipe_in("ppqqrr")
685 .succeeds()
686 .stdout_is("pqrr");
687 }
688
689 #[test]
690 fn tr_squeeze_tail_range() {
691 new_ucmd!()
692 .args(&["-s", "[q-r]"])
693 .pipe_in("ppqqrr")
694 .succeeds()
695 .stdout_is("ppqr");
696 }
697
698 #[test]
699 fn tr_squeeze_nul_range() {
700 new_ucmd!()
701 .args(&["-s", r"[\0-\5]"])
702 .pipe_in(
703 "\u{0}\u{0}a\u{1}\u{1}b\u{2}\u{2}\u{2}c\u{3}\u{3}\u{3}d\u{4}\u{4}\u{4}\u{4}e\u{5}\u{5}",
704 )
705 .succeeds()
706 .stdout_is("\u{0}a\u{1}b\u{2}c\u{3}d\u{4}e\u{5}");
707 }
708
709 #[test]
710 fn tr_delete_equivalence_class_open() {
711 // # tests of delete
712 new_ucmd!()
713 .args(&["-d", "[=[=]"])
714 .pipe_in("[[[[[[[]]]]]]]]")
715 .succeeds()
716 .stdout_is("]]]]]]]]");
717 }
718
719 #[test]
720 fn tr_delete_equivalence_class_close() {
721 new_ucmd!()
722 .args(&["-d", "[=]=]"])
723 .pipe_in("[[[[[[[]]]]]]]]")
724 .succeeds()
725 .stdout_is("[[[[[[[");
726 }
727
728 #[test]
729 fn tr_delete_xdigit_all() {
730 new_ucmd!()
731 .args(&["-d", "[:xdigit:]"])
732 .pipe_in("0123456789acbdefABCDEF")
733 .succeeds()
734 .no_output();
735 }
736
737 #[test]
738 fn tr_delete_xdigit_leaves_non_hex() {
739 new_ucmd!()
740 .args(&["-d", "[:xdigit:]"])
741 .pipe_in("w0x1y2z3456789acbdefABCDEFz")
742 .succeeds()
743 .stdout_is("wxyzz");
744 }
745
746 #[test]
747 fn tr_delete_digit_all() {
748 new_ucmd!()
749 .args(&["-d", "[:digit:]"])
750 .pipe_in("0123456789")
751 .succeeds()
752 .no_output();
753 }
754
755 #[test]
756 fn tr_delete_digit_leaves_alpha() {
757 new_ucmd!()
758 .args(&["-d", "[:digit:]"])
759 .pipe_in("a0b1c2d3e4f5g6h7i8j9k")
760 .succeeds()
761 .stdout_is("abcdefghijk");
762 }
763
764 #[test]
765 fn tr_delete_lower_all() {
766 new_ucmd!()
767 .args(&["-d", "[:lower:]"])
768 .pipe_in("abcdefghijklmnopqrstuvwxyz")
769 .succeeds()
770 .no_output();
771 }
772
773 #[test]
774 fn tr_delete_upper_all() {
775 new_ucmd!()
776 .args(&["-d", "[:upper:]"])
777 .pipe_in("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
778 .succeeds()
779 .no_output();
780 }
781
782 #[test]
783 fn tr_delete_alpha_all() {
784 new_ucmd!()
785 .args(&["-d", "[:lower:][:upper:]"])
786 .pipe_in("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
787 .succeeds()
788 .no_output();
789 }
790
791 #[test]
792 fn tr_delete_alnum_all() {
793 new_ucmd!()
794 .args(&["-d", "[:alpha:]"])
795 .pipe_in("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
796 .succeeds()
797 .no_output();
798 }
799
800 #[test]
801 fn tr_delete_space_class() {
802 new_ucmd!()
803 .args(&["-d", "[:alnum:]"])
804 .pipe_in("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
805 .succeeds()
806 .no_output();
807 }
808
809 #[test]
810 fn tr_delete_complement_alnum() {
811 new_ucmd!()
812 .args(&["-d", "[:alnum:]"])
813 .pipe_in(".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.")
814 .succeeds()
815 .stdout_is("..");
816 }
817
818 #[test]
819 fn tr_delete_complement_alpha() {
820 new_ucmd!()
821 .args(&["-ds", "[:alnum:]", "."])
822 .pipe_in(".abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.")
823 .succeeds()
824 .stdout_is(".");
825 }
826
827 #[test]
828 fn tr_squeeze_complement_alnum_to_newline_bsd() {
829 // # The classic example, with string2 BSD-style
830 new_ucmd!()
831 .args(&["-cs", "[:alnum:]", "\n"])
832 .pipe_in("The quick brown fox jumps over the lazy dog.")
833 .succeeds()
834 .stdout_is("The\nquick\nbrown\nfox\njumps\nover\nthe\nlazy\ndog\n");
835 }
836
837 #[test]
838 fn tr_squeeze_complement_alnum_to_newline_posix() {
839 // # The classic example, POSIX-style
840 new_ucmd!()
841 .args(&["-cs", "[:alnum:]", "[\n*]"])
842 .pipe_in("The big black fox jumped over the fence.")
843 .succeeds()
844 .stdout_is("The\nbig\nblack\nfox\njumped\nover\nthe\nfence\n");
845 }
846
847 #[test]
848 fn tr_delete_squeeze_combined() {
849 new_ucmd!()
850 .args(&["-ds", "q", "p"])
851 .pipe_in("ppqqpp")
852 .succeeds()
853 .stdout_is("p");
854 }
855
856 #[test]
857 fn tr_delete_squeeze_xdigit() {
858 new_ucmd!()
859 .args(&["-ds", "[:xdigit:]", "Z"])
860 .pipe_in("ZZ0123456789acbdefABCDEFZZ")
861 .succeeds()
862 .stdout_is("Z");
863 }
864
865 #[test]
866 fn tr_translate_alnum_to_rot13_plus_space() {
867 // # Try some data with 8th bit set in case something is mistakenly
868 // # sign-extended.
869 new_ucmd!()
870 .arg("-ds")
871 .args(&["\\350", "\\345"])
872 .pipe_in([0o300, 0o301, 0o377, 0o345, 0o345, 0o350, 0o345])
873 .succeeds()
874 .stdout_is_bytes([0o300, 0o301, 0o377, 0o345]);
875 }
876
877 #[test]
878 fn tr_squeeze_alnum_remove_spaces() {
879 new_ucmd!()
880 .args(&["-s", "abcdefghijklmn", "[:*016]"])
881 .pipe_in("abcdefghijklmnop")
882 .succeeds()
883 .stdout_is(":op");
884 }
885
886 #[test]
887 fn tr_translate_digits_to_letters() {
888 new_ucmd!()
889 .args(&["-d", "a-z"])
890 .pipe_in("abc $code")
891 .succeeds()
892 .stdout_is(" $");
893 }
894
895 #[test]
896 fn tr_translate_control_chars() {
897 new_ucmd!()
898 .args(&["-ds", "a-z", "$."])
899 .pipe_in("a.b.c $$$$code\\")
900 .succeeds()
901 .stdout_is(". $\\");
902 }
903
904 #[test]
905 fn tr_translate_single_char_range() {
906 // # Make sure that a-a is accepted.
907 new_ucmd!()
908 .args(&["a-a", "z"])
909 .pipe_in("abc")
910 .succeeds()
911 .stdout_is("zbc");
912 }
913
914 #[test]
915 fn tr_translate_with_null_byte() {
916 // {ERR=>"$prog: when not truncating set1, string2 must be non-empty\n"}],
917 new_ucmd!()
918 .args(&["a", ""])
919 .pipe_in("")
920 .fails()
921 .stderr_is("tr: when not truncating set1, string2 must be non-empty\n");
922 }
923
924 #[test]
925 fn tr_translate_lower_to_upper() {
926 new_ucmd!()
927 .args(&["[:lower:]", "[:upper:]"])
928 .pipe_in("abcxyzABCXYZ")
929 .succeeds()
930 .stdout_is("ABCXYZABCXYZ");
931 }
932
933 #[test]
934 fn tr_translate_upper_to_lower() {
935 new_ucmd!()
936 .args(&["[:upper:]", "[:lower:]"])
937 .pipe_in("abcxyzABCXYZ")
938 .succeeds()
939 .stdout_is("abcxyzabcxyz");
940 }
941
942 #[test]
943 fn tr_repeat_complement_char_class() {
944 new_ucmd!()
945 .args(&["a[=*2][=c=]", "xyyz"])
946 .pipe_in("a=c")
947 .succeeds()
948 .stdout_is("xyz");
949 }
950
951 #[test]
952 fn tr_repeat_class_in_set2_basic() {
953 new_ucmd!()
954 .args(&["[:*3][:digit:]", "a-m"])
955 .pipe_in(":1239")
956 .succeeds()
957 .stdout_is("cefgm");
958 }
959
960 #[test]
961 fn tr_repeat_class_extends_set2() {
962 new_ucmd!()
963 .args(&["a[b*512]c", "1[x*]2"])
964 .pipe_in("abc")
965 .succeeds()
966 .stdout_is("1x2");
967 }
968
969 #[test]
970 fn tr_repeat_class_with_squeeze() {
971 new_ucmd!()
972 .args(&["a[b*513]c", "1[x*]2"])
973 .pipe_in("abc")
974 .succeeds()
975 .stdout_is("1x2");
976 }
977
978 #[test]
979 fn tr_translate_overlap_repeat() {
980 // # Another couple octal repeat count tests.
981 // {ERR=>"$prog: invalid repeat count '08' in [c*n] construct\n"}],
982 new_ucmd!()
983 .args(&["[b*08]", "[x*]"])
984 .pipe_in("")
985 .fails()
986 .stderr_is("tr: invalid repeat count '08' in [c*n] construct\n");
987 }
988
989 #[test]
990 fn tr_translate_overlap_repeat_squeeze() {
991 new_ucmd!()
992 .args(&["[b*010]cd", "[a*7]BC[x*]"])
993 .pipe_in("bcd")
994 .succeeds()
995 .stdout_is("BCx");
996 }
997
998 #[test]
999 fn octal_repeat_count_test() {
1000 //below will result in 8'x' and 4'y' as octal 010 = decimal 8
1001 new_ucmd!()
1002 .args(&["ABCdefghijkl", "[x*010]Y"])
1003 .pipe_in("ABCdefghijklmn12")
1004 .succeeds()
1005 .stdout_is("xxxxxxxxYYYYmn12");
1006 }
1007
1008 #[test]
1009 fn non_octal_repeat_count_test() {
1010 //below will result in 10'x' and 2'y' as the 10 does not have 0 prefix
1011 new_ucmd!()
1012 .args(&["ABCdefghijkl", "[x*10]Y"])
1013 .pipe_in("ABCdefghijklmn12")
1014 .succeeds()
1015 .stdout_is("xxxxxxxxxxYYmn12");
1016 }
1017
1018 #[test]
1019 fn tr_translate_with_escape_sequence() {
1020 new_ucmd!()
1021 .args(&[r"a\-z", "A-Z"])
1022 .pipe_in("abc-z")
1023 .succeeds()
1024 .stdout_is("AbcBC");
1025 }
1026
1027 #[test]
1028 fn tr_translate_octal_backslash() {
1029 new_ucmd!()
1030 .args(&["a\u{055}b", "def"])
1031 .pipe_in("a\u{055}b")
1032 .succeeds()
1033 .stdout_is("def");
1034 }
1035
1036 #[test]
1037 // Fails on Windows because it will not separate '\' and 'x' as separate arguments
1038 #[cfg(unix)]
1039 fn tr_translate_backslash_at_end() {
1040 // {ERR=>"$prog: warning: an unescaped backslash at end of "
1041 // . "string is not portable\n"}],
1042 new_ucmd!()
1043 .args(&[r"\", "x"])
1044 .pipe_in(r"\")
1045 .succeeds()
1046 .stdout_is("x")
1047 .stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n");
1048 }
1049
1050 #[test]
1051 fn tr_ross_delete_no_squeeze() {
1052 // # From Ross
1053 // {ERR=>$map_all_to_1}],
1054 new_ucmd!()
1055 .args(&["-cs", "[:upper:]", "X[Y*]"])
1056 .pipe_in("")
1057 .fails()
1058 .stderr_is("tr: when translating with complemented character classes,\nstring2 must map all characters in the domain to one\n");
1059 }
1060
1061 #[test]
1062 fn tr_ross_delete_with_squeeze() {
1063 // {ERR=>$map_all_to_1}],
1064 new_ucmd!()
1065 .args(&["-cs", "[:cntrl:]", "X[Y*]"])
1066 .pipe_in("")
1067 .fails()
1068 .stderr_is("tr: when translating with complemented character classes,\nstring2 must map all characters in the domain to one\n");
1069 }
1070
1071 #[test]
1072 fn tr_ross_translate_complement() {
1073 new_ucmd!()
1074 .args(&["-cs", "[:upper:]", "[X*]"])
1075 .pipe_in("AMZamz123.-+AMZ")
1076 .succeeds()
1077 .stdout_is("AMZXAMZ");
1078 }
1079
1080 #[test]
1081 fn tr_ross_translate_complement_squeeze() {
1082 new_ucmd!()
1083 .args(&["-cs", "[:upper:][:digit:]", "[Z*]"])
1084 .pipe_in("")
1085 .succeeds()
1086 .no_output();
1087 }
1088
1089 #[test]
1090 fn tr_ross_delete_complement() {
1091 new_ucmd!()
1092 .args(&["-dcs", "[:lower:]", "n-rs-z"])
1093 .pipe_in("amzAMZ123.-+amz")
1094 .succeeds()
1095 .stdout_is("amzamz");
1096 }
1097
1098 #[test]
1099 fn tr_ross_delete_complement_squeeze() {
1100 new_ucmd!()
1101 .args(&["-ds", "[:xdigit:]", "[:alnum:]"])
1102 .pipe_in(".ZABCDEFGzabcdefg.0123456788899.GG")
1103 .succeeds()
1104 .stdout_is(".ZGzg..G");
1105 }
1106
1107 #[test]
1108 fn tr_ross_translate_overlong() {
1109 new_ucmd!()
1110 .args(&["-dcs", "[:alnum:]", "[:digit:]"])
1111 .pipe_in("")
1112 .succeeds()
1113 .no_output();
1114 }
1115
1116 #[test]
1117 fn tr_ross_translate_squeeze_overlong() {
1118 new_ucmd!()
1119 .args(&["-dc", "[:lower:]"])
1120 .pipe_in("")
1121 .succeeds()
1122 .no_output();
1123 }
1124
1125 #[test]
1126 fn tr_ross_translate_squeeze_delete() {
1127 new_ucmd!()
1128 .args(&["-dc", "[:upper:]"])
1129 .pipe_in("")
1130 .succeeds()
1131 .no_output();
1132 }
1133
1134 #[test]
1135 fn tr_error_empty_equivalence_class() {
1136 // # Ensure that these fail.
1137 // # Prior to 2.0.20, each would evoke a failed assertion.
1138 // {ERR=>"$prog: missing equivalence class character '[==]'\n"}],
1139 new_ucmd!()
1140 .args(&["[==]", "x"])
1141 .pipe_in("")
1142 .fails()
1143 .stderr_is("tr: missing equivalence class character '[==]'\n");
1144 }
1145
1146 #[test]
1147 fn check_too_many_chars_in_eq() {
1148 new_ucmd!()
1149 .args(&["-d", "[=aa=]"])
1150 .pipe_in("")
1151 .fails()
1152 .stderr_contains("aa: equivalence class operand must be a single character\n");
1153 }
1154
1155 #[test]
1156 fn tr_error_empty_char_class() {
1157 // {ERR=>"$prog: missing character class name '[::]'\n"}],
1158 new_ucmd!()
1159 .args(&["[::]", "x"])
1160 .pipe_in("")
1161 .fails()
1162 .stderr_is("tr: missing character class name '[::]'\n");
1163 }
1164
1165 #[test]
1166 fn tr_error_invalid_char_class() {
1167 new_ucmd!()
1168 .args(&["[:fooclass:]", "x"])
1169 .pipe_in("")
1170 .fails()
1171 .stderr_is("tr: invalid character class 'fooclass'\n");
1172 }
1173
1174 #[test]
1175 fn tr_error_repeat_in_set1() {
1176 new_ucmd!()
1177 .args(&["[a*]", "a"])
1178 .pipe_in("")
1179 .fails()
1180 .stderr_is("tr: the [c*] repeat construct may not appear in string1\n");
1181 }
1182
1183 #[test]
1184 fn tr_translate_repeat_in_set2() {
1185 new_ucmd!()
1186 .args(&["a", "[a*][a*]"])
1187 .pipe_in("")
1188 .fails()
1189 .stderr_is("tr: only one [c*] repeat construct may appear in string2\n");
1190 }
1191
1192 #[test]
1193 fn tr_translate_repeat_octal() {
1194 // # Weird repeat counts.
1195 new_ucmd!()
1196 .args(&["abc", r"[b*\9]"])
1197 .pipe_in("abcd")
1198 .succeeds()
1199 .stdout_is("[b*d");
1200 }
1201
1202 #[test]
1203 fn tr_translate_repeat_zero_count() {
1204 new_ucmd!()
1205 .args(&["abc", "[b*0]"])
1206 .pipe_in("abcd")
1207 .succeeds()
1208 .stdout_is("bbbd");
1209 }
1210
1211 #[test]
1212 fn tr_translate_repeat_multiple_zeros() {
1213 new_ucmd!()
1214 .args(&["abc", "[b*00000000000000000000]"])
1215 .pipe_in("abcd")
1216 .succeeds()
1217 .stdout_is("bbbd");
1218 }
1219
1220 #[test]
1221 fn tr_translate_repeat_complement() {
1222 new_ucmd!()
1223 .args(&["-c", "[a*65536]\n", "[b*]"])
1224 .pipe_in("abcd")
1225 .succeeds()
1226 .stdout_is("abbb");
1227 }
1228
1229 #[test]
1230 fn tr_translate_repeat_x_complement() {
1231 new_ucmd!()
1232 .args(&["-C", "[a*65536]\n", "[b*]"])
1233 .pipe_in("abcd")
1234 .succeeds()
1235 .stdout_is("abbb");
1236 }
1237
1238 #[test]
1239 fn tr_fowler_translate_basic() {
1240 // # From Glenn Fowler.
1241 new_ucmd!()
1242 .args(&["ah", "-H"])
1243 .pipe_in("aha")
1244 .succeeds()
1245 .stdout_is("-H-");
1246 }
1247
1248 #[test]
1249 fn tr_translate_no_abort_on_long_input() {
1250 // # Up to coreutils-6.9, this would provoke a failed assertion.
1251 new_ucmd!()
1252 .args(&["-c", "a", "[b*256]"])
1253 .pipe_in("abc")
1254 .succeeds()
1255 .stdout_is("abb");
1256 }
1257
1258 #[test]
1259 fn test_delete_flag_takes_only_one_operand() {
1260 // gnu tr -d fails with more than 1 argument
1261 new_ucmd!().args(&["-d", "a", "p"]).fails().stderr_contains(
1262 "extra operand 'p'\nOnly one string may be given when deleting without squeezing repeats.",
1263 );
1264 }
1265
1266 #[test]
1267 fn test_truncate_flag_fails_with_more_than_two_operand() {
1268 new_ucmd!()
1269 .args(&["-t", "a", "b", "c"])
1270 .fails()
1271 .stderr_contains("extra operand 'c'");
1272 }
1273
1274 #[test]
1275 fn test_squeeze_flag_fails_with_more_than_two_operand() {
1276 new_ucmd!()
1277 .args(&["-s", "a", "b", "c"])
1278 .fails()
1279 .stderr_contains("extra operand 'c'");
1280 }
1281
1282 #[test]
1283 fn test_complement_flag_fails_with_more_than_two_operand() {
1284 new_ucmd!()
1285 .args(&["-c", "a", "b", "c"])
1286 .fails()
1287 .stderr_contains("extra operand 'c'");
1288 }
1289
1290 #[test]
1291 fn check_regression_class_space() {
1292 // This invocation checks:
1293 // 1. that the [:space:] class has exactly 6 characters,
1294 // 2. that the [:space:] class contains at least the given 6 characters (and therefore no other characters), and
1295 // 3. that the given characters occur in exactly this order.
1296 new_ucmd!()
1297 .args(&["[:space:][:upper:]", "123456[:lower:]"])
1298 // 0x0B = "\v" ("VERTICAL TAB")
1299 // 0x0C = "\f" ("FEED FORWARD")
1300 .pipe_in("A\t\n\u{0B}\u{0C}\r B")
1301 .succeeds()
1302 .no_stderr()
1303 .stdout_only("a123456b");
1304 }
1305
1306 #[test]
1307 fn check_regression_class_blank() {
1308 // This invocation checks:
1309 // 1. that the [:blank:] class has exactly 2 characters,
1310 // 2. that the [:blank:] class contains at least the given 2 characters (and therefore no other characters), and
1311 // 3. that the given characters occur in exactly this order.
1312 new_ucmd!()
1313 .args(&["[:blank:][:upper:]", "12[:lower:]"])
1314 .pipe_in("A\t B")
1315 .succeeds()
1316 .no_stderr()
1317 .stdout_only("a12b");
1318 }
1319
1320 // Check regression found in https://github.com/uutils/coreutils/issues/6163
1321 #[test]
1322 fn check_regression_issue_6163_no_match() {
1323 new_ucmd!()
1324 .args(&["-c", "-t", "Y", "Z"])
1325 .pipe_in("X\n")
1326 .succeeds()
1327 .no_stderr()
1328 .stdout_only("X\n");
1329 }
1330
1331 #[test]
1332 fn check_regression_issue_6163_match() {
1333 new_ucmd!()
1334 .args(&["-c", "-t", "Y", "Z"])
1335 .pipe_in("\0\n")
1336 .succeeds()
1337 .no_stderr()
1338 .stdout_only("Z\n");
1339 }
1340
1341 #[test]
1342 fn check_ignore_truncate_when_deleting_and_squeezing() {
1343 new_ucmd!()
1344 .args(&["-dts", "asdf", "qwe"])
1345 .pipe_in("asdfqqwweerr\n")
1346 .succeeds()
1347 .no_stderr()
1348 .stdout_only("qwerr\n");
1349 }
1350
1351 #[test]
1352 fn check_ignore_truncate_when_deleting() {
1353 new_ucmd!()
1354 .args(&["-dt", "asdf"])
1355 .pipe_in("asdfqwer\n")
1356 .succeeds()
1357 .no_stderr()
1358 .stdout_only("qwer\n");
1359 }
1360
1361 #[test]
1362 fn check_ignore_truncate_when_squeezing() {
1363 new_ucmd!()
1364 .args(&["-ts", "asdf"])
1365 .pipe_in("aassddffqwer\n")
1366 .succeeds()
1367 .no_stderr()
1368 .stdout_only("asdfqwer\n");
1369 }
1370
1371 #[test]
1372 fn check_disallow_blank_in_set2_when_translating() {
1373 new_ucmd!().args(&["-t", "1234", "[:blank:]"]).fails();
1374 }
1375
1376 #[test]
1377 fn check_class_in_set2_must_be_matched_in_set1() {
1378 new_ucmd!().args(&["-t", "1[:upper:]", "[:upper:]"]).fails();
1379 }
1380
1381 #[test]
1382 fn check_class_in_set2_must_be_matched_in_set1_right_length_check() {
1383 new_ucmd!()
1384 .args(&["-t", "a-z[:upper:]", "abcdefghijklmnopqrstuvwxyz[:upper:]"])
1385 .succeeds();
1386 }
1387
1388 #[test]
1389 fn check_set1_longer_set2_ends_in_class() {
1390 new_ucmd!().args(&["[:lower:]a", "[:upper:]"]).fails();
1391 }
1392
1393 #[test]
1394 fn check_set1_longer_set2_ends_in_class_with_trunc() {
1395 new_ucmd!()
1396 .args(&["-t", "[:lower:]a", "[:upper:]"])
1397 .succeeds();
1398 }
1399
1400 #[test]
1401 fn check_complement_2_unique_in_set2() {
1402 let x226 = "x".repeat(226);
1403
1404 // [y*] is expanded tp "y" here
1405 let arg = x226 + "[y*]xxx";
1406 new_ucmd!().args(&["-c", "[:upper:]", arg.as_str()]).fails();
1407 }
1408
1409 #[test]
1410 fn check_complement_1_unique_in_set2() {
1411 let x226 = "x".repeat(226);
1412
1413 // [y*] is expanded to "" here
1414 let arg = x226 + "[y*]xxxx";
1415 new_ucmd!()
1416 .args(&["-c", "[:upper:]", arg.as_str()])
1417 .succeeds();
1418 }
1419
1420 #[test]
1421 fn check_complement_set2_too_big() {
1422 let x231 = "x".repeat(231);
1423 let x230 = &x231[..230];
1424
1425 // The complement of [:upper:] expands to 230 characters,
1426 // putting more characters in set2 should fail.
1427 new_ucmd!().args(&["-c", "[:upper:]", x230]).succeeds();
1428 new_ucmd!()
1429 .args(&["-c", "[:upper:]", x231.as_str()])
1430 .fails()
1431 .stderr_contains("when translating with complemented character classes,\nstring2 must map all characters in the domain to one");
1432 }
1433
1434 #[test]
1435 #[cfg(unix)]
1436 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
1437 fn test_truncate_non_utf8_set() {
1438 let stdin = b"\x01amp\xfe\xff";
1439 let set1 = OsStr::from_bytes(b"a\xfe\xffz"); // spell-checker:disable-line
1440 let set2 = OsStr::from_bytes(b"01234");
1441
1442 new_ucmd!()
1443 .arg(set1)
1444 .arg(set2)
1445 .pipe_in(*stdin)
1446 .succeeds()
1447 .stdout_is_bytes(b"\x010mp12");
1448 }
1449
1450 #[test]
1451 #[cfg(unix)]
1452 fn test_unescaped_backslash_warning_false_positive() {
1453 // Was erroneously printing this warning (even though the backslash was escaped):
1454 // "tr: warning: an unescaped backslash at end of string is not portable"
1455 new_ucmd!()
1456 .args(&["-d", r"\\"])
1457 .pipe_in(r"a\b\c\")
1458 .succeeds()
1459 .stdout_only("abc");
1460 new_ucmd!()
1461 .args(&["-d", r"\\\\"])
1462 .pipe_in(r"a\b\c\")
1463 .succeeds()
1464 .stdout_only("abc");
1465 new_ucmd!()
1466 .args(&["-d", r"\\\\\\"])
1467 .pipe_in(r"a\b\c\")
1468 .succeeds()
1469 .stdout_only("abc");
1470 }
1471
1472 #[test]
1473 #[cfg(unix)]
1474 fn test_trailing_backslash() {
1475 new_ucmd!()
1476 .args(&["-d", r"\"])
1477 .pipe_in(r"a\b\c\")
1478 .succeeds()
1479 .stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
1480 .stdout_is("abc");
1481 new_ucmd!()
1482 .args(&["-d", r"\\\"])
1483 .pipe_in(r"a\b\c\")
1484 .succeeds()
1485 .stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
1486 .stdout_is("abc");
1487 new_ucmd!()
1488 .args(&["-d", r"\\\\\"])
1489 .pipe_in(r"a\b\c\")
1490 .succeeds()
1491 .stderr_is("tr: warning: an unescaped backslash at end of string is not portable\n")
1492 .stdout_is("abc");
1493 }
1494
1495 #[test]
1496 fn test_multibyte_octal_sequence() {
1497 new_ucmd!()
1498 .args(&["-d", r"\501"])
1499 .pipe_in("(1Ł)")
1500 .succeeds()
1501 .stderr_is("tr: warning: the ambiguous octal escape \\501 is being interpreted as the 2-byte sequence \\050, 1\n")
1502 .stdout_is("Ł)");
1503 }
1504
1505 #[test]
1506 fn test_octal_warning_still_fires_after_a_bad_sequence() {
1507 // The set fails on the character class, but the ambiguous octal escape
1508 // after it must still be warned about: the whole set is parsed even when
1509 // an earlier sequence is bad.
1510 // No `pipe_in`: the sets are rejected before stdin is ever read, so
1511 // handing the child input only races its exit.
1512 new_ucmd!()
1513 .args(&[r"[:foo:]\400", "y"])
1514 .fails()
1515 .stderr_is(
1516 "tr: warning: the ambiguous octal escape \\400 is being interpreted as the 2-byte sequence \\040, 0\n\
1517 tr: invalid character class 'foo'\n",
1518 );
1519 }
1520
1521 #[test]
1522 fn test_backwards_range() {
1523 new_ucmd!()
1524 .args(&["-d", r"\046-\048"])
1525 .pipe_in("")
1526 .fails()
1527 .stderr_only(
1528 r"tr: range-endpoints of '&-\004' are in reverse collating sequence order
1529 ",
1530 );
1531 }
1532
1533 #[test]
1534 fn test_non_digit_repeat() {
1535 new_ucmd!()
1536 .args(&["a", "[b*c]"])
1537 .pipe_in("")
1538 .fails()
1539 .stderr_only("tr: invalid repeat count 'c' in [c*n] construct\n");
1540 }
1541
1542 #[test]
1543 #[cfg(unix)]
1544 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
1545 fn test_octal_escape_ambiguous_followed_by_non_utf8() {
1546 // This case does not trigger the panic
1547 let set1 = OsStr::from_bytes(b"\\501a");
1548 new_ucmd!()
1549 .arg("-d")
1550 .arg(set1)
1551 .pipe_in("(1a)")
1552 .succeeds()
1553 .stderr_contains("warning: the ambiguous octal escape")
1554 .stdout_is(")");
1555
1556 // An user is not supposed to use this invalid utf8 set but
1557 // we would need to make the command more error-proof still
1558 let set1 = OsStr::from_bytes(b"\\501\xff");
1559 new_ucmd!()
1560 .arg("-d")
1561 .arg(set1)
1562 .pipe_in([b'(', b'1', 0xff, b')'])
1563 .succeeds()
1564 .stderr_contains("warning: invalid utf8 sequence");
1565 }
1566
1567 #[cfg(target_os = "linux")]
1568 #[test]
1569 fn test_failed_write_is_reported() {
1570 new_ucmd!()
1571 .pipe_in("hello")
1572 .args(&["e", "a"])
1573 .set_stdout(std::fs::File::create("/dev/full").unwrap())
1574 .fails()
1575 .stderr_is("tr: write error: No space left on device\n");
1576 }
1577
1578 #[test]
1579 #[cfg_attr(wasi_runner, ignore = "WASI: no pipe/signal support")]
1580 fn test_broken_pipe_no_error() {
1581 new_ucmd!()
1582 .args(&["e", "a"])
1583 .pipe_in("hello".repeat(100))
1584 .run_stdout_starts_with(b"")
1585 .fails_silently();
1586 }
1587
1588 #[cfg(unix)]
1589 #[test]
1590 fn test_stdin_is_socket() {
1591 use std::io::Write as _;
1592
1593 let (fd1, fd2) = rustix::net::socketpair(
1594 rustix::net::AddressFamily::UNIX,
1595 rustix::net::SocketType::STREAM,
1596 rustix::net::SocketFlags::empty(),
1597 None,
1598 )
1599 .unwrap();
1600 std::fs::File::from(fd1).write_all(b"::").unwrap();
1601 new_ucmd!()
1602 .args(&[":", ";"])
1603 .set_stdin(fd2)
1604 .succeeds()
1605 .stdout_is(";;");
1606 }
1607
1608 #[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))]
1609 mod diagnostics {
1610 use super::*;
1611
1612 #[cfg(unix)]
1613 #[test]
1614 fn test_snippet_points_at_the_misspelled_class() {
1615 let result = new_ucmd!()
1616 .terminal_sim_stderr()
1617 .args(&["w[:lowre:]w", "x"])
1618 .fails_with_code(1);
1619
1620 // The whole report: the `tr: ` prefix of the plain form, the argument
1621 // list echoed back, a caret covering `[:lowre:]` only — not the whole
1622 // set — and the list of valid class names.
1623 assert_eq!(
1624 result.stderr_as_displayed(),
1625 "\
1626 tr: invalid character class 'lowre'
1627 ╭─[ tr:1:5 ]
1628 │
1629 1 │ tr w[:lowre:]w x
1630 │ ─────────
1631 │
1632 │ Help: classes are alnum, alpha, blank, cntrl, digit, graph, lower, print, punct, space, upper and xdigit
1633 ───╯"
1634 );
1635 }
1636
1637 #[cfg(unix)]
1638 #[test]
1639 fn test_snippet_points_at_the_backwards_range() {
1640 let result = new_ucmd!()
1641 .terminal_sim_stderr()
1642 .args(&["qw[y-b]", "x"])
1643 .fails_with_code(1);
1644
1645 // The caret covers `y-b` alone, not the letters around the range.
1646 assert_eq!(
1647 result.stderr_as_displayed(),
1648 "\
1649 tr: range-endpoints of 'y-b' are in reverse collating sequence order
1650 ╭─[ tr:1:7 ]
1651 │
1652 1 │ tr qw[y-b] x
1653 │ ─┬─
1654 │ ╰─── did you mean 'b-y'?
1655 │
1656 │ Help: a range goes from the lower character to the higher one, as in a-z
1657 ───╯"
1658 );
1659 }
1660
1661 #[cfg(unix)]
1662 #[test]
1663 fn test_snippet_points_at_the_repeat_count() {
1664 let result = new_ucmd!()
1665 .terminal_sim_stderr()
1666 .args(&["wxy", "[z*4k]"])
1667 .fails_with_code(1);
1668
1669 assert_eq!(
1670 result.stderr_as_displayed(),
1671 "\
1672 tr: invalid repeat count '4k' in [c*n] construct
1673 ╭─[ tr:1:8 ]
1674 │
1675 1 │ tr wxy [z*4k]
1676 │ ──────
1677 │
1678 │ Help: [c*N] repeats c N times, [c*] pads SET2 to the length of SET1
1679 ───╯"
1680 );
1681 }
1682
1683 #[cfg(unix)]
1684 #[test]
1685 fn test_snippet_underlines_the_whole_set_when_the_set_is_at_fault() {
1686 let result = new_ucmd!()
1687 .terminal_sim_stderr()
1688 .args(&["w[q*]", "xyz"])
1689 .fails_with_code(1);
1690
1691 // Nothing inside the set is wrong on its own, so all of it is marked.
1692 assert_eq!(
1693 result.stderr_as_displayed(),
1694 "\
1695 tr: the [c*] repeat construct may not appear in string1
1696 ╭─[ tr:1:4 ]
1697 │
1698 1 │ tr w[q*] xyz
1699 │ ─────
1700 │
1701 │ Help: [c*N] repeats c N times, [c*] pads SET2 to the length of SET1
1702 ───╯"
1703 );
1704 }
1705
1706 #[cfg(unix)]
1707 #[test]
1708 fn test_snippet_points_into_the_second_set() {
1709 let result = new_ucmd!()
1710 .terminal_sim_stderr()
1711 .args(&["[:lower:]", "q[=we=]"])
1712 .fails_with_code(1);
1713
1714 // The set at fault is the second operand, not the first.
1715 assert_eq!(
1716 result.stderr_as_displayed(),
1717 "\
1718 tr: we: equivalence class operand must be a single character
1719 ╭─[ tr:1:15 ]
1720 │
1721 1 │ tr [:lower:] q[=we=]
1722 │ ──────
1723 │
1724 │ Help: [=c=] stands for every character equivalent to c
1725 ───╯"
1726 );
1727 }
1728
1729 #[cfg(unix)]
1730 #[test]
1731 fn test_snippet_ignores_the_program_name_as_a_set() {
1732 // The second set reads exactly like the program name; the caret must
1733 // stay on the operand, not drift onto `tr` at the start of the line.
1734 let result = new_ucmd!()
1735 .terminal_sim_stderr()
1736 .args(&["-c", "[:alpha:]", "tr"])
1737 .fails_with_code(1);
1738
1739 assert_eq!(
1740 result.stderr_as_displayed(),
1741 "\
1742 tr: when translating with complemented character classes,
1743 string2 must map all characters in the domain to one
1744 ╭─[ tr:1:17 ]
1745 │
1746 1 │ tr -c [:alpha:] tr
1747 │ ─┬
1748 │ ╰── only one character may be complemented to
1749 ───╯"
1750 );
1751 }
1752
1753 #[test]
1754 fn test_plain_message_when_stderr_is_a_pipe() {
1755 // The test harness pipes stderr, so the report must not appear.
1756 new_ucmd!()
1757 .args(&["w[:lowre:]w", "x"])
1758 .fails_with_code(1)
1759 .stderr_only("tr: invalid character class 'lowre'\n");
1760 }
1761 }