Brijesh's Git Server — bsh @ 476de6711bde624186baf0ab1b748a04341e0644

my own shell program

src/tests/parser_test.rs (view raw)

 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
#[cfg(test)]
mod tests {
    use crate::parser;

    #[test]
    fn test_parse_simple_command() {
        let input = "echo hello";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello"]);
    }

    #[test]
    fn test_parse_quoted_argument() {
        let input = "echo 'hello world'";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello world"]);
    }

    #[test]
    fn test_parse_empty_input() {
        let input = "";
        let parsed = parser::parse(input);
        assert!(parsed.is_err(), "Empty input should result in error");
    }

    #[test]
    fn test_parse_escape_character() {
        let input = "echo hello\\ world";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello world"]);
    }

    #[test]
    fn test_parse_unclosed_quote() {
        let input = "echo \"hello";
        let parsed = parser::parse(input);
        assert!(parsed.is_err(), "Unclosed quote should result in error");
        assert_eq!(parsed.unwrap_err(), "Unclosed quote in input");
    }

    #[test]
    fn test_parse_carriage_return() {
        let input = "echo hello\rworld";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello\rworld"]);
    }

    #[test]
    fn test_parse_newline() {
        let input = "echo hello\nworld";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello\nworld"]);
    }

    #[test]
    fn test_parse_tab() {
        let input = "echo hello\tworld";
        let parsed = parser::parse(input).expect("Failed to parse command");
        assert_eq!(parsed.command, "echo");
        assert_eq!(parsed.args, vec!["hello\tworld"]);
    }
}