1
0
dust/tests/types.rs

73 lines
1.7 KiB
Rust
Raw Permalink Normal View History

2024-02-01 00:35:27 +00:00
use dust_lang::{error::ValidationError, *};
2024-01-06 07:26:51 +00:00
#[test]
fn simple_type_check() {
let result = interpret("x <bool> = 1");
2024-02-01 00:35:27 +00:00
assert_eq!(
Err(Error::Validation(ValidationError::TypeCheck {
expected: Type::Boolean,
actual: Type::Integer,
position: SourcePosition {
start_byte: 0,
2024-02-01 02:21:42 +00:00
end_byte: 12,
start_row: 1,
2024-02-01 00:35:27 +00:00
start_column: 0,
2024-02-01 02:21:42 +00:00
end_row: 1,
end_column: 12,
2024-02-01 00:35:27 +00:00
}
})),
result
);
2024-01-06 07:26:51 +00:00
}
#[test]
fn argument_count_check() {
let source = "
2024-01-10 01:38:40 +00:00
foo = (x <int>) <int> {
2024-01-06 07:26:51 +00:00
x
}
foo()
";
2024-01-23 22:08:26 +00:00
let result = interpret(source);
2024-01-06 07:26:51 +00:00
assert_eq!(
2024-01-10 01:38:40 +00:00
"Expected 1 arguments, but got 0. Occured at (5, 12) to (5, 17). Source: foo()",
2024-01-06 07:26:51 +00:00
result.unwrap_err().to_string()
)
}
#[test]
fn callback_type_check() {
let result = interpret(
"
x = (cb <() -> bool>) <bool> {
cb()
}
x(() <int> { 1 })
",
);
2024-02-01 00:35:27 +00:00
assert_eq!(
Err(Error::Validation(ValidationError::TypeCheck {
expected: Type::Function {
parameter_types: vec![],
return_type: Box::new(Type::Boolean),
},
actual: Type::Function {
parameter_types: vec![],
return_type: Box::new(Type::Integer),
},
position: SourcePosition {
2024-02-01 02:21:42 +00:00
start_byte: 91,
end_byte: 108,
start_row: 5,
start_column: 12,
end_row: 5,
end_column: 29,
2024-02-01 00:35:27 +00:00
}
})),
result
);
2024-01-06 07:26:51 +00:00
}