]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/result.rs
debuginfo: Make debuginfo source location assignment more stable (Pt. 1)
[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 pub fn op1() -> Result<int, &'static str> { Ok(666) }
12 pub fn op2() -> Result<int, &'static str> { Err("sadface") }
13
14 #[test]
15 pub fn test_and() {
16     assert_eq!(op1().and(Ok(667i)).unwrap(), 667);
17     assert_eq!(op1().and(Err::<(), &'static str>("bad")).unwrap_err(),
18                "bad");
19
20     assert_eq!(op2().and(Ok(667i)).unwrap_err(), "sadface");
21     assert_eq!(op2().and(Err::<(),&'static str>("bad")).unwrap_err(),
22                "sadface");
23 }
24
25 #[test]
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(),
29                "bad");
30
31     assert_eq!(op2().and_then(|i| Ok::<int, &'static str>(i + 1)).unwrap_err(),
32                "sadface");
33     assert_eq!(op2().and_then(|_| Err::<int, &'static str>("bad")).unwrap_err(),
34                "sadface");
35 }
36
37 #[test]
38 pub fn test_or() {
39     assert_eq!(op1().or(Ok(667)).unwrap(), 666);
40     assert_eq!(op1().or(Err("bad")).unwrap(), 666);
41
42     assert_eq!(op2().or(Ok(667)).unwrap(), 667);
43     assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
44 }
45
46 #[test]
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);
50
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(),
53                "sadface");
54 }
55
56 #[test]
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));
60 }
61
62 #[test]
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));
66 }
67
68 /* FIXME(#20575)
69 #[test]
70 fn test_collect() {
71     let v: Result<Vec<int>, ()> = range(0i, 0).map(|_| Ok::<int, ()>(0)).collect();
72     assert!(v == Ok(vec![]));
73
74     let v: Result<Vec<int>, ()> = range(0i, 3).map(|x| Ok::<int, ()>(x)).collect();
75     assert!(v == Ok(vec![0, 1, 2]));
76
77     let v: Result<Vec<int>, int> = range(0i, 3).map(|x| {
78         if x > 1 { Err(x) } else { Ok(x) }
79     }).collect();
80     assert!(v == Err(2));
81
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!()];
85
86     let v: Result<Vec<()>, int> = functions.iter_mut().map(|f| (*f)()).collect();
87     assert!(v == Err(1));
88 }
89 */
90
91 #[test]
92 pub fn test_fmt_default() {
93     let ok: Result<int, &'static str> = Ok(100);
94     let err: Result<int, &'static str> = Err("Err");
95
96     let s = format!("{:?}", ok);
97     assert_eq!(s, "Ok(100i)");
98     let s = format!("{:?}", err);
99     assert_eq!(s, "Err(\"Err\")");
100 }
101
102 #[test]
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");
106
107     assert_eq!(ok.unwrap_or(50), 100);
108     assert_eq!(ok_err.unwrap_or(50), 50);
109 }
110
111 #[test]
112 pub fn test_unwrap_or_else() {
113     fn handler(msg: &'static str) -> int {
114         if msg == "I got this." {
115             50i
116         } else {
117             panic!("BadBad")
118         }
119     }
120
121     let ok: Result<int, &'static str> = Ok(100);
122     let ok_err: Result<int, &'static str> = Err("I got this.");
123
124     assert_eq!(ok.unwrap_or_else(handler), 100);
125     assert_eq!(ok_err.unwrap_or_else(handler), 50);
126 }
127
128 #[test]
129 #[should_fail]
130 pub fn test_unwrap_or_else_panic() {
131     fn handler(msg: &'static str) -> int {
132         if msg == "I got this." {
133             50i
134         } else {
135             panic!("BadBad")
136         }
137     }
138
139     let bad_err: Result<int, &'static str> = Err("Unrecoverable mess.");
140     let _ : int = bad_err.unwrap_or_else(handler);
141 }