]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/result.rs
rollup merge of #19577: aidancully/master
[rust.git] / src / libcoretest / result.rs
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.
4 //
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.
10
11 use core::iter::range;
12
13 pub fn op1() -> Result<int, &'static str> { Ok(666) }
14 pub fn op2() -> Result<int, &'static str> { Err("sadface") }
15
16 #[test]
17 pub fn test_and() {
18     assert_eq!(op1().and(Ok(667i)).unwrap(), 667);
19     assert_eq!(op1().and(Err::<(), &'static str>("bad")).unwrap_err(),
20                "bad");
21
22     assert_eq!(op2().and(Ok(667i)).unwrap_err(), "sadface");
23     assert_eq!(op2().and(Err::<(),&'static str>("bad")).unwrap_err(),
24                "sadface");
25 }
26
27 #[test]
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(),
31                "bad");
32
33     assert_eq!(op2().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap_err(),
34                "sadface");
35     assert_eq!(op2().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
36                "sadface");
37 }
38
39 #[test]
40 pub fn test_or() {
41     assert_eq!(op1().or(Ok(667)).unwrap(), 666);
42     assert_eq!(op1().or(Err("bad")).unwrap(), 666);
43
44     assert_eq!(op2().or(Ok(667)).unwrap(), 667);
45     assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
46 }
47
48 #[test]
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);
52
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(),
55                "sadface");
56 }
57
58 #[test]
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));
62 }
63
64 #[test]
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));
68 }
69
70 #[test]
71 fn test_collect() {
72     let v: Result<Vec<int>, ()> = range(0i, 0).map(|_| Ok::<int, ()>(0)).collect();
73     assert!(v == Ok(vec![]));
74
75     let v: Result<Vec<int>, ()> = range(0i, 3).map(|x| Ok::<int, ()>(x)).collect();
76     assert!(v == Ok(vec![0, 1, 2]));
77
78     let v: Result<Vec<int>, int> = range(0i, 3).map(|x| {
79         if x > 1 { Err(x) } else { Ok(x) }
80     }).collect();
81     assert!(v == Err(2));
82
83     // test that it does not take more elements than it needs
84     let mut functions = [|| Ok(()), || Err(1i), || panic!()];
85
86     let v: Result<Vec<()>, int> = functions.iter_mut().map(|f| (*f)()).collect();
87     assert!(v == Err(1));
88 }
89
90 #[test]
91 pub fn test_fmt_default() {
92     let ok: Result<int, &'static str> = Ok(100);
93     let err: Result<int, &'static str> = Err("Err");
94
95     let s = format!("{}", ok);
96     assert_eq!(s, "Ok(100)");
97     let s = format!("{}", err);
98     assert_eq!(s, "Err(Err)");
99 }
100
101 #[test]
102 pub fn test_unwrap_or() {
103     let ok: Result<int, &'static str> = Ok(100i);
104     let ok_err: Result<int, &'static str> = Err("Err");
105
106     assert_eq!(ok.unwrap_or(50), 100);
107     assert_eq!(ok_err.unwrap_or(50), 50);
108 }
109
110 #[test]
111 pub fn test_unwrap_or_else() {
112     fn handler(msg: &'static str) -> int {
113         if msg == "I got this." {
114             50i
115         } else {
116             panic!("BadBad")
117         }
118     }
119
120     let ok: Result<int, &'static str> = Ok(100);
121     let ok_err: Result<int, &'static str> = Err("I got this.");
122
123     assert_eq!(ok.unwrap_or_else(handler), 100);
124     assert_eq!(ok_err.unwrap_or_else(handler), 50);
125 }
126
127 #[test]
128 #[should_fail]
129 pub fn test_unwrap_or_else_panic() {
130     fn handler(msg: &'static str) -> int {
131         if msg == "I got this." {
132             50i
133         } else {
134             panic!("BadBad")
135         }
136     }
137
138     let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
139     let _ : int = bad_err.unwrap_or_else(handler);
140 }