Skip to content

Commit f098d7b

Browse files
huntiepnikomatsakis
authored andcommitted
Add tests for Option and Result Try impl
1 parent 2bd104f commit f098d7b

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(test)]
3939
#![feature(trusted_len)]
4040
#![feature(try_from)]
41+
#![feature(try_trait)]
4142
#![feature(unique)]
4243

4344
#![feature(const_atomic_bool_new)]

src/libcore/tests/option.rs

+27
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,30 @@ fn test_cloned() {
270270
assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
271271
assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
272272
}
273+
274+
#[test]
275+
fn test_try() {
276+
fn try_option_some() -> Option<u8> {
277+
let val = Some(1)?;
278+
Some(val)
279+
}
280+
assert_eq!(try_option_some(), Some(1));
281+
282+
fn try_option_none() -> Option<u8> {
283+
let val = None?;
284+
Some(val)
285+
}
286+
assert_eq!(try_option_none(), None);
287+
288+
fn try_option_ok() -> Result<u8, Missing> {
289+
let val = Ok(1)?;
290+
Ok(val)
291+
}
292+
assert_eq!(try_option_ok(), Ok(1));
293+
294+
fn try_option_err() -> Result<u8, Missing> {
295+
let val = Err(Missing)?;
296+
Ok(val)
297+
}
298+
assert_eq!(try_option_err(), Err(Missing));
299+
}

src/libcore/tests/result.rs

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use core::option::*;
12+
1113
fn op1() -> Result<isize, &'static str> { Ok(666) }
1214
fn op2() -> Result<isize, &'static str> { Err("sadface") }
1315

@@ -202,3 +204,30 @@ pub fn test_unwrap_or_default() {
202204
assert_eq!(op1().unwrap_or_default(), 666);
203205
assert_eq!(op2().unwrap_or_default(), 0);
204206
}
207+
208+
#[test]
209+
fn test_try() {
210+
fn try_result_some() -> Option<u8> {
211+
let val = Ok(1)?;
212+
Some(val)
213+
}
214+
assert_eq!(try_result_some(), Some(1));
215+
216+
fn try_result_none() -> Option<u8> {
217+
let val = Err(Missing)?;
218+
Some(val)
219+
}
220+
assert_eq!(try_result_none(), None);
221+
222+
fn try_result_ok() -> Result<u8, u8> {
223+
let val = Ok(1)?;
224+
Ok(val)
225+
}
226+
assert_eq!(try_result_ok(), Ok(1));
227+
228+
fn try_result_err() -> Result<u8, u8> {
229+
let val = Err(1)?;
230+
Ok(val)
231+
}
232+
assert_eq!(try_result_err(), Err(1));
233+
}

0 commit comments

Comments
 (0)