proptest/test_runner/
errors.rs

1//-
2// Copyright 2017, 2018 The proptest developers
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use crate::std_facade::fmt;
11
12#[cfg(feature = "std")]
13use std::string::ToString;
14
15use crate::test_runner::Reason;
16
17/// Errors which can be returned from test cases to indicate non-successful
18/// completion.
19///
20/// Note that in spite of the name, `TestCaseError` is currently *not* an
21/// instance of `Error`, since otherwise `impl<E : Error> From<E>` could not be
22/// provided.
23///
24/// Any `Error` can be converted to a `TestCaseError`, which places
25/// `Error::display()` into the `Fail` case.
26#[derive(Debug, Clone)]
27pub enum TestCaseError {
28    /// The input was not valid for the test case. This does not count as a
29    /// test failure (nor a success); rather, it simply signals to generate
30    /// a new input and try again.
31    Reject(Reason),
32    /// The code under test failed the test.
33    Fail(Reason),
34}
35
36/// Convenience for the type returned by test cases.
37pub type TestCaseResult = Result<(), TestCaseError>;
38
39impl TestCaseError {
40    /// Rejects the generated test input as invalid for this test case. This
41    /// does not count as a test failure (nor a success); rather, it simply
42    /// signals to generate a new input and try again.
43    ///
44    /// The string gives the location and context of the rejection, and
45    /// should be suitable for formatting like `Foo did X at {whence}`.
46    pub fn reject(reason: impl Into<Reason>) -> Self {
47        TestCaseError::Reject(reason.into())
48    }
49
50    /// The code under test failed the test.
51    ///
52    /// The string should indicate the location of the failure, but may
53    /// generally be any string.
54    pub fn fail(reason: impl Into<Reason>) -> Self {
55        TestCaseError::Fail(reason.into())
56    }
57}
58
59impl fmt::Display for TestCaseError {
60    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61        match *self {
62            TestCaseError::Reject(ref whence) => {
63                write!(f, "Input rejected at {}", whence)
64            }
65            TestCaseError::Fail(ref why) => write!(f, "Case failed: {}", why),
66        }
67    }
68}
69
70#[cfg(feature = "std")]
71impl<E: ::std::error::Error> From<E> for TestCaseError {
72    fn from(cause: E) -> Self {
73        TestCaseError::fail(cause.to_string())
74    }
75}
76
77/// A failure state from running test cases for a single test.
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub enum TestError<T> {
80    /// The test was aborted for the given reason, for example, due to too many
81    /// inputs having been rejected.
82    Abort(Reason),
83    /// A failing test case was found. The string indicates where and/or why
84    /// the test failed. The `T` is the minimal input found to reproduce the
85    /// failure.
86    Fail(Reason, T),
87}
88
89impl<T: fmt::Debug> fmt::Display for TestError<T> {
90    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91        match *self {
92            TestError::Abort(ref why) => write!(f, "Test aborted: {}", why),
93            TestError::Fail(ref why, ref what) => write!(
94                f,
95                "Test failed: {}; minimal failing input: {:?}",
96                why, what
97            ),
98        }
99    }
100}
101
102#[cfg(feature = "std")]
103#[allow(deprecated)] // description()
104impl<T: fmt::Debug> ::std::error::Error for TestError<T> {
105    fn description(&self) -> &str {
106        match *self {
107            TestError::Abort(..) => "Abort",
108            TestError::Fail(..) => "Fail",
109        }
110    }
111}