2024-02-13 17:04:02 +00:00
|
|
|
use dust_lang::{
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
*,
|
|
|
|
};
|
2024-02-13 15:26:26 +00:00
|
|
|
|
|
|
|
#[test]
|
2024-02-14 23:57:24 +00:00
|
|
|
fn string_as_string_list() {
|
2024-02-13 15:26:26 +00:00
|
|
|
assert_eq!(
|
|
|
|
interpret("'foobar' as [str]"),
|
|
|
|
Ok(Value::List(List::with_items(vec![
|
|
|
|
Value::String("f".to_string()),
|
|
|
|
Value::String("o".to_string()),
|
|
|
|
Value::String("o".to_string()),
|
|
|
|
Value::String("b".to_string()),
|
|
|
|
Value::String("a".to_string()),
|
|
|
|
Value::String("r".to_string()),
|
|
|
|
])))
|
|
|
|
)
|
|
|
|
}
|
2024-02-13 15:49:49 +00:00
|
|
|
|
|
|
|
#[test]
|
2024-02-14 23:57:24 +00:00
|
|
|
fn string_as_list_error() {
|
2024-02-13 15:49:49 +00:00
|
|
|
assert_eq!(
|
|
|
|
interpret("'foobar' as [float]"),
|
|
|
|
Err(Error::Validation(ValidationError::ConversionImpossible {
|
|
|
|
initial_type: Type::String,
|
|
|
|
target_type: Type::List(Box::new(Type::Float))
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|
2024-02-13 17:04:02 +00:00
|
|
|
|
|
|
|
const JSON: &str = "{ \"x\": 1 }";
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn conversion_runtime_error() {
|
|
|
|
let json_value = interpret(&format!("json:parse('{JSON}')")).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
interpret(&format!("json:parse('{JSON}') as [map]")),
|
|
|
|
Err(Error::Runtime(RuntimeError::ConversionImpossible {
|
2024-02-16 23:54:00 +00:00
|
|
|
from: json_value.r#type().unwrap(),
|
|
|
|
to: Type::List(Box::new(Type::Map)),
|
|
|
|
position: SourcePosition {
|
|
|
|
start_byte: 0,
|
|
|
|
end_byte: 0,
|
|
|
|
start_row: 0,
|
|
|
|
start_column: 0,
|
|
|
|
end_row: 0,
|
|
|
|
end_column: 0
|
|
|
|
}
|
2024-02-13 17:04:02 +00:00
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|