about summary refs log tree commit diff
path: root/src/libsyntax_ext/format_foreign/shell
diff options
context:
space:
mode:
authorchansuke <chansuke@georepublic.de>2019-06-16 10:12:26 +0900
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-06-16 14:17:01 +0300
commitce51e653c75e0be65cb807590109c20af9513a5e (patch)
tree9001ee9733ee3d93342ac862d8199b5e4677b334 /src/libsyntax_ext/format_foreign/shell
parentf0c7857704bb2fee0807038207436b389adae9ee (diff)
downloadrust-ce51e653c75e0be65cb807590109c20af9513a5e.tar.gz
rust-ce51e653c75e0be65cb807590109c20af9513a5e.zip
Separate libsyntax_ext module
Diffstat (limited to 'src/libsyntax_ext/format_foreign/shell')
-rw-r--r--src/libsyntax_ext/format_foreign/shell/tests.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libsyntax_ext/format_foreign/shell/tests.rs b/src/libsyntax_ext/format_foreign/shell/tests.rs
new file mode 100644
index 00000000000..8ef58b8387e
--- /dev/null
+++ b/src/libsyntax_ext/format_foreign/shell/tests.rs
@@ -0,0 +1,59 @@
+use super::{
+    Substitution as S,
+    parse_next_substitution as pns,
+};
+
+macro_rules! assert_eq_pnsat {
+    ($lhs:expr, $rhs:expr) => {
+        assert_eq!(
+            pns($lhs).and_then(|(f, _)| f.translate()),
+            $rhs.map(<String as From<&str>>::from)
+        )
+    };
+}
+
+#[test]
+fn test_escape() {
+    assert_eq!(pns("has no escapes"), None);
+    assert_eq!(pns("has no escapes, either $"), None);
+    assert_eq!(pns("*so* has a $$ escape"), Some((S::Escape((11, 13)), " escape")));
+    assert_eq!(pns("$$ leading escape"), Some((S::Escape((0, 2)), " leading escape")));
+    assert_eq!(pns("trailing escape $$"), Some((S::Escape((16, 18)), "")));
+}
+
+#[test]
+fn test_parse() {
+    macro_rules! assert_pns_eq_sub {
+        ($in_:expr, $kind:ident($arg:expr, $pos:expr)) => {
+            assert_eq!(pns(concat!($in_, "!")), Some((S::$kind($arg.into(), $pos), "!")))
+        };
+    }
+
+    assert_pns_eq_sub!("$0", Ordinal(0, (0, 2)));
+    assert_pns_eq_sub!("$1", Ordinal(1, (0, 2)));
+    assert_pns_eq_sub!("$9", Ordinal(9, (0, 2)));
+    assert_pns_eq_sub!("$N", Name("N", (0, 2)));
+    assert_pns_eq_sub!("$NAME", Name("NAME", (0, 5)));
+}
+
+#[test]
+fn test_iter() {
+    use super::iter_subs;
+    let s = "The $0'th word $$ is: `$WORD` $!\n";
+    let subs: Vec<_> = iter_subs(s, 0).map(|sub| sub.translate()).collect();
+    assert_eq!(
+        subs.iter().map(|ms| ms.as_ref().map(|s| &s[..])).collect::<Vec<_>>(),
+        vec![Some("{0}"), None, Some("{WORD}")]
+    );
+}
+
+#[test]
+fn test_translation() {
+    assert_eq_pnsat!("$0", Some("{0}"));
+    assert_eq_pnsat!("$9", Some("{9}"));
+    assert_eq_pnsat!("$1", Some("{1}"));
+    assert_eq_pnsat!("$10", Some("{1}"));
+    assert_eq_pnsat!("$stuff", Some("{stuff}"));
+    assert_eq_pnsat!("$NAME", Some("{NAME}"));
+    assert_eq_pnsat!("$PREFIX/bin", Some("{PREFIX}"));
+}