1
0
dust/tests/as.rs

54 lines
1.3 KiB
Rust
Raw Permalink Normal View History

use dust_lang::{
error::{RuntimeError, ValidationError},
*,
};
#[test]
2024-02-14 23:57:24 +00:00
fn string_as_string_list() {
assert_eq!(
interpret("'foobar' as [str]"),
Ok(Value::List(List::with_items(vec![
Value::string("f"),
Value::string("o"),
Value::string("o"),
Value::string("b"),
Value::string("a"),
Value::string("r"),
])))
)
}
#[test]
2024-02-14 23:57:24 +00:00
fn string_as_list_error() {
assert_eq!(
interpret("'foobar' as [float]"),
Err(Error::Validation(ValidationError::ConversionImpossible {
initial_type: Type::String,
2024-02-18 20:07:53 +00:00
target_type: Type::ListOf(Box::new(Type::Float))
}))
)
}
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 {
from: json_value.r#type().unwrap(),
2024-02-18 20:07:53 +00:00
to: Type::ListOf(Box::new(Type::Map(None))),
position: SourcePosition {
start_byte: 0,
2024-02-18 15:59:49 +00:00
end_byte: 33,
start_row: 1,
start_column: 0,
2024-02-18 15:59:49 +00:00
end_row: 1,
end_column: 33,
}
}))
)
}