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 {
|
|
|
|
value: json_value,
|
2024-02-14 23:46:05 +00:00
|
|
|
target_type: Type::List(Box::new(Type::Map(None)))
|
2024-02-13 17:04:02 +00:00
|
|
|
}))
|
|
|
|
)
|
|
|
|
}
|