2014-06-28 15:57:36 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-28 23:22:58 -05:00
|
|
|
fn op1() -> Result<isize, &'static str> { Ok(666) }
|
|
|
|
fn op2() -> Result<isize, &'static str> { Err("sadface") }
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_and() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(op1().and(Ok(667)).unwrap(), 667);
|
2015-01-20 17:45:07 -06:00
|
|
|
assert_eq!(op1().and(Err::<i32, &'static str>("bad")).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"bad");
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface");
|
2015-01-20 17:45:07 -06:00
|
|
|
assert_eq!(op2().and(Err::<i32,&'static str>("bad")).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"sadface");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_and_then() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(op1().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap(), 667);
|
|
|
|
assert_eq!(op1().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"bad");
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(op2().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"sadface");
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(op2().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"sadface");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_or() {
|
2015-02-25 12:37:22 -06:00
|
|
|
assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(op1().or(Err("bad")).unwrap(), 666);
|
|
|
|
|
2015-02-25 12:37:22 -06:00
|
|
|
assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_or_else() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(op1().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 666);
|
|
|
|
assert_eq!(op1().or_else(|e| Err::<isize, &'static str>(e)).unwrap(), 666);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(op2().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 667);
|
|
|
|
assert_eq!(op2().or_else(|e| Err::<isize, &'static str>(e)).unwrap_err(),
|
2014-06-28 15:57:36 -05:00
|
|
|
"sadface");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_impl_map() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert!(Ok::<isize, isize>(1).map(|x| x + 1) == Ok(2));
|
|
|
|
assert!(Err::<isize, isize>(1).map(|x| x + 1) == Err(1));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_impl_map_err() {
|
2015-03-25 19:06:52 -05:00
|
|
|
assert!(Ok::<isize, isize>(1).map_err(|x| x + 1) == Ok(1));
|
|
|
|
assert!(Err::<isize, isize>(1).map_err(|x| x + 1) == Err(2));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_collect() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Result<Vec<isize>, ()> = (0..0).map(|_| Ok::<isize, ()>(0)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Ok(vec![]));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Result<Vec<isize>, ()> = (0..3).map(|x| Ok::<isize, ()>(x)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Ok(vec![0, 1, 2]));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Result<Vec<isize>, isize> = (0..3).map(|x| {
|
2014-10-15 01:05:01 -05:00
|
|
|
if x > 1 { Err(x) } else { Ok(x) }
|
|
|
|
}).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Err(2));
|
|
|
|
|
|
|
|
// test that it does not take more elements than it needs
|
2015-03-25 19:06:52 -05:00
|
|
|
let mut functions: [Box<Fn() -> Result<(), isize>>; 3] =
|
2015-01-25 15:05:03 -06:00
|
|
|
[box || Ok(()), box || Err(1), box || panic!()];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Err(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_fmt_default() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let ok: Result<isize, &'static str> = Ok(100);
|
|
|
|
let err: Result<isize, &'static str> = Err("Err");
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2014-12-20 02:09:35 -06:00
|
|
|
let s = format!("{:?}", ok);
|
2015-01-20 17:45:07 -06:00
|
|
|
assert_eq!(s, "Ok(100)");
|
2014-12-20 02:09:35 -06:00
|
|
|
let s = format!("{:?}", err);
|
|
|
|
assert_eq!(s, "Err(\"Err\")");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_unwrap_or() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let ok: Result<isize, &'static str> = Ok(100);
|
|
|
|
let ok_err: Result<isize, &'static str> = Err("Err");
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert_eq!(ok.unwrap_or(50), 100);
|
|
|
|
assert_eq!(ok_err.unwrap_or(50), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-04-28 23:22:58 -05:00
|
|
|
fn test_unwrap_or_else() {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn handler(msg: &'static str) -> isize {
|
2014-06-28 15:57:36 -05:00
|
|
|
if msg == "I got this." {
|
2015-01-25 15:05:03 -06:00
|
|
|
50
|
2014-06-28 15:57:36 -05:00
|
|
|
} else {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("BadBad")
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let ok: Result<isize, &'static str> = Ok(100);
|
|
|
|
let ok_err: Result<isize, &'static str> = Err("I got this.");
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert_eq!(ok.unwrap_or_else(handler), 100);
|
|
|
|
assert_eq!(ok_err.unwrap_or_else(handler), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2014-10-09 14:17:22 -05:00
|
|
|
pub fn test_unwrap_or_else_panic() {
|
2015-03-25 19:06:52 -05:00
|
|
|
fn handler(msg: &'static str) -> isize {
|
2014-06-28 15:57:36 -05:00
|
|
|
if msg == "I got this." {
|
2015-01-25 15:05:03 -06:00
|
|
|
50
|
2014-06-28 15:57:36 -05:00
|
|
|
} else {
|
2014-10-09 14:17:22 -05:00
|
|
|
panic!("BadBad")
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
|
|
|
|
let _ : isize = bad_err.unwrap_or_else(handler);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
2015-06-13 09:34:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_expect_ok() {
|
|
|
|
let ok: Result<isize, &'static str> = Ok(100);
|
|
|
|
assert_eq!(ok.expect("Unexpected error"), 100);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected="Got expected error: \"All good\"")]
|
|
|
|
pub fn test_expect_err() {
|
|
|
|
let err: Result<isize, &'static str> = Err("All good");
|
|
|
|
err.expect("Got expected error");
|
|
|
|
}
|