Skip to content

Commit

Permalink
Add tests for Option and Result Try impl
Browse files Browse the repository at this point in the history
  • Loading branch information
huntiep authored and nikomatsakis committed Sep 27, 2017
1 parent 2bd104f commit f098d7b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/libcore/tests/lib.rs
Expand Up @@ -38,6 +38,7 @@
#![feature(test)]
#![feature(trusted_len)]
#![feature(try_from)]
#![feature(try_trait)]
#![feature(unique)]

#![feature(const_atomic_bool_new)]
Expand Down
27 changes: 27 additions & 0 deletions src/libcore/tests/option.rs
Expand Up @@ -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));
}
29 changes: 29 additions & 0 deletions src/libcore/tests/result.rs
Expand Up @@ -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") }

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

0 comments on commit f098d7b

Please sign in to comment.