1
0
Fork 0

Add tests for variable declaration

This commit is contained in:
Vili Sinervä 2025-01-31 16:50:51 +02:00
parent 640b4b8b10
commit 97ae0e5ce6
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
2 changed files with 29 additions and 0 deletions

View file

@ -5,6 +5,7 @@ pub enum Expression<'source> {
BoolLiteral(bool), BoolLiteral(bool),
Identifier(&'source str), Identifier(&'source str),
UnaryOp(&'source str, Box<Expression<'source>>), UnaryOp(&'source str, Box<Expression<'source>>),
VarDeclaration(&'source str, Box<Expression<'source>>),
BinaryOp( BinaryOp(
Box<Expression<'source>>, Box<Expression<'source>>,
&'source str, &'source str,

View file

@ -401,3 +401,31 @@ fn test_block_unmatched() {
fn test_block_missing_semicolon() { fn test_block_missing_semicolon() {
parse(&tokenize("{ a = 1\nb }")); parse(&tokenize("{ a = 1\nb }"));
} }
#[test]
fn test_var_basic() {
let result = parse(&tokenize("var x = 1"));
assert_eq!(result, VarDeclaration("x", int_ast!(1)));
let result = parse(&tokenize("{ var x = 1; x = 2; }"));
assert_eq!(
result,
Block(vec![
VarDeclaration("x", int_ast!(1)),
BinaryOp(id_ast!("x"), "=", int_ast!(2)),
EmptyLiteral()
])
);
}
#[test]
#[should_panic]
fn test_var_chain() {
parse(&tokenize("var x = var y = 1"));
}
#[test]
#[should_panic]
fn test_var_embedded() {
parse(&tokenize("if true then var x = 3"));
}