summaryrefslogtreecommitdiffstats
path: root/vendor/gix-quote/tests/quote.rs
blob: fa8a73eda2e88791ec56e9c11c4b34e45ae07e45 (plain)
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
mod single {
    use gix_quote::single;

    #[test]
    fn empty() {
        assert_eq!(single("".into()), "''");
    }

    #[test]
    fn unquoted_becomes_quoted() {
        assert_eq!(single("a".into()), "'a'");
        assert_eq!(single("a b".into()), "'a b'");
        assert_eq!(single("a\nb".into()), "'a\nb'", "newlines play no role");
    }

    #[test]
    fn existing_exclamation_mark_gets_escaped() {
        assert_eq!(single(r"a!b".into()), r"'a'\!'b'");
        assert_eq!(single(r"!".into()), r"''\!''");
        assert_eq!(single(r"\!".into()), r"'\'\!''");
    }

    #[test]
    fn existing_quote_gets_escaped() {
        assert_eq!(single(r"a'b".into()), r"'a'\''b'");
        assert_eq!(single(r"'".into()), r"''\'''");
        assert_eq!(single(r"'\''".into()), r"''\''\'\'''\'''");
    }

    #[test]
    fn complex() {
        let expected = "\'\0cmd `arg` $var\\\'\\\'\'ring\\// arg \"quoted\'\\!\'\"\'";
        assert_eq!(single("\0cmd `arg` $var\\'ring\\// arg \"quoted!\"".into()), expected);
    }
}

mod ansi_c {
    mod undo {
        use bstr::ByteSlice;
        use gix_quote::ansi_c;

        macro_rules! test {
            ($name:ident, $input:literal, $expected:literal, $consumed:literal) => {
                #[test]
                fn $name() {
                    assert_eq!(
                        ansi_c::undo($input.as_bytes().as_bstr()).expect("valid input"),
                        (
                            std::borrow::Cow::Borrowed($expected.as_bytes().as_bstr()),
                            $consumed
                        )
                    );
                }
            };
        }

        test!(unquoted_remains_unchanged, "hello", "hello", 5);
        test!(empty_surrounded_by_quotes, "\"\"", "", 2);
        test!(surrounded_only_by_quotes, "\"hello\"", "hello", 7);
        test!(typical_escapes, r#""\n\r\t""#, b"\n\r\t", 8);
        test!(untypical_escapes, r#""\a\b\f\v""#, b"\x07\x08\x0c\x0b", 10);
        test!(literal_escape_and_double_quote, r#""\"\\""#, br#""\"#, 6);
        test!(
            unicode_byte_escapes_by_number,
            r#""\346\277\261\351\207\216\t\347\264\224""#,
            "濱野\t純",
            40
        );
        test!(
            exclamation_and_tilde_survive_an_escape_with_double_escaping,
            r#""\\!\\#hello there/file.ext""#,
            r"\!\#hello there/file.ext",
            28
        );

        #[test]
        fn out_of_quote_characters_can_be_passed_and_will_not_be_consumed() {
            let input = br#""hello there" out of quote"#.as_bstr();
            let (unquoted, consumed) = ansi_c::undo(input).expect("valid input");
            assert_eq!(unquoted, std::borrow::Cow::Borrowed(b"hello there".as_bstr()));
            assert_eq!(&input[consumed..], " out of quote");
        }
    }
}