2024-03-07 04:21:07 +00:00
|
|
|
use dust_lang::{
|
2024-03-17 11:31:45 +00:00
|
|
|
abstract_tree::{AbstractTree, Block, Expression, Identifier, Statement, Type},
|
2024-03-17 04:49:01 +00:00
|
|
|
error::{Error, TypeConflict, ValidationError},
|
2024-03-07 04:21:07 +00:00
|
|
|
*,
|
|
|
|
};
|
2024-03-06 23:15:25 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_and_get_variable() {
|
2024-03-08 17:24:11 +00:00
|
|
|
assert_eq!(
|
|
|
|
interpret("foobar = true; foobar"),
|
|
|
|
Ok(Some(Value::boolean(true)))
|
|
|
|
);
|
2024-03-06 23:15:25 +00:00
|
|
|
}
|
2024-03-07 04:21:07 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_variable_with_type() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret("foobar: bool = true; foobar"),
|
2024-03-08 17:24:11 +00:00
|
|
|
Ok(Some(Value::boolean(true)))
|
2024-03-07 04:21:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn set_variable_with_type_error() {
|
|
|
|
assert_eq!(
|
|
|
|
interpret("foobar: str = true"),
|
|
|
|
Err(vec![Error::Validation {
|
2024-03-17 11:31:45 +00:00
|
|
|
error: ValidationError::TypeCheck {
|
|
|
|
conflict: TypeConflict {
|
|
|
|
actual: Type::Boolean,
|
|
|
|
expected: Type::String
|
|
|
|
},
|
|
|
|
actual_position: (0, 0),
|
|
|
|
expected_position: (0, 0)
|
|
|
|
},
|
|
|
|
position: (0, 18)
|
2024-03-07 04:21:07 +00:00
|
|
|
}])
|
|
|
|
);
|
|
|
|
}
|
2024-03-09 00:05:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_variable() {
|
|
|
|
assert_eq!(
|
2024-03-11 19:17:01 +00:00
|
|
|
interpret("foobar = (x: int): int { x }; foobar"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::function(
|
2024-03-17 11:31:45 +00:00
|
|
|
vec![(
|
|
|
|
Identifier::new("x"),
|
|
|
|
Type::Integer.with_position((0..0).into())
|
|
|
|
)],
|
|
|
|
Type::Integer.with_position((0..0).into()),
|
|
|
|
Block::new(vec![Statement::Expression(Expression::Identifier(
|
|
|
|
Identifier::new("x")
|
|
|
|
))
|
|
|
|
.with_position((0..0).into())])
|
|
|
|
.with_position((0..0).into())
|
2024-03-09 00:05:17 +00:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|