1
0
Fork 0

Add tests for function parsing

This commit is contained in:
Vili Sinervä 2025-01-29 13:27:29 +02:00
parent 8d4f452f34
commit c133ced4fc
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
2 changed files with 85 additions and 0 deletions

View file

@ -13,4 +13,5 @@ pub enum Expression<'source> {
Box<Expression<'source>>, Box<Expression<'source>>,
Option<Box<Expression<'source>>>, Option<Box<Expression<'source>>>,
), ),
FunCall(Vec<Box<Expression<'source>>>),
} }

View file

@ -200,6 +200,14 @@ mod tests {
) )
} }
fn new_punc(text: &str) -> Token {
Token::new(
text,
TokenType::Punctuation,
CodeLocation::new(usize::MAX, usize::MAX),
)
}
#[test] #[test]
fn test_binary_op_basic() { fn test_binary_op_basic() {
let result = parse(&vec![new_int("1"), new_id("+"), new_int("23")]); let result = parse(&vec![new_int("1"), new_id("+"), new_int("23")]);
@ -468,6 +476,64 @@ mod tests {
); );
} }
#[test]
fn test_func_basic() {
let result = parse(&vec![
new_id("f"),
new_punc("("),
new_id("a"),
new_punc(","),
new_id("b"),
new_punc(")"),
]);
assert_eq!(
result,
FunCall(vec![Box::new(Identifier("a")), Box::new(Identifier("b")),])
);
}
#[test]
fn test_func_nested() {
let result = parse(&vec![
new_id("f"),
new_punc("("),
new_id("a"),
new_punc(","),
new_id("g"),
new_punc("("),
new_id("b"),
new_punc(")"),
new_punc(")"),
]);
assert_eq!(
result,
FunCall(vec![
Box::new(Identifier("a")),
Box::new(FunCall(vec![Box::new(Identifier("b"))])),
])
);
}
#[test]
fn test_func_embedded() {
let result = parse(&vec![
new_int("1"),
new_id("+"),
new_id("f"),
new_punc("("),
new_id("a"),
new_punc(")"),
]);
assert_eq!(
result,
BinaryOp(
Box::new(IntLiteral(1)),
"+",
Box::new(FunCall(vec![Box::new(Identifier("a"))]))
)
);
}
#[test] #[test]
#[should_panic] #[should_panic]
fn test_parenthesized_mismatched() { fn test_parenthesized_mismatched() {
@ -481,6 +547,24 @@ mod tests {
]); ]);
} }
#[test]
#[should_panic]
fn test_func_missing_comma() {
parse(&vec![
new_id("f"),
new_punc("("),
new_id("a"),
new_id("b"),
new_punc(")"),
]);
}
#[test]
#[should_panic]
fn test_func_missing_close() {
parse(&vec![new_id("f"), new_punc("("), new_id("a")]);
}
#[test] #[test]
#[should_panic] #[should_panic]
fn test_if_no_then() { fn test_if_no_then() {