Skip to content
Snippets Groups Projects
mod.rs 4.88 KiB
Newer Older
  • Learn to ignore specific revisions
  • pub mod macros;
    
    #[cfg(test)]
    mod tests {
        use core::panic;
    
        use crate::combinators::common::characters::Char;
        use crate::parser::Parser;
    
        use crate::parser;
    
        
        #[test]
        fn test_pure() -> Result<(), String>{
            match parser!(pure 'e').parse("c") {
                Ok(_c) => Ok(()),
                Err(e) => panic!("{}", e) 
            }
        }
        
        #[test]
        fn test_empty() -> Result<(), String>{
            match parser!(empty).parse("c") {
                Ok(e) => panic!("{}", e),
                Err(_s) => Ok(())
            }
        }
    
        #[test]
        fn test_sat() -> Result<(), String>{
            let f = |c:char| c == 'k';
            match parser!(sat f).parse("kb") {
                Ok(c) => {
                    assert_eq!(c, 'k');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
        }
        
        #[test]
        fn test_try() -> Result<(), String>{
            let p = Char {c: 'c'};
            match parser!(try(p)).parse("cb") {
                Ok(c) => assert_eq!(c, 'c'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(try 'c' ).parse("cb") {
                Ok(c) => {
                    assert_eq!(c, 'c');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
        }
        
        #[test]
        fn test_alternative() -> Result<(), String> {
            let p = Char {c: 'c'};
            let p2 = Char {c: 'k'};
            let p3 = Char {c: 'l'};
    
            match parser!(('c'<|>'x')).parse("xb") {
                Ok(c) => assert_eq!(c, 'x'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(p <|> p2).parse("kb") {
                Ok(c) => assert_eq!(c, 'k'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(('c' <|> p3)).parse("lb") {
                Ok(c) => {
                    assert_eq!(c, 'l');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
        }
        
        #[test]
        fn test_zip() -> Result<(), String>{
            let p = Char {c: 'c'};
            let p2 = Char {c: 'k'};
            let p3 = Char {c: 'l'};
    
            match parser!('c'<~>'x').parse("cx") {
                Ok((c1, c2)) => {
                    assert_eq!(c1, 'c');
                    assert_eq!(c2, 'x');
                },
                Err(e) => panic!("{}", e)
            }
    
            match parser!(p<~>p2).parse("ck") {
                Ok((c1, c2)) => {
                    assert_eq!(c1, 'c');
                    assert_eq!(c2, 'k');
                },
                Err(e) => panic!("{}", e)
            }
    
            match parser!('c' <~> 'm').parse("cl") {
                Ok(_) => panic!("Didnt fail at m"),
                Err(_e) => ()
            }
    
            match parser!(p3<~>'x').parse("lx") {
                Ok((c1, c2)) => {
                    assert_eq!(c1, 'l');
                    assert_eq!(c2, 'x');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
            
        }
        
        #[test]
        fn test_map() -> Result<(), String>{
            let f = |_c: char| 'b';
            match parser!(map f 'c').parse("c"){
                Ok(c) => assert_eq!(c, 'b'),
                Err(e) => panic!("{}", e)
            }
            let p = Char {c: 'c'};
            match parser!((map f p)).parse("c"){
                Ok(c) => {
                    assert_eq!(c, 'b');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
        }
    
    Carlos Valencia's avatar
    Carlos Valencia committed
    
        #[test]
        fn test_left() -> Result<(), String>{
            let p = Char {c: 'c'};
            let p2 = Char {c: 'k'};
            let p3 = Char {c: 'l'};
    
            match parser!('c'<*'x').parse("cx") {
                Ok(c) => assert_eq!(c, 'c'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(p<*p2).parse("ck") {
                Ok(c) => assert_eq!(c, 'c'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!('c' <* 'm').parse("cl") {
                Ok(_) => panic!("Didnt fail at m"),
                Err(_e) => ()
            }
    
            match parser!(p3<*'x').parse("lx") {
                Ok(c) => {
                    assert_eq!(c, 'l');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
            
        }
    
        #[test]
        fn test_right() -> Result<(), String>{
            let p = Char {c: 'c'};
            let p2 = Char {c: 'k'};
            let p3 = Char {c: 'l'};
    
            match parser!('c'*>'x').parse("cx") {
                Ok(c) => assert_eq!(c, 'x'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(p*>p2).parse("ck") {
                Ok(c) => assert_eq!(c, 'k'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!('c' *> 'm').parse("cl") {
                Ok(_) => panic!("Didnt fail at m"),
                Err(_e) => ()
            }
    
            let p = Char {c: 'a'};
            match parser!('c' *> p *> 'r').parse("car") {
                Ok(c) => assert_eq!(c, 'r'),
                Err(e) => panic!("{}", e)
            }
    
            match parser!(p3*>'x').parse("lx") {
                Ok(c) => {
                    assert_eq!(c, 'x');
                    Ok(())
                },
                Err(e) => panic!("{}", e)
            }
            
        }