Add tests for variable declaration
This commit is contained in:
parent
640b4b8b10
commit
97ae0e5ce6
2 changed files with 29 additions and 0 deletions
|
@ -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,
|
||||||
|
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
|
Reference in a new issue