2024-03-07 04:21:07 +00:00
|
|
|
use dust_lang::{
|
2024-06-17 14:10:06 +00:00
|
|
|
abstract_tree::{Block, Expression, Statement, Type, WithPos},
|
2024-06-22 17:55:43 +00:00
|
|
|
context::Context,
|
2024-06-18 23:42:04 +00:00
|
|
|
error::{DustError, TypeConflict, ValidationError},
|
2024-03-25 04:16:55 +00:00
|
|
|
identifier::Identifier,
|
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!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "foobar = true; foobar"),
|
2024-03-08 17:24:11 +00:00
|
|
|
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!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "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!(
|
2024-03-24 19:35:19 +00:00
|
|
|
interpret("test", "foobar: str = true")
|
2024-03-24 13:10:49 +00:00
|
|
|
.unwrap_err()
|
|
|
|
.errors(),
|
2024-06-18 23:42:04 +00:00
|
|
|
&vec![DustError::Validation {
|
2024-03-17 11:31:45 +00:00
|
|
|
error: ValidationError::TypeCheck {
|
|
|
|
conflict: TypeConflict {
|
|
|
|
actual: Type::Boolean,
|
|
|
|
expected: Type::String
|
|
|
|
},
|
2024-03-17 17:36:31 +00:00
|
|
|
actual_position: (14, 18).into(),
|
2024-06-17 14:10:06 +00:00
|
|
|
expected_position: Some((8, 11).into())
|
2024-03-17 11:31:45 +00:00
|
|
|
},
|
2024-03-17 12:30:46 +00:00
|
|
|
position: (0, 18).into()
|
2024-03-24 13:10:49 +00:00
|
|
|
}]
|
2024-03-07 04:21:07 +00:00
|
|
|
);
|
|
|
|
}
|
2024-03-09 00:05:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn function_variable() {
|
|
|
|
assert_eq!(
|
2024-06-19 02:03:41 +00:00
|
|
|
interpret("test", "foobar = fn (x: int) -> int { x }; foobar"),
|
2024-03-09 00:05:17 +00:00
|
|
|
Ok(Some(Value::function(
|
2024-06-19 02:03:41 +00:00
|
|
|
None,
|
2024-06-17 14:10:06 +00:00
|
|
|
vec![(Identifier::new("x"), Type::Integer)],
|
2024-06-22 04:58:30 +00:00
|
|
|
Some(Type::Integer),
|
2024-06-17 19:47:07 +00:00
|
|
|
Block::new(vec![Statement::Expression(Expression::Identifier(
|
2024-06-19 02:03:41 +00:00
|
|
|
Identifier::new("x").with_position((30, 31))
|
2024-06-22 17:55:43 +00:00
|
|
|
))]),
|
|
|
|
Context::new(None)
|
2024-03-09 00:05:17 +00:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
}
|