Implement ? in catch expressions and add tests

This commit is contained in:
Taylor Cramer 2017-02-28 11:05:03 -08:00
parent 703b246287
commit fc04eaacc5
16 changed files with 571 additions and 273 deletions

View file

@ -29,4 +29,38 @@ pub fn main() {
match catch {
_ => {}
};
let catch_err = do catch {
Err(22)?;
Ok(1)
};
assert_eq!(catch_err, Err(22));
let catch_okay: Result<i32, i32> = do catch {
if false { Err(25)?; }
Ok::<(), i32>(())?;
Ok(28)
};
assert_eq!(catch_okay, Ok(28));
let catch_from_loop: Result<i32, i32> = do catch {
for i in 0..10 {
if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; }
}
Ok(22)
};
assert_eq!(catch_from_loop, Err(5));
let cfg_init;
let _res: Result<(), ()> = do catch {
cfg_init = 5;
Ok(())
};
assert_eq!(cfg_init, 5);
let my_string = "test".to_string();
let res: Result<&str, ()> = do catch {
Ok(&my_string)
};
assert_eq!(res, Ok("test"));
}