]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/finally.rs
rollup merge of #20642: michaelwoerister/sane-source-locations-pt1
[rust.git] / src / libcoretest / finally.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 #![allow(deprecated)]
12
13 use core::finally::{try_finally, Finally};
14 use std::thread::Thread;
15
16 #[test]
17 fn test_success() {
18     let mut i = 0i;
19     try_finally(
20         &mut i, (),
21         |i, ()| {
22             *i = 10;
23         },
24         |i| {
25             assert!(!Thread::panicking());
26             assert_eq!(*i, 10);
27             *i = 20;
28         });
29     assert_eq!(i, 20);
30 }
31
32 #[test]
33 #[should_fail]
34 fn test_fail() {
35     let mut i = 0i;
36     try_finally(
37         &mut i, (),
38         |i, ()| {
39             *i = 10;
40             panic!();
41         },
42         |i| {
43             assert!(Thread::panicking());
44             assert_eq!(*i, 10);
45         })
46 }
47
48 #[test]
49 fn test_retval() {
50     let mut closure = |&mut:| 10i;
51     let i = closure.finally(|| { });
52     assert_eq!(i, 10);
53 }
54
55 #[test]
56 fn test_compact() {
57     fn do_some_fallible_work() {}
58     fn but_always_run_this_function() { }
59     let mut f = do_some_fallible_work;
60     f.finally(but_always_run_this_function);
61 }