summaryrefslogtreecommitdiffstats
path: root/third_party/rust/failure_derive/tests/wraps.rs
blob: 9144325cad5d73beb69cadd19d42eb1f8ec79715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
extern crate failure;
#[macro_use]
extern crate failure_derive;

use std::fmt;
use std::io;

use failure::{Backtrace, Fail};

#[derive(Fail, Debug)]
#[fail(display = "An error has occurred: {}", inner)]
struct WrapError {
    #[fail(cause)]
    inner: io::Error,
}

#[test]
fn wrap_error() {
    let inner = io::Error::from_raw_os_error(98);
    let err = WrapError { inner };
    assert!(err
        .cause()
        .and_then(|err| err.downcast_ref::<io::Error>())
        .is_some());
}

#[derive(Fail, Debug)]
#[fail(display = "An error has occurred: {}", _0)]
struct WrapTupleError(#[fail(cause)] io::Error);

#[test]
fn wrap_tuple_error() {
    let io_error = io::Error::from_raw_os_error(98);
    let err: WrapTupleError = WrapTupleError(io_error);
    assert!(err
        .cause()
        .and_then(|err| err.downcast_ref::<io::Error>())
        .is_some());
}

#[derive(Fail, Debug)]
#[fail(display = "An error has occurred: {}", inner)]
struct WrapBacktraceError {
    #[fail(cause)]
    inner: io::Error,
    backtrace: Backtrace,
}

#[test]
fn wrap_backtrace_error() {
    let inner = io::Error::from_raw_os_error(98);
    let err: WrapBacktraceError = WrapBacktraceError {
        inner,
        backtrace: Backtrace::new(),
    };
    assert!(err
        .cause()
        .and_then(|err| err.downcast_ref::<io::Error>())
        .is_some());
    assert!(err.backtrace().is_some());
    assert!(err.backtrace().unwrap().is_empty());
    assert!(err.backtrace().unwrap().to_string().trim().is_empty());
}

#[derive(Fail, Debug)]
enum WrapEnumError {
    #[fail(display = "An error has occurred: {}", _0)]
    Io(#[fail(cause)] io::Error),
    #[fail(display = "An error has occurred: {}", inner)]
    Fmt {
        #[fail(cause)]
        inner: fmt::Error,
        backtrace: Backtrace,
    },
}

#[test]
fn wrap_enum_error() {
    let io_error = io::Error::from_raw_os_error(98);
    let err: WrapEnumError = WrapEnumError::Io(io_error);
    assert!(err
        .cause()
        .and_then(|err| err.downcast_ref::<io::Error>())
        .is_some());
    assert!(err.backtrace().is_none());
    let fmt_error = fmt::Error::default();
    let err: WrapEnumError = WrapEnumError::Fmt {
        inner: fmt_error,
        backtrace: Backtrace::new(),
    };
    assert!(err
        .cause()
        .and_then(|err| err.downcast_ref::<fmt::Error>())
        .is_some());
    assert!(err.backtrace().is_some());
    assert!(err.backtrace().unwrap().is_empty());
    assert!(err.backtrace().unwrap().to_string().trim().is_empty());
}