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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2018 Pascal Bach
 * Copyright (c) 2021 Siemens Mobility GmbH
 *
 * SPDX-License-Identifier:     MIT
 */

use derive_getters::Getters;
use time::{Duration, OffsetDateTime};

/// A `TestSuite` groups together several [`TestCase`s](struct.TestCase.html).
#[derive(Debug, Clone, Getters)]
pub struct TestSuite {
    pub name: String,
    pub package: String,
    pub timestamp: OffsetDateTime,
    pub hostname: String,
    pub testcases: Vec<TestCase>,
    pub system_out: Option<String>,
    pub system_err: Option<String>,
}

impl TestSuite {
    /// Create a new `TestSuite` with a given name
    pub fn new(name: &str) -> Self {
        TestSuite {
            hostname: "localhost".into(),
            package: format!("testsuite/{}", &name),
            name: name.into(),
            timestamp: OffsetDateTime::now_utc(),
            testcases: Vec::new(),
            system_out: None,
            system_err: None,
        }
    }

    /// Add a [`TestCase`](struct.TestCase.html) to the `TestSuite`.
    pub fn add_testcase(&mut self, testcase: TestCase) {
        self.testcases.push(testcase);
    }

    /// Add several [`TestCase`s](struct.TestCase.html) from a Vec.
    pub fn add_testcases(&mut self, testcases: impl IntoIterator<Item = TestCase>) {
        self.testcases.extend(testcases);
    }

    /// Set the timestamp of the given `TestSuite`.
    ///
    /// By default the timestamp is set to the time when the `TestSuite` was created.
    pub fn set_timestamp(&mut self, timestamp: OffsetDateTime) {
        self.timestamp = timestamp;
    }

    pub fn set_system_out(&mut self, system_out: &str) {
        self.system_out = Some(system_out.to_owned());
    }

    pub fn set_system_err(&mut self, system_err: &str) {
        self.system_err = Some(system_err.to_owned());
    }

    pub fn tests(&self) -> usize {
        self.testcases.len()
    }

    pub fn errors(&self) -> usize {
        self.testcases.iter().filter(|x| x.is_error()).count()
    }

    pub fn failures(&self) -> usize {
        self.testcases.iter().filter(|x| x.is_failure()).count()
    }

    pub fn skipped(&self) -> usize {
        self.testcases.iter().filter(|x| x.is_skipped()).count()
    }

    pub fn time(&self) -> Duration {
        self.testcases
            .iter()
            .fold(Duration::ZERO, |sum, d| sum + d.time)
    }
}

///  Builder for [`TestSuite`](struct.TestSuite.html) objects.
#[derive(Debug, Clone, Getters)]
pub struct TestSuiteBuilder {
    pub testsuite: TestSuite,
}

impl TestSuiteBuilder {
    /// Create a new `TestSuiteBuilder` with a given name
    pub fn new(name: &str) -> Self {
        TestSuiteBuilder {
            testsuite: TestSuite::new(name),
        }
    }

    /// Add a [`TestCase`](struct.TestCase.html) to the `TestSuiteBuilder`.
    pub fn add_testcase(&mut self, testcase: TestCase) -> &mut Self {
        self.testsuite.testcases.push(testcase);
        self
    }

    /// Add several [`TestCase`s](struct.TestCase.html) from a Vec.
    pub fn add_testcases(&mut self, testcases: impl IntoIterator<Item = TestCase>) -> &mut Self {
        self.testsuite.testcases.extend(testcases);
        self
    }

    /// Set the timestamp of the `TestSuiteBuilder`.
    ///
    /// By default the timestamp is set to the time when the `TestSuiteBuilder` was created.
    pub fn set_timestamp(&mut self, timestamp: OffsetDateTime) -> &mut Self {
        self.testsuite.timestamp = timestamp;
        self
    }

    pub fn set_system_out(&mut self, system_out: &str) -> &mut Self {
        self.testsuite.system_out = Some(system_out.to_owned());
        self
    }

    pub fn set_system_err(&mut self, system_err: &str) -> &mut Self {
        self.testsuite.system_err = Some(system_err.to_owned());
        self
    }

    /// Build and return a [`TestSuite`](struct.TestSuite.html) object based on the data stored in this TestSuiteBuilder object.
    pub fn build(&self) -> TestSuite {
        self.testsuite.clone()
    }
}

/// One single test case
#[derive(Debug, Clone, Getters)]
pub struct TestCase {
    pub name: String,
    pub time: Duration,
    pub result: TestResult,
    pub classname: Option<String>,
    pub system_out: Option<String>,
    pub system_err: Option<String>,
}

/// Result of a test case
#[derive(Debug, Clone)]
pub enum TestResult {
    Success,
    Skipped,
    Error { type_: String, message: String },
    Failure { type_: String, message: String },
}

impl TestCase {
    /// Creates a new successful `TestCase`
    pub fn success(name: &str, time: Duration) -> Self {
        TestCase {
            name: name.into(),
            time,
            result: TestResult::Success,
            classname: None,
            system_out: None,
            system_err: None,
        }
    }

    /// Set the `classname` for the `TestCase`
    pub fn set_classname(&mut self, classname: &str) {
        self.classname = Some(classname.to_owned());
    }

    /// Set the `system_out` for the `TestCase`
    pub fn set_system_out(&mut self, system_out: &str) {
        self.system_out = Some(system_out.to_owned());
    }

    /// Set the `system_err` for the `TestCase`
    pub fn set_system_err(&mut self, system_err: &str) {
        self.system_err = Some(system_err.to_owned());
    }

    /// Check if a `TestCase` is successful
    pub fn is_success(&self) -> bool {
        matches!(self.result, TestResult::Success)
    }

    /// Creates a new erroneous `TestCase`
    ///
    /// An erroneous `TestCase` is one that encountered an unexpected error condition.
    pub fn error(name: &str, time: Duration, type_: &str, message: &str) -> Self {
        TestCase {
            name: name.into(),
            time,
            result: TestResult::Error {
                type_: type_.into(),
                message: message.into(),
            },
            classname: None,
            system_out: None,
            system_err: None,
        }
    }

    /// Check if a `TestCase` is erroneous
    pub fn is_error(&self) -> bool {
        matches!(self.result, TestResult::Error { .. })
    }

    /// Creates a new failed `TestCase`
    ///
    /// A failed `TestCase` is one where an explicit assertion failed
    pub fn failure(name: &str, time: Duration, type_: &str, message: &str) -> Self {
        TestCase {
            name: name.into(),
            time,
            result: TestResult::Failure {
                type_: type_.into(),
                message: message.into(),
            },
            classname: None,
            system_out: None,
            system_err: None,
        }
    }

    /// Check if a `TestCase` failed
    pub fn is_failure(&self) -> bool {
        matches!(self.result, TestResult::Failure { .. })
    }

    /// Create a new ignored `TestCase`
    ///
    /// An ignored `TestCase` is one where an ignored or skipped
    pub fn skipped(name: &str) -> Self {
        TestCase {
            name: name.into(),
            time: Duration::ZERO,
            result: TestResult::Skipped,
            classname: None,
            system_out: None,
            system_err: None,
        }
    }

    /// Check if a `TestCase` ignored
    pub fn is_skipped(&self) -> bool {
        matches!(self.result, TestResult::Skipped)
    }
}

///  Builder for [`TestCase`](struct.TestCase.html) objects.
#[derive(Debug, Clone, Getters)]
pub struct TestCaseBuilder {
    pub testcase: TestCase,
}

impl TestCaseBuilder {
    /// Creates a new TestCaseBuilder for a successful `TestCase`
    pub fn success(name: &str, time: Duration) -> Self {
        TestCaseBuilder {
            testcase: TestCase::success(name, time),
        }
    }

    /// Set the `classname` for the `TestCase`
    pub fn set_classname(&mut self, classname: &str) -> &mut Self {
        self.testcase.classname = Some(classname.to_owned());
        self
    }

    /// Set the `system_out` for the `TestCase`
    pub fn set_system_out(&mut self, system_out: &str) -> &mut Self {
        self.testcase.system_out = Some(system_out.to_owned());
        self
    }

    /// Set the `system_err` for the `TestCase`
    pub fn set_system_err(&mut self, system_err: &str) -> &mut Self {
        self.testcase.system_err = Some(system_err.to_owned());
        self
    }

    /// Creates a new TestCaseBuilder for an erroneous `TestCase`
    ///
    /// An erroneous `TestCase` is one that encountered an unexpected error condition.
    pub fn error(name: &str, time: Duration, type_: &str, message: &str) -> Self {
        TestCaseBuilder {
            testcase: TestCase::error(name, time, type_, message),
        }
    }

    /// Creates a new TestCaseBuilder for a failed `TestCase`
    ///
    /// A failed `TestCase` is one where an explicit assertion failed
    pub fn failure(name: &str, time: Duration, type_: &str, message: &str) -> Self {
        TestCaseBuilder {
            testcase: TestCase::failure(name, time, type_, message),
        }
    }

    /// Creates a new TestCaseBuilder for an ignored `TestCase`
    ///
    /// An ignored `TestCase` is one where an ignored or skipped
    pub fn skipped(name: &str) -> Self {
        TestCaseBuilder {
            testcase: TestCase::skipped(name),
        }
    }

    /// Build and return a [`TestCase`](struct.TestCase.html) object based on the data stored in this TestCaseBuilder object.
    pub fn build(&self) -> TestCase {
        self.testcase.clone()
    }
}

// Make sure the readme is tested too
#[cfg(doctest)]
doc_comment::doctest!("../README.md");