diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f1e47c..3825ded 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,7 @@ - [x] Parse function declaration arguments (`Type id`) - [x] Parse function return datatype (`fun f() -> Type`) - [x] Return parsing to variables to var/val -- [ ] Write tests +- [x] Write tests ## v0.0.10 diff --git a/src/syntax/expression/comparison.rs b/src/syntax/expression/comparison.rs index d456ceb..5b4b4e2 100644 --- a/src/syntax/expression/comparison.rs +++ b/src/syntax/expression/comparison.rs @@ -47,3 +47,45 @@ fn parse_many<'a>( _ => Ok((prev_expr, pos)), } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::lexic::get_tokens; + + #[test] + fn should_parse_comparison() { + let tokens = get_tokens(&String::from("a >= b")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Ok((expr, _)) => match expr { + Expression::BinaryOperator(exp1, exp2, op) => { + match (*exp1, *exp2) { + (Expression::Identifier(id1), Expression::Identifier(id2)) => { + assert_eq!("a", id1); + assert_eq!("b", id2); + } + _ => panic!("Expected 2 identifiers"), + } + assert_eq!(">=", op) + } + _ => panic!("Expected a binary expression with 2 identifiers"), + }, + Err(err) => { + panic!("{:?}", err) + } + } + } + + #[test] + fn should_not_parse_unfinished_comparison() { + let tokens = get_tokens(&String::from("a >=")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Err(ParsingError::Unmatched) => assert!(true), + _ => panic!("Expected an Unmatched error"), + } + } +} diff --git a/src/syntax/expression/equality.rs b/src/syntax/expression/equality.rs index 3d4a838..8e89b3f 100644 --- a/src/syntax/expression/equality.rs +++ b/src/syntax/expression/equality.rs @@ -42,3 +42,45 @@ fn parse_many<'a>( _ => Ok((prev_expr, pos)), } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::lexic::get_tokens; + + #[test] + fn should_parse_comparison() { + let tokens = get_tokens(&String::from("a == b")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Ok((expr, _)) => match expr { + Expression::BinaryOperator(exp1, exp2, op) => { + match (*exp1, *exp2) { + (Expression::Identifier(id1), Expression::Identifier(id2)) => { + assert_eq!("a", id1); + assert_eq!("b", id2); + } + _ => panic!("Expected 2 identifiers"), + } + assert_eq!("==", op) + } + _ => panic!("Expected a binary expression with 2 identifiers"), + }, + Err(err) => { + panic!("{:?}", err) + } + } + } + + #[test] + fn should_not_parse_unfinished_comparison() { + let tokens = get_tokens(&String::from("a ==")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Err(ParsingError::Unmatched) => assert!(true), + _ => panic!("Expected an Unmatched error") + } + } +} diff --git a/src/syntax/expression/factor.rs b/src/syntax/expression/factor.rs index ace232f..5c86919 100644 --- a/src/syntax/expression/factor.rs +++ b/src/syntax/expression/factor.rs @@ -42,3 +42,47 @@ fn parse_many<'a>( _ => Ok((prev_expr, pos)), } } + + +#[cfg(test)] +mod tests { + use super::*; + use crate::lexic::get_tokens; + + #[test] + fn should_parse_comparison() { + let tokens = get_tokens(&String::from("a * b")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Ok((expr, _)) => match expr { + Expression::BinaryOperator(exp1, exp2, op) => { + match (*exp1, *exp2) { + (Expression::Identifier(id1), Expression::Identifier(id2)) => { + assert_eq!("a", id1); + assert_eq!("b", id2); + } + _ => panic!("Expected 2 identifiers"), + } + assert_eq!("*", op) + } + _ => panic!("Expected a binary expression with 2 identifiers"), + }, + Err(err) => { + panic!("{:?}", err) + } + } + } + + #[test] + fn should_not_parse_unfinished_comparison() { + let tokens = get_tokens(&String::from("a /")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Err(ParsingError::Unmatched) => assert!(true), + _ => panic!("Expected an Unmatched error"), + } + } +} + diff --git a/src/syntax/expression/term.rs b/src/syntax/expression/term.rs index f680a93..1729d3d 100644 --- a/src/syntax/expression/term.rs +++ b/src/syntax/expression/term.rs @@ -42,3 +42,45 @@ fn parse_many<'a>( _ => Ok((prev_expr, pos)), } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::lexic::get_tokens; + + #[test] + fn should_parse_comparison() { + let tokens = get_tokens(&String::from("a + b")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Ok((expr, _)) => match expr { + Expression::BinaryOperator(exp1, exp2, op) => { + match (*exp1, *exp2) { + (Expression::Identifier(id1), Expression::Identifier(id2)) => { + assert_eq!("a", id1); + assert_eq!("b", id2); + } + _ => panic!("Expected 2 identifiers"), + } + assert_eq!("+", op) + } + _ => panic!("Expected a binary expression with 2 identifiers"), + }, + Err(err) => { + panic!("{:?}", err) + } + } + } + + #[test] + fn should_not_parse_unfinished_comparison() { + let tokens = get_tokens(&String::from("a -")).unwrap(); + let result = try_parse(&tokens, 0); + + match result { + Err(ParsingError::Unmatched) => assert!(true), + _ => panic!("Expected an Unmatched error"), + } + } +}