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 use core::iter::range;
13 pub fn op1() -> Result<int, &'static str> { Ok(666) }
14 pub fn op2() -> Result<int, &'static str> { Err("sadface") }
18 assert_eq!(op1().and(Ok(667i)).unwrap(), 667);
19 assert_eq!(op1().and(Err::<(), &'static str>("bad")).unwrap_err(),
22 assert_eq!(op2().and(Ok(667i)).unwrap_err(), "sadface");
23 assert_eq!(op2().and(Err::<(),&'static str>("bad")).unwrap_err(),
28 pub fn test_and_then() {
29 assert_eq!(op1().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap(), 667);
30 assert_eq!(op1().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
33 assert_eq!(op2().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap_err(),
35 assert_eq!(op2().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
41 assert_eq!(op1().or(Ok(667)).unwrap(), 666);
42 assert_eq!(op1().or(Err("bad")).unwrap(), 666);
44 assert_eq!(op2().or(Ok(667)).unwrap(), 667);
45 assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
49 pub fn test_or_else() {
50 assert_eq!(op1().or_else(|_| Ok::<int, &'static str>(667)).unwrap(), 666);
51 assert_eq!(op1().or_else(|e| Err::<int, &'static str>(e)).unwrap(), 666);
53 assert_eq!(op2().or_else(|_| Ok::<int, &'static str>(667)).unwrap(), 667);
54 assert_eq!(op2().or_else(|e| Err::<int, &'static str>(e)).unwrap_err(),
59 pub fn test_impl_map() {
60 assert!(Ok::<int, int>(1).map(|x| x + 1) == Ok(2));
61 assert!(Err::<int, int>(1).map(|x| x + 1) == Err(1));
65 pub fn test_impl_map_err() {
66 assert!(Ok::<int, int>(1).map_err(|x| x + 1) == Ok(1));
67 assert!(Err::<int, int>(1).map_err(|x| x + 1) == Err(2));
73 let v: Result<Vec<int>, ()> = range(0i, 0).map(|_| Ok::<int, ()>(0)).collect();
74 assert!(v == Ok(vec![]));
76 let v: Result<Vec<int>, ()> = range(0i, 3).map(|x| Ok::<int, ()>(x)).collect();
77 assert!(v == Ok(vec![0, 1, 2]));
79 let v: Result<Vec<int>, int> = range(0i, 3).map(|x| {
80 if x > 1 { Err(x) } else { Ok(x) }
84 // test that it does not take more elements than it needs
85 let mut functions: [Box<Fn() -> Result<(), int>>; 3] =
86 [box || Ok(()), box || Err(1i), box || panic!()];
88 let v: Result<Vec<()>, int> = functions.iter_mut().map(|f| (*f)()).collect();
94 pub fn test_fmt_default() {
95 let ok: Result<int, &'static str> = Ok(100);
96 let err: Result<int, &'static str> = Err("Err");
98 let s = format!("{}", ok);
99 assert_eq!(s, "Ok(100)");
100 let s = format!("{}", err);
101 assert_eq!(s, "Err(Err)");
105 pub fn test_unwrap_or() {
106 let ok: Result<int, &'static str> = Ok(100i);
107 let ok_err: Result<int, &'static str> = Err("Err");
109 assert_eq!(ok.unwrap_or(50), 100);
110 assert_eq!(ok_err.unwrap_or(50), 50);
114 pub fn test_unwrap_or_else() {
115 fn handler(msg: &'static str) -> int {
116 if msg == "I got this." {
123 let ok: Result<int, &'static str> = Ok(100);
124 let ok_err: Result<int, &'static str> = Err("I got this.");
126 assert_eq!(ok.unwrap_or_else(handler), 100);
127 assert_eq!(ok_err.unwrap_or_else(handler), 50);
132 pub fn test_unwrap_or_else_panic() {
133 fn handler(msg: &'static str) -> int {
134 if msg == "I got this." {
141 let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
142 let _ : int = bad_err.unwrap_or_else(handler);