test: simple tests for block parsing

master
Araozu 2024-06-05 07:38:47 -05:00
parent 6a951434f3
commit 0393995a49
3 changed files with 49 additions and 13 deletions

View File

@ -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

View File

@ -101,21 +101,58 @@ mod tests {
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();

View File

@ -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)) => {