Add tests for Option and Result Try impl
This commit is contained in:
parent
2bd104fd4f
commit
f098d7be29
@ -38,6 +38,7 @@
|
||||
#![feature(test)]
|
||||
#![feature(trusted_len)]
|
||||
#![feature(try_from)]
|
||||
#![feature(try_trait)]
|
||||
#![feature(unique)]
|
||||
|
||||
#![feature(const_atomic_bool_new)]
|
||||
|
@ -270,3 +270,30 @@ fn test_cloned() {
|
||||
assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
|
||||
assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try() {
|
||||
fn try_option_some() -> Option<u8> {
|
||||
let val = Some(1)?;
|
||||
Some(val)
|
||||
}
|
||||
assert_eq!(try_option_some(), Some(1));
|
||||
|
||||
fn try_option_none() -> Option<u8> {
|
||||
let val = None?;
|
||||
Some(val)
|
||||
}
|
||||
assert_eq!(try_option_none(), None);
|
||||
|
||||
fn try_option_ok() -> Result<u8, Missing> {
|
||||
let val = Ok(1)?;
|
||||
Ok(val)
|
||||
}
|
||||
assert_eq!(try_option_ok(), Ok(1));
|
||||
|
||||
fn try_option_err() -> Result<u8, Missing> {
|
||||
let val = Err(Missing)?;
|
||||
Ok(val)
|
||||
}
|
||||
assert_eq!(try_option_err(), Err(Missing));
|
||||
}
|
||||
|
@ -8,6 +8,8 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
use core::option::*;
|
||||
|
||||
fn op1() -> Result<isize, &'static str> { Ok(666) }
|
||||
fn op2() -> Result<isize, &'static str> { Err("sadface") }
|
||||
|
||||
@ -202,3 +204,30 @@ pub fn test_unwrap_or_default() {
|
||||
assert_eq!(op1().unwrap_or_default(), 666);
|
||||
assert_eq!(op2().unwrap_or_default(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_try() {
|
||||
fn try_result_some() -> Option<u8> {
|
||||
let val = Ok(1)?;
|
||||
Some(val)
|
||||
}
|
||||
assert_eq!(try_result_some(), Some(1));
|
||||
|
||||
fn try_result_none() -> Option<u8> {
|
||||
let val = Err(Missing)?;
|
||||
Some(val)
|
||||
}
|
||||
assert_eq!(try_result_none(), None);
|
||||
|
||||
fn try_result_ok() -> Result<u8, u8> {
|
||||
let val = Ok(1)?;
|
||||
Ok(val)
|
||||
}
|
||||
assert_eq!(try_result_ok(), Ok(1));
|
||||
|
||||
fn try_result_err() -> Result<u8, u8> {
|
||||
let val = Err(1)?;
|
||||
Ok(val)
|
||||
}
|
||||
assert_eq!(try_result_err(), Err(1));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user