1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 fn op1() -> Result<isize, &'static str> { Ok(666) }
12 fn op2() -> Result<isize, &'static str> { Err("sadface") }
16 assert_eq!(op1().and(Ok(667)).unwrap(), 667);
17 assert_eq!(op1().and(Err::<i32, &'static str>("bad")).unwrap_err(),
20 assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface");
21 assert_eq!(op2().and(Err::<i32,&'static str>("bad")).unwrap_err(),
27 assert_eq!(op1().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap(), 667);
28 assert_eq!(op1().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
31 assert_eq!(op2().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap_err(),
33 assert_eq!(op2().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
39 assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
40 assert_eq!(op1().or(Err("bad")).unwrap(), 666);
42 assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
43 assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
48 assert_eq!(op1().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 666);
49 assert_eq!(op1().or_else(|e| Err::<isize, &'static str>(e)).unwrap(), 666);
51 assert_eq!(op2().or_else(|_| Ok::<isize, &'static str>(667)).unwrap(), 667);
52 assert_eq!(op2().or_else(|e| Err::<isize, &'static str>(e)).unwrap_err(),
58 assert!(Ok::<isize, isize>(1).map(|x| x + 1) == Ok(2));
59 assert!(Err::<isize, isize>(1).map(|x| x + 1) == Err(1));
63 fn test_impl_map_err() {
64 assert!(Ok::<isize, isize>(1).map_err(|x| x + 1) == Ok(1));
65 assert!(Err::<isize, isize>(1).map_err(|x| x + 1) == Err(2));
70 let v: Result<Vec<isize>, ()> = (0..0).map(|_| Ok::<isize, ()>(0)).collect();
71 assert!(v == Ok(vec![]));
73 let v: Result<Vec<isize>, ()> = (0..3).map(|x| Ok::<isize, ()>(x)).collect();
74 assert!(v == Ok(vec![0, 1, 2]));
76 let v: Result<Vec<isize>, isize> = (0..3).map(|x| {
77 if x > 1 { Err(x) } else { Ok(x) }
81 // test that it does not take more elements than it needs
82 let mut functions: [Box<Fn() -> Result<(), isize>>; 3] =
83 [box || Ok(()), box || Err(1), box || panic!()];
85 let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
90 fn test_fmt_default() {
91 let ok: Result<isize, &'static str> = Ok(100);
92 let err: Result<isize, &'static str> = Err("Err");
94 let s = format!("{:?}", ok);
95 assert_eq!(s, "Ok(100)");
96 let s = format!("{:?}", err);
97 assert_eq!(s, "Err(\"Err\")");
101 fn test_unwrap_or() {
102 let ok: Result<isize, &'static str> = Ok(100);
103 let ok_err: Result<isize, &'static str> = Err("Err");
105 assert_eq!(ok.unwrap_or(50), 100);
106 assert_eq!(ok_err.unwrap_or(50), 50);
110 fn test_unwrap_or_else() {
111 fn handler(msg: &'static str) -> isize {
112 if msg == "I got this." {
119 let ok: Result<isize, &'static str> = Ok(100);
120 let ok_err: Result<isize, &'static str> = Err("I got this.");
122 assert_eq!(ok.unwrap_or_else(handler), 100);
123 assert_eq!(ok_err.unwrap_or_else(handler), 50);
128 pub fn test_unwrap_or_else_panic() {
129 fn handler(msg: &'static str) -> isize {
130 if msg == "I got this." {
137 let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
138 let _ : isize = bad_err.unwrap_or_else(handler);