dust/dust-lang/tests/structs.rs

121 lines
2.7 KiB
Rust
Raw Normal View History

2024-03-20 05:29:07 +00:00
use dust_lang::{
2024-03-25 04:16:55 +00:00
abstract_tree::{Type, WithPos},
2024-06-18 23:42:04 +00:00
error::{DustError, TypeConflict, ValidationError},
2024-03-25 04:16:55 +00:00
identifier::Identifier,
2024-03-24 19:35:19 +00:00
interpret, Value,
2024-03-20 05:29:07 +00:00
};
2024-03-24 19:35:19 +00:00
2024-03-19 23:16:33 +00:00
#[test]
fn simple_structure() {
assert_eq!(
interpret(
2024-03-24 19:35:19 +00:00
"test",
2024-03-19 23:16:33 +00:00
"
struct Foo {
bar : int,
baz : str,
}
Foo {
bar = 42,
baz = 'hiya',
}
"
),
Ok(Some(Value::structure(
Identifier::new("Foo").with_position((127, 130)),
2024-03-19 23:16:33 +00:00
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(
2024-03-24 19:35:19 +00:00
"test",
2024-03-20 05:29:07 +00:00
"
struct Foo {
bar : int,
}
Foo {
bar = 'hiya',
}
"
2024-03-24 13:10:49 +00:00
)
.unwrap_err()
.errors(),
2024-06-18 23:42:04 +00:00
&vec![DustError::Validation {
2024-03-20 05:29:07 +00:00
error: ValidationError::TypeCheck {
conflict: TypeConflict {
actual: Type::String,
expected: Type::Integer
},
actual_position: (128, 134).into(),
2024-06-17 14:10:06 +00:00
expected_position: Some((56, 59).into()),
2024-03-20 05:29:07 +00:00
},
2024-03-20 15:43:47 +00:00
position: (96, 153).into()
2024-03-24 13:10:49 +00:00
}]
2024-03-20 05:29:07 +00:00
)
}
2024-03-19 23:33:02 +00:00
#[test]
fn nested_structure() {
assert_eq!(
interpret(
2024-03-24 19:35:19 +00:00
"test",
2024-03-19 23:33:02 +00:00
"
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").with_position((172, 175)),
2024-03-19 23:37:18 +00:00
vec![(
Identifier::new("bar"),
Value::structure(
Identifier::new("Bar").with_position((204, 207)),
2024-03-19 23:37:18 +00:00
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(
2024-03-24 19:35:19 +00:00
"test",
2024-03-19 23:46:41 +00:00
"
Foo {
bar = 42
}
"
2024-03-24 13:10:49 +00:00
)
.unwrap_err()
.errors(),
2024-06-18 23:42:04 +00:00
&vec![DustError::Validation {
2024-03-25 04:16:55 +00:00
error: ValidationError::VariableNotFound {
identifier: Identifier::new("Foo"),
position: (17, 20).into()
2024-03-25 04:16:55 +00:00
},
2024-03-20 15:43:47 +00:00
position: (17, 69).into()
2024-03-24 13:10:49 +00:00
}]
2024-03-19 23:46:41 +00:00
)
}