Fix bug in try block.
This commit is contained in:
@@ -302,3 +302,68 @@ fn test_get_set_collection() -> Result<(), Box<EvalAltResult>> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "no_index"))]
|
||||
#[test]
|
||||
fn test_get_set_indexer() -> Result<(), Box<EvalAltResult>> {
|
||||
type MyMap = std::collections::BTreeMap<String, INT>;
|
||||
|
||||
let mut engine = Engine::new();
|
||||
|
||||
engine
|
||||
.register_type_with_name::<MyMap>("MyMap")
|
||||
.register_fn("new_map", || MyMap::new())
|
||||
.register_indexer_get_result(|map: &mut MyMap, index: &str| {
|
||||
map.get(index)
|
||||
.cloned()
|
||||
.ok_or_else(|| format!("Index `{}` not found!", index).into())
|
||||
})
|
||||
.register_indexer_set(|map: &mut MyMap, index: &str, value: INT| {
|
||||
map.insert(index.to_string(), value);
|
||||
})
|
||||
.register_fn("to_string", |map: &mut MyMap| format!("{:?}", map));
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
let my_map = new_map();
|
||||
my_map["eggs"] = 42;
|
||||
my_map["eggs"]
|
||||
"#,
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
assert!(engine
|
||||
.eval::<INT>(
|
||||
r#"
|
||||
let my_map = new_map();
|
||||
my_map["eggs"] = 42;
|
||||
my_map["not_found"]
|
||||
"#,
|
||||
)
|
||||
.is_err());
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
r#"
|
||||
let my_map = new_map();
|
||||
my_map["eggs"] = 42;
|
||||
|
||||
try {
|
||||
let eggs = my_map["eggs"];
|
||||
let eggs = my_map["not found"];
|
||||
}
|
||||
catch(x)
|
||||
{
|
||||
print("Not found!");
|
||||
}
|
||||
|
||||
my_map["eggs"]
|
||||
"#,
|
||||
)?,
|
||||
42
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -29,6 +29,11 @@ fn test_try_catch() -> Result<(), Box<EvalAltResult>> {
|
||||
123
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
engine.eval::<INT>("let x = 42; try { let y = 123; print(x/0); } catch { x = 0 } x")?,
|
||||
0
|
||||
);
|
||||
|
||||
#[cfg(not(feature = "no_function"))]
|
||||
assert_eq!(
|
||||
engine.eval::<INT>(
|
||||
|
Reference in New Issue
Block a user