]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/result.rs
rollup merge of #20518: nagisa/weighted-bool
[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 /* FIXME(#20575)
71 #[test]
72 fn test_collect() {
73     let v: Result<Vec<int>, ()> = range(0i, 0).map(|_| Ok::<int, ()>(0)).collect();
74     assert!(v == Ok(vec![]));
75
76     let v: Result<Vec<int>, ()> = range(0i, 3).map(|x| Ok::<int, ()>(x)).collect();
77     assert!(v == Ok(vec![0, 1, 2]));
78
79     let v: Result<Vec<int>, int> = range(0i, 3).map(|x| {
80         if x > 1 { Err(x) } else { Ok(x) }
81     }).collect();
82     assert!(v == Err(2));
83
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!()];
87
88     let v: Result<Vec<()>, int> = functions.iter_mut().map(|f| (*f)()).collect();
89     assert!(v == Err(1));
90 }
91 */
92
93 #[test]
94 pub fn test_fmt_default() {
95     let ok: Result<int, &'static str> = Ok(100);
96     let err: Result<int, &'static str> = Err("Err");
97
98     let s = format!("{}", ok);
99     assert_eq!(s, "Ok(100)");
100     let s = format!("{}", err);
101     assert_eq!(s, "Err(Err)");
102 }
103
104 #[test]
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");
108
109     assert_eq!(ok.unwrap_or(50), 100);
110     assert_eq!(ok_err.unwrap_or(50), 50);
111 }
112
113 #[test]
114 pub fn test_unwrap_or_else() {
115     fn handler(msg: &'static str) -> int {
116         if msg == "I got this." {
117             50i
118         } else {
119             panic!("BadBad")
120         }
121     }
122
123     let ok: Result<int, &'static str> = Ok(100);
124     let ok_err: Result<int, &'static str> = Err("I got this.");
125
126     assert_eq!(ok.unwrap_or_else(handler), 100);
127     assert_eq!(ok_err.unwrap_or_else(handler), 50);
128 }
129
130 #[test]
131 #[should_fail]
132 pub fn test_unwrap_or_else_panic() {
133     fn handler(msg: &'static str) -> int {
134         if msg == "I got this." {
135             50i
136         } else {
137             panic!("BadBad")
138         }
139     }
140
141     let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
142     let _ : int = bad_err.unwrap_or_else(handler);
143 }