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 pub fn op1() -> Result<int, &'static str> { Ok(666) }
12 pub fn op2() -> Result<int, &'static str> { Err("sadface") }
16 assert_eq!(op1().and(Ok(667i)).unwrap(), 667);
17 assert_eq!(op1().and(Err::<(), &'static str>("bad")).unwrap_err(),
20 assert_eq!(op2().and(Ok(667i)).unwrap_err(), "sadface");
21 assert_eq!(op2().and(Err::<(),&'static str>("bad")).unwrap_err(),
26 pub fn test_and_then() {
27 assert_eq!(op1().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap(), 667);
28 assert_eq!(op1().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
31 assert_eq!(op2().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap_err(),
33 assert_eq!(op2().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
39 assert_eq!(op1().or(Ok(667)).unwrap(), 666);
40 assert_eq!(op1().or(Err("bad")).unwrap(), 666);
42 assert_eq!(op2().or(Ok(667)).unwrap(), 667);
43 assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
47 pub fn test_or_else() {
48 assert_eq!(op1().or_else(|_| Ok::<int, &'static str>(667)).unwrap(), 666);
49 assert_eq!(op1().or_else(|e| Err::<int, &'static str>(e)).unwrap(), 666);
51 assert_eq!(op2().or_else(|_| Ok::<int, &'static str>(667)).unwrap(), 667);
52 assert_eq!(op2().or_else(|e| Err::<int, &'static str>(e)).unwrap_err(),
57 pub fn test_impl_map() {
58 assert!(Ok::<int, int>(1).map(|x| x + 1) == Ok(2));
59 assert!(Err::<int, int>(1).map(|x| x + 1) == Err(1));
63 pub fn test_impl_map_err() {
64 assert!(Ok::<int, int>(1).map_err(|x| x + 1) == Ok(1));
65 assert!(Err::<int, int>(1).map_err(|x| x + 1) == Err(2));
71 let v: Result<Vec<int>, ()> = range(0i, 0).map(|_| Ok::<int, ()>(0)).collect();
72 assert!(v == Ok(vec![]));
74 let v: Result<Vec<int>, ()> = range(0i, 3).map(|x| Ok::<int, ()>(x)).collect();
75 assert!(v == Ok(vec![0, 1, 2]));
77 let v: Result<Vec<int>, int> = range(0i, 3).map(|x| {
78 if x > 1 { Err(x) } else { Ok(x) }
82 // test that it does not take more elements than it needs
83 let mut functions: [Box<Fn() -> Result<(), int>>; 3] =
84 [box || Ok(()), box || Err(1i), box || panic!()];
86 let v: Result<Vec<()>, int> = functions.iter_mut().map(|f| (*f)()).collect();
92 pub fn test_fmt_default() {
93 let ok: Result<int, &'static str> = Ok(100);
94 let err: Result<int, &'static str> = Err("Err");
96 let s = format!("{:?}", ok);
97 assert_eq!(s, "Ok(100i)");
98 let s = format!("{:?}", err);
99 assert_eq!(s, "Err(\"Err\")");
103 pub fn test_unwrap_or() {
104 let ok: Result<int, &'static str> = Ok(100i);
105 let ok_err: Result<int, &'static str> = Err("Err");
107 assert_eq!(ok.unwrap_or(50), 100);
108 assert_eq!(ok_err.unwrap_or(50), 50);
112 pub fn test_unwrap_or_else() {
113 fn handler(msg: &'static str) -> int {
114 if msg == "I got this." {
121 let ok: Result<int, &'static str> = Ok(100);
122 let ok_err: Result<int, &'static str> = Err("I got this.");
124 assert_eq!(ok.unwrap_or_else(handler), 100);
125 assert_eq!(ok_err.unwrap_or_else(handler), 50);
130 pub fn test_unwrap_or_else_panic() {
131 fn handler(msg: &'static str) -> int {
132 if msg == "I got this." {
139 let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
140 let _ : int = bad_err.unwrap_or_else(handler);