Add tests for function parsing
This commit is contained in:
parent
8d4f452f34
commit
c133ced4fc
2 changed files with 85 additions and 0 deletions
|
@ -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>>>),
|
||||||
}
|
}
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Reference in a new issue