Add tests for Option and Result Try impl

This commit is contained in:
Hunter Praska 2017-06-07 22:52:13 -05:00 committed by Niko Matsakis
parent 2bd104fd4f
commit f098d7be29
3 changed files with 57 additions and 0 deletions

View File

@ -38,6 +38,7 @@
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(try_trait)]
#![feature(unique)]
#![feature(const_atomic_bool_new)]

View File

@ -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));
}

View File

@ -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));
}