]> git.proxmox.com Git - rustc.git/blame - src/libcoretest/result.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / libcoretest / result.rs
CommitLineData
1a4d82fc
JJ
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
d9579d0f
AL
11fn op1() -> Result<isize, &'static str> { Ok(666) }
12fn op2() -> Result<isize, &'static str> { Err("sadface") }
1a4d82fc
JJ
13
14#[test]
d9579d0f 15fn test_and() {
85aaf69f
SL
16 assert_eq!(op1().and(Ok(667)).unwrap(), 667);
17 assert_eq!(op1().and(Err::<i32, &'static str>("bad")).unwrap_err(),
1a4d82fc
JJ
18 "bad");
19
85aaf69f
SL
20 assert_eq!(op2().and(Ok(667)).unwrap_err(), "sadface");
21 assert_eq!(op2().and(Err::<i32,&'static str>("bad")).unwrap_err(),
1a4d82fc
JJ
22 "sadface");
23}
24
25#[test]
d9579d0f 26fn test_and_then() {
c34b1796
AL
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(),
1a4d82fc
JJ
29 "bad");
30
c34b1796 31 assert_eq!(op2().and_then(|i| Ok::<isize, &'static str>(i + 1)).unwrap_err(),
1a4d82fc 32 "sadface");
c34b1796 33 assert_eq!(op2().and_then(|_| Err::<isize, &'static str>("bad")).unwrap_err(),
1a4d82fc
JJ
34 "sadface");
35}
36
37#[test]
d9579d0f 38fn test_or() {
c34b1796 39 assert_eq!(op1().or(Ok::<_, &'static str>(667)).unwrap(), 666);
1a4d82fc
JJ
40 assert_eq!(op1().or(Err("bad")).unwrap(), 666);
41
c34b1796 42 assert_eq!(op2().or(Ok::<_, &'static str>(667)).unwrap(), 667);
1a4d82fc
JJ
43 assert_eq!(op2().or(Err("bad")).unwrap_err(), "bad");
44}
45
46#[test]
d9579d0f 47fn test_or_else() {
c34b1796
AL
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);
1a4d82fc 50
c34b1796
AL
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(),
1a4d82fc
JJ
53 "sadface");
54}
55
56#[test]
d9579d0f 57fn test_impl_map() {
c34b1796
AL
58 assert!(Ok::<isize, isize>(1).map(|x| x + 1) == Ok(2));
59 assert!(Err::<isize, isize>(1).map(|x| x + 1) == Err(1));
1a4d82fc
JJ
60}
61
62#[test]
d9579d0f 63fn test_impl_map_err() {
c34b1796
AL
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));
1a4d82fc
JJ
66}
67
1a4d82fc
JJ
68#[test]
69fn test_collect() {
c34b1796 70 let v: Result<Vec<isize>, ()> = (0..0).map(|_| Ok::<isize, ()>(0)).collect();
1a4d82fc
JJ
71 assert!(v == Ok(vec![]));
72
c34b1796 73 let v: Result<Vec<isize>, ()> = (0..3).map(|x| Ok::<isize, ()>(x)).collect();
1a4d82fc
JJ
74 assert!(v == Ok(vec![0, 1, 2]));
75
c34b1796 76 let v: Result<Vec<isize>, isize> = (0..3).map(|x| {
1a4d82fc
JJ
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
c34b1796 82 let mut functions: [Box<Fn() -> Result<(), isize>>; 3] =
85aaf69f 83 [box || Ok(()), box || Err(1), box || panic!()];
1a4d82fc 84
c34b1796 85 let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
1a4d82fc
JJ
86 assert!(v == Err(1));
87}
1a4d82fc
JJ
88
89#[test]
d9579d0f 90fn test_fmt_default() {
c34b1796
AL
91 let ok: Result<isize, &'static str> = Ok(100);
92 let err: Result<isize, &'static str> = Err("Err");
1a4d82fc
JJ
93
94 let s = format!("{:?}", ok);
85aaf69f 95 assert_eq!(s, "Ok(100)");
1a4d82fc
JJ
96 let s = format!("{:?}", err);
97 assert_eq!(s, "Err(\"Err\")");
98}
99
100#[test]
d9579d0f 101fn test_unwrap_or() {
c34b1796
AL
102 let ok: Result<isize, &'static str> = Ok(100);
103 let ok_err: Result<isize, &'static str> = Err("Err");
1a4d82fc
JJ
104
105 assert_eq!(ok.unwrap_or(50), 100);
106 assert_eq!(ok_err.unwrap_or(50), 50);
107}
108
109#[test]
d9579d0f 110fn test_unwrap_or_else() {
c34b1796 111 fn handler(msg: &'static str) -> isize {
1a4d82fc 112 if msg == "I got this." {
85aaf69f 113 50
1a4d82fc
JJ
114 } else {
115 panic!("BadBad")
116 }
117 }
118
c34b1796
AL
119 let ok: Result<isize, &'static str> = Ok(100);
120 let ok_err: Result<isize, &'static str> = Err("I got this.");
1a4d82fc
JJ
121
122 assert_eq!(ok.unwrap_or_else(handler), 100);
123 assert_eq!(ok_err.unwrap_or_else(handler), 50);
124}
125
126#[test]
c34b1796 127#[should_panic]
1a4d82fc 128pub fn test_unwrap_or_else_panic() {
c34b1796 129 fn handler(msg: &'static str) -> isize {
1a4d82fc 130 if msg == "I got this." {
85aaf69f 131 50
1a4d82fc
JJ
132 } else {
133 panic!("BadBad")
134 }
135 }
136
c34b1796
AL
137 let bad_err: Result<isize, &'static str> = Err("Unrecoverable mess.");
138 let _ : isize = bad_err.unwrap_or_else(handler);
1a4d82fc 139}