Clean up and improve tests
This commit is contained in:
parent
ece75d7b9c
commit
6cb2df55f7
@ -61,12 +61,6 @@ impl AbstractTree for Assignment {
|
|||||||
let statement_type = statement.expected_type(context)?;
|
let statement_type = statement.expected_type(context)?;
|
||||||
|
|
||||||
if let Some(type_definition) = &type_definition {
|
if let Some(type_definition) = &type_definition {
|
||||||
context.set(
|
|
||||||
identifier.inner().clone(),
|
|
||||||
Value::Empty,
|
|
||||||
Some(type_definition.inner().clone()),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
match operator {
|
match operator {
|
||||||
AssignmentOperator::Equal => {
|
AssignmentOperator::Equal => {
|
||||||
type_definition
|
type_definition
|
||||||
@ -98,6 +92,15 @@ impl AbstractTree for Assignment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let variable_key = identifier.inner().clone();
|
||||||
|
let variable_type = if let Some(definition) = &type_definition {
|
||||||
|
definition.inner().clone()
|
||||||
|
} else {
|
||||||
|
statement_type
|
||||||
|
};
|
||||||
|
|
||||||
|
context.set(variable_key, Value::Empty, Some(variable_type))?;
|
||||||
|
|
||||||
Ok(Assignment {
|
Ok(Assignment {
|
||||||
identifier,
|
identifier,
|
||||||
type_definition,
|
type_definition,
|
||||||
|
@ -45,8 +45,6 @@ impl AbstractTree for FunctionCall {
|
|||||||
{
|
{
|
||||||
let expected_type = parameter_types.get(argument_index).unwrap();
|
let expected_type = parameter_types.get(argument_index).unwrap();
|
||||||
|
|
||||||
println!("{expected_type} {expression_type}");
|
|
||||||
|
|
||||||
expected_type
|
expected_type
|
||||||
.check(&expression_type)
|
.check(&expression_type)
|
||||||
.map_err(|error| error.at_node(child, source))?;
|
.map_err(|error| error.at_node(child, source))?;
|
||||||
|
@ -260,8 +260,7 @@ mod tests {
|
|||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert!(result.unwrap_err().is_type_check_error(&Error::TypeCheck {
|
||||||
Err(Error::TypeCheck {
|
|
||||||
expected: Type::Function {
|
expected: Type::Function {
|
||||||
parameter_types: vec![],
|
parameter_types: vec![],
|
||||||
return_type: Box::new(Type::Boolean),
|
return_type: Box::new(Type::Boolean),
|
||||||
@ -270,8 +269,6 @@ mod tests {
|
|||||||
parameter_types: vec![],
|
parameter_types: vec![],
|
||||||
return_type: Box::new(Type::Integer),
|
return_type: Box::new(Type::Integer),
|
||||||
},
|
},
|
||||||
}),
|
}));
|
||||||
result
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -255,4 +255,24 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(evaluate("{ x = 1, foo = 'bar' }"), Ok(Value::Map(map)));
|
assert_eq!(evaluate("{ x = 1, foo = 'bar' }"), Ok(Value::Map(map)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn evaluate_function() {
|
||||||
|
let result = evaluate("(fn) <bool> {true}");
|
||||||
|
let value = result.unwrap();
|
||||||
|
let function = value.as_function().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(&Vec::<Identifier>::with_capacity(0), function.parameters());
|
||||||
|
assert_eq!(Ok(&Type::Boolean), function.return_type());
|
||||||
|
|
||||||
|
let result = evaluate("(fn x <bool>) <bool> {true}");
|
||||||
|
let value = result.unwrap();
|
||||||
|
let function = value.as_function().unwrap();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
&vec![Identifier::new("x".to_string())],
|
||||||
|
function.parameters()
|
||||||
|
);
|
||||||
|
assert_eq!(Ok(&Type::Boolean), function.return_type());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user