File: tests.rs

package info (click to toggle)
rust-err-derive 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 164 kB
  • sloc: makefile: 4
file content (175 lines) | stat: -rw-r--r-- 4,288 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![cfg(feature = "std")]
use std::error::Error;

#[macro_use]
extern crate err_derive;

#[derive(Debug, Error)]
#[error(display = "An error has occurred.")]
struct UnitError;

#[test]
fn unit_struct() {
    let s = format!("{}", UnitError);
    assert_eq!(&s[..], "An error has occurred.");
}

#[derive(Debug, Error)]
#[error(display = "Error code: {}", code)]
struct RecordError {
    code: u32,
}

#[test]
fn record_struct() {
    let s = format!("{}", RecordError { code: 0 });
    assert_eq!(&s[..], "Error code: 0");
}

#[derive(Debug, Error)]
#[error(display = "Error code: {}", _0)]
struct TupleError(i32);

#[test]
fn tuple_struct() {
    let s = format!("{}", TupleError(2));
    assert_eq!(&s[..], "Error code: 2");
}

#[derive(Debug, Error)]
enum EnumError {
    #[error(display = "Error code: {}", code)]
    StructVariant { code: i32 },
    #[error(display = "Error: {}", _0)]
    TupleVariant(&'static str),
    #[error(display = "An error has occurred.")]
    UnitVariant,
}

#[test]
fn enum_error() {
    let s = format!("{}", EnumError::StructVariant { code: 2 });
    assert_eq!(&s[..], "Error code: 2");
    let s = format!("{}", EnumError::TupleVariant("foobar"));
    assert_eq!(&s[..], "Error: foobar");
    let s = format!("{}", EnumError::UnitVariant);
    assert_eq!(&s[..], "An error has occurred.");
}

#[derive(Debug, Error)]
enum ErrorWithSource {
    #[error(display = "tuple error")]
    A(#[error(source)] TupleError),
}

#[test]
fn error_with_source() {
    let e = ErrorWithSource::A(TupleError(1));

    assert!(e.source().is_some());
}

#[derive(Debug, Error)]
enum EnumFrom {
    #[error(display = "Tuple error: {}", _0)]
    TupleVariant(#[error(source)] UnitError),
    #[error(display = "Struct error: {}", inner)]
    StructVariant {
        #[error(from)]
        inner: EnumError,
    },
}

#[allow(dead_code)]
#[derive(Debug, Error)]
enum EnumNoFromVariant {
    #[error(display = "First error: {}", _0)]
    First(#[error(source)] UnitError),
    #[error(display = "Second error: {}", _0)]
    Second(#[error(source, no_from)] UnitError),
}

#[allow(dead_code)]
#[derive(Debug, Error)]
#[error(no_from)]
enum EnumNoFromStruct {
    #[error(display = "First error: {}", _0)]
    First(#[error(source)] UnitError),
    #[error(display = "Second error: {}", _0)]
    Second(#[error(source, from)] UnitError),
}

#[test]
fn from_inner() {
    let inner = EnumError::StructVariant { code: 12 };
    let outer = EnumFrom::from(inner);

    let s = format!("{}", outer);
    assert_eq!(&s[..], "Struct error: Error code: 12");

    let s = format!("{}", EnumNoFromVariant::from(UnitError));
    assert_eq!(&s[..], "First error: An error has occurred.");

    let s = format!("{}", EnumNoFromStruct::from(UnitError));
    assert_eq!(&s[..], "Second error: An error has occurred.");
}

#[allow(dead_code)]
#[derive(Debug, Error)]
enum EnumAutoBox {
    #[error(display = "Boxed error: {}", _0)]
    First(#[error(source)] Box<UnitError>),
}

#[test]
fn from_enum_autobox() {
    let s = format!("{}", EnumAutoBox::from(UnitError));
    assert_eq!(&s[..], "Boxed error: An error has occurred.");

    let s = format!("{}", EnumAutoBox::from(Box::new(UnitError)));
    assert_eq!(&s[..], "Boxed error: An error has occurred.");
}

#[derive(Debug, Error)]
pub enum TestsNonFieldDisplayValues {
    #[error(display = "{}", STATIC_STR)]
    A,
    #[error(display = "{}", get_static_str())]
    B,
    #[error(display = "{}", Self::some_method(self))]
    C,
    #[error(display = "{}", u8::max_value())]
    D,
    #[error(display = "{}", std::u8::MAX)]
    E,
}

impl TestsNonFieldDisplayValues {
    fn some_method(&self) -> &'static str {
        "some_method"
    }
}

static STATIC_STR: &str = "STATIC_STR";

fn get_static_str() -> &'static str {
    STATIC_STR
}

#[test]
fn non_field_display() {
    let s = format!("{}", TestsNonFieldDisplayValues::A);
    assert_eq!(&s, "STATIC_STR");

    let s = format!("{}", TestsNonFieldDisplayValues::B);
    assert_eq!(&s, "STATIC_STR");

    let s = format!("{}", TestsNonFieldDisplayValues::C);
    assert_eq!(&s, "some_method");

    let s = format!("{}", TestsNonFieldDisplayValues::D);
    assert_eq!(&s, "255");

    let s = format!("{}", TestsNonFieldDisplayValues::E);
    assert_eq!(&s, "255");
}