From 0393995a496b517fefcc7379854684352eae2b23 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 5 Jun 2024 07:38:47 -0500 Subject: [PATCH] test: simple tests for block parsing --- CHANGELOG.md | 2 +- src/syntax/parsers/block.rs | 59 ++++++++++++++++++---- src/syntax/parsers/function_declaration.rs | 1 - 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 313e4fa..fb263ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ - Store tokens for the semantic analysis phase, to have actual error reporting - Parse more complex bindings - Watch mode -- Improve error messages +- Rework error messages - Parse other language constructions - Type checking - Check for conflicting identifiers diff --git a/src/syntax/parsers/block.rs b/src/syntax/parsers/block.rs index 21f4fc2..92274f2 100644 --- a/src/syntax/parsers/block.rs +++ b/src/syntax/parsers/block.rs @@ -100,22 +100,59 @@ mod tests { assert_eq!(2, next_pos); assert_eq!(0, block.members.len()) } - - // TODO: rewrite, refactor - /* + #[test] - fn test_parse_block() { - let tokens = get_tokens(&String::from("{f()}")).unwrap(); - let block = parse_block(&tokens, 0); + fn should_parse_block_with_fn() { + let tokens = get_tokens(&String::from("{\n fun f(){}\n}")).unwrap(); + let (block, next_pos) = Block::try_parse(&tokens, 0).unwrap(); - let block = match block { - ParsingResult::Ok((p, _)) => p, - _ => panic!("Expected a block, got: {:?}", block), - }; + assert_eq!(12, next_pos); + assert_eq!(1, block.members.len()); - assert_eq!(block.statements.len(), 1); + let member = &block.members[0]; + match member { + BlockMember::Stmt(Statement::FnDecl(f)) => { + assert_eq!(f.identifier.value, "f"); + } + _ => panic!("Expected a function declaration, got {:?}", member) + } } + #[test] + fn should_parse_block_with_fn_2() { + let tokens = get_tokens(&String::from("{\n fun f(){}\nfun g(){}\n}")).unwrap(); + let (block, next_pos) = Block::try_parse(&tokens, 0).unwrap(); + + assert_eq!(19, next_pos); + assert_eq!(2, block.members.len()); + + let member = &block.members[0]; + match member { + BlockMember::Stmt(Statement::FnDecl(f)) => { + assert_eq!(f.identifier.value, "f"); + } + _ => panic!("Expected a function declaration, got {:?}", member) + } + + let member = &block.members[1]; + match member { + BlockMember::Stmt(Statement::FnDecl(f)) => { + assert_eq!(f.identifier.value, "g"); + } + _ => panic!("Expected a function declaration, got {:?}", member) + } + } + + // TODO: rewrite, refactor + #[test] + fn should_parse_simple_expression() { + let tokens = get_tokens(&String::from("{f()}")).unwrap(); + let (block, _) = Block::try_parse(&tokens, 0).unwrap(); + + assert_eq!(block.members.len(), 1); + } + + /* #[test] fn test_parse_block_2() { let tokens = get_tokens(&String::from("{f()\ng()}")).unwrap(); diff --git a/src/syntax/parsers/function_declaration.rs b/src/syntax/parsers/function_declaration.rs index a9d7391..ad08193 100644 --- a/src/syntax/parsers/function_declaration.rs +++ b/src/syntax/parsers/function_declaration.rs @@ -101,7 +101,6 @@ impl<'a> Parseable<'a> for FunctionDeclaration<'a> { current_pos = next_pos; // Function body (block) - // TODO: impl Parseable let (block, next_pos) = match Block::try_parse(tokens, current_pos) { Ok((block, next_pos)) => (block, next_pos), Err(ParsingError::Err(error)) => {