]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/result.rs
Auto merge of #27630 - sylvestre:master, r=dotdash
[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 fn op1() -> Result<isize, &'static str> { Ok(666) }
12 fn op2() -> Result<isize, &'static str> { Err("sadface") }
13
14 #[test]
15 fn test_and() {
16     assert_eq!(op1().and(Ok(667)).unwrap(), 667);
17     assert_eq!(op1().and(Err::<i32, &'static str>("bad")).unwrap_err(),
18                "bad");
19
20     assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface");
21     assert_eq!(op2().and(Err::<i32,&'static str>("bad")).unwrap_err(),
22                "sadface");
23 }
24
25 #[test]
26 fn test_and_then() {
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(),
29                "bad");
30
31     assert_eq!(op2().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap_err(),
32                "sadface");
33     assert_eq!(op2().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
34                "sadface");
35 }
36
37 #[test]
38 fn test_or() {
39     assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
40     assert_eq!(op1().or(Err("bad")).unwrap(), 666);
41
42     assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
43     assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
44 }
45
46 #[test]
47 fn test_or_else() {
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);
50
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(),
53                "sadface");
54 }
55
56 #[test]
57 fn test_impl_map() {
58     assert!(Ok::<isize, isize>(1).map(|x| x + 1) == Ok(2));
59     assert!(Err::<isize, isize>(1).map(|x| x + 1) == Err(1));
60 }
61
62 #[test]
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));
66 }
67
68 #[test]
69 fn test_collect() {
70     let v: Result<Vec<isize>, ()> = (0..0).map(|_| Ok::<isize, ()>(0)).collect();
71     assert!(v == Ok(vec![]));
72
73     let v: Result<Vec<isize>, ()> = (0..3).map(|x| Ok::<isize, ()>(x)).collect();
74     assert!(v == Ok(vec![0, 1, 2]));
75
76     let v: Result<Vec<isize>, isize> = (0..3).map(|x| {
77         if x > 1 { Err(x) } else { Ok(x) }
78     }).collect();
79     assert!(v == Err(2));
80
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!()];
84
85     let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
86     assert!(v == Err(1));
87 }
88
89 #[test]
90 fn test_fmt_default() {
91     let ok: Result<isize, &'static str> = Ok(100);
92     let err: Result<isize, &'static str> = Err("Err");
93
94     let s = format!("{:?}", ok);
95     assert_eq!(s, "Ok(100)");
96     let s = format!("{:?}", err);
97     assert_eq!(s, "Err(\"Err\")");
98 }
99
100 #[test]
101 fn test_unwrap_or() {
102     let ok: Result<isize, &'static str> = Ok(100);
103     let ok_err: Result<isize, &'static str> = Err("Err");
104
105     assert_eq!(ok.unwrap_or(50), 100);
106     assert_eq!(ok_err.unwrap_or(50), 50);
107 }
108
109 #[test]
110 fn test_unwrap_or_else() {
111     fn handler(msg: &'static str) -> isize {
112         if msg == "I got this." {
113             50
114         } else {
115             panic!("BadBad")
116         }
117     }
118
119     let ok: Result<isize, &'static str> = Ok(100);
120     let ok_err: Result<isize, &'static str> = Err("I got this.");
121
122     assert_eq!(ok.unwrap_or_else(handler), 100);
123     assert_eq!(ok_err.unwrap_or_else(handler), 50);
124 }
125
126 #[test]
127 #[should_panic]
128 pub fn test_unwrap_or_else_panic() {
129     fn handler(msg: &'static str) -> isize {
130         if msg == "I got this." {
131             50
132         } else {
133             panic!("BadBad")
134         }
135     }
136
137     let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
138     let _ : isize = bad_err.unwrap_or_else(handler);
139 }
140
141
142 #[test]
143 pub fn test_expect_ok() {
144     let ok: Result<isize, &'static str> = Ok(100);
145     assert_eq!(ok.expect("Unexpected error"), 100);
146 }
147 #[test]
148 #[should_panic(expected="Got expected error: \"All good\"")]
149 pub fn test_expect_err() {
150     let err: Result<isize, &'static str> = Err("All good");
151     err.expect("Got expected error");
152 }