1
0
Fork 0

Add tests for block expression parsing

This commit is contained in:
Vili Sinervä 2025-01-31 16:09:47 +02:00
parent 9e8df0750f
commit b36a4b2bdf
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
2 changed files with 62 additions and 0 deletions

View file

@ -1,5 +1,6 @@
#[derive(Debug, PartialEq)]
pub enum Expression<'source> {
EmptyLiteral(),
IntLiteral(u32),
BoolLiteral(bool),
Identifier(&'source str),
@ -15,4 +16,5 @@ pub enum Expression<'source> {
Option<Box<Expression<'source>>>,
),
FunCall(&'source str, Vec<Expression<'source>>),
Block(Vec<Expression<'source>>),
}

View file

@ -341,3 +341,63 @@ fn test_func_missing_comma() {
fn test_func_missing_close() {
parse(&tokenize("f(a"));
}
#[test]
fn test_block_basic() {
let result = parse(&tokenize("{ a = 1; b; }"));
assert_eq!(
result,
Block(vec![
BinaryOp(id_ast!("a"), "=", int_ast!(1)),
Identifier("b"),
EmptyLiteral()
])
);
let result = parse(&tokenize("{ a = 1; b }"));
assert_eq!(
result,
Block(vec![
BinaryOp(id_ast!("a"), "=", int_ast!(1)),
Identifier("b"),
])
);
}
#[test]
fn test_block_embedded() {
let result = parse(&tokenize("{ 1 + 2 } * 3"));
assert_eq!(
result,
BinaryOp(
Box::new(Block(vec![BinaryOp(int_ast!(1), "+", int_ast!(2))])),
"*",
int_ast!(3)
)
);
}
#[test]
fn test_block_nested() {
let result = parse(&tokenize("{ a = { 1 + 2}}"));
assert_eq!(
result,
Block(vec![BinaryOp(
id_ast!("a"),
"=",
Box::new(Block(vec![BinaryOp(int_ast!(1), "+", int_ast!(2))])),
)])
);
}
#[test]
#[should_panic]
fn test_block_unmatched() {
parse(&tokenize("{ a = 1 "));
}
#[test]
#[should_panic]
fn test_block_missing_semicolon() {
parse(&tokenize("{ a = 1\nb }"));
}