]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/format_foreign/shell/tests.rs
Rollup merge of #104772 - GuillaumeGomez:small-accessibility-improvement, r=notriddle
[rust.git] / compiler / rustc_builtin_macros / src / format_foreign / shell / tests.rs
1 use super::{parse_next_substitution as pns, Substitution as S};
2
3 macro_rules! assert_eq_pnsat {
4     ($lhs:expr, $rhs:expr) => {
5         assert_eq!(
6             pns($lhs).and_then(|(f, _)| f.translate().ok()),
7             $rhs.map(<String as From<&str>>::from)
8         )
9     };
10 }
11
12 #[test]
13 fn test_escape() {
14     assert_eq!(pns("has no escapes"), None);
15     assert_eq!(pns("has no escapes, either $"), None);
16     assert_eq!(pns("*so* has a $$ escape"), Some((S::Escape((11, 13)), " escape")));
17     assert_eq!(pns("$$ leading escape"), Some((S::Escape((0, 2)), " leading escape")));
18     assert_eq!(pns("trailing escape $$"), Some((S::Escape((16, 18)), "")));
19 }
20
21 #[test]
22 fn test_parse() {
23     macro_rules! assert_pns_eq_sub {
24         ($in_:expr, $kind:ident($arg:expr, $pos:expr)) => {
25             assert_eq!(pns(concat!($in_, "!")), Some((S::$kind($arg.into(), $pos), "!")))
26         };
27     }
28
29     assert_pns_eq_sub!("$0", Ordinal(0, (0, 2)));
30     assert_pns_eq_sub!("$1", Ordinal(1, (0, 2)));
31     assert_pns_eq_sub!("$9", Ordinal(9, (0, 2)));
32     assert_pns_eq_sub!("$N", Name("N", (0, 2)));
33     assert_pns_eq_sub!("$NAME", Name("NAME", (0, 5)));
34 }
35
36 #[test]
37 fn test_iter() {
38     use super::iter_subs;
39     let s = "The $0'th word $$ is: `$WORD` $!\n";
40     let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate().ok()).collect();
41     assert_eq!(
42         subs.iter().map(Option::as_deref).collect::<Vec<_>>(),
43         vec![Some("{0}"), None, Some("{WORD}")]
44     );
45 }
46
47 #[test]
48 fn test_translation() {
49     assert_eq_pnsat!("$0", Some("{0}"));
50     assert_eq_pnsat!("$9", Some("{9}"));
51     assert_eq_pnsat!("$1", Some("{1}"));
52     assert_eq_pnsat!("$10", Some("{1}"));
53     assert_eq_pnsat!("$stuff", Some("{stuff}"));
54     assert_eq_pnsat!("$NAME", Some("{NAME}"));
55     assert_eq_pnsat!("$PREFIX/bin", Some("{PREFIX}"));
56 }