1
0
dust/dust-lang/tests/structs.rs

108 lines
2.4 KiB
Rust
Raw Normal View History

2024-03-20 05:29:07 +00:00
use dust_lang::{
abstract_tree::{Identifier, Type},
error::{Error, TypeConflict, ValidationError},
*,
};
2024-03-19 23:16:33 +00:00
#[test]
fn simple_structure() {
assert_eq!(
interpret(
"
struct Foo {
bar : int,
baz : str,
}
Foo {
bar = 42,
baz = 'hiya',
}
"
),
Ok(Some(Value::structure(
Identifier::new("Foo"),
vec![
(Identifier::new("bar"), Value::integer(42)),
(Identifier::new("baz"), Value::string("hiya".to_string())),
]
)))
)
}
2024-03-19 23:33:02 +00:00
2024-03-20 05:29:07 +00:00
#[test]
fn field_type_error() {
assert_eq!(
interpret(
"
struct Foo {
bar : int,
}
Foo {
bar = 'hiya',
}
"
),
Err(vec![Error::Validation {
error: ValidationError::TypeCheck {
conflict: TypeConflict {
actual: Type::String,
expected: Type::Integer
},
actual_position: (128, 134).into(),
expected_position: (56, 59).into()
},
2024-03-20 15:43:47 +00:00
position: (96, 153).into()
2024-03-20 05:29:07 +00:00
}])
)
}
2024-03-19 23:33:02 +00:00
#[test]
fn nested_structure() {
assert_eq!(
interpret(
"
struct Bar {
baz : int
}
struct Foo {
bar : Bar
}
Foo {
2024-03-19 23:37:18 +00:00
bar = Bar {
2024-03-19 23:33:02 +00:00
baz = 42
}
}
"
),
Ok(Some(Value::structure(
Identifier::new("Foo"),
2024-03-19 23:37:18 +00:00
vec![(
Identifier::new("bar"),
Value::structure(
Identifier::new("Bar"),
vec![(Identifier::new("baz"), Value::integer(42))]
)
),]
2024-03-19 23:33:02 +00:00
)))
)
}
2024-03-19 23:46:41 +00:00
#[test]
fn undefined_struct() {
assert_eq!(
interpret(
"
Foo {
bar = 42
}
"
),
Err(vec![Error::Validation {
error: error::ValidationError::VariableNotFound(Identifier::new("Foo")),
2024-03-20 15:43:47 +00:00
position: (17, 69).into()
2024-03-19 23:46:41 +00:00
}])
)
}