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/// Indicates the type of test that ran successfully.
37///
38/// This is used for managing whether or not a success is counted against
39/// configured `PROPTEST_CASES`; only `NewCases` shall be counted.
40///
41/// TODO-v2: Ideally `TestCaseResult = Result<TestCaseOk, TestCaseError>`
42/// however this breaks source compatibility in version 1.x.x because
43/// `TestCaseResult` is public.
44#[derive(Debug, Clone)]
45pub(crate) enum TestCaseOk {
46    NewCaseSuccess,
47    PersistedCaseSuccess,
48    ReplayFromForkSuccess,
49    CacheHitSuccess,
50    Reject,
51}
52
53/// Convenience for the type returned by test cases.
54pub type TestCaseResult = Result<(), TestCaseError>;
55
56/// Intended to replace `TestCaseResult` in v2.
57///
58/// TODO-v2: Ideally `TestCaseResult = Result<TestCaseOk, TestCaseError>`
59/// however this breaks source compatibility in version 1.x.x because
60/// `TestCaseResult` is public.
61pub(crate) type TestCaseResultV2 = Result<TestCaseOk, TestCaseError>;
62
63impl TestCaseError {
64    /// Rejects the generated test input as invalid for this test case. This
65    /// does not count as a test failure (nor a success); rather, it simply
66    /// signals to generate a new input and try again.
67    ///
68    /// The string gives the location and context of the rejection, and
69    /// should be suitable for formatting like `Foo did X at {whence}`.
70    pub fn reject(reason: impl Into<Reason>) -> Self {
71        TestCaseError::Reject(reason.into())
72    }
73
74    /// The code under test failed the test.
75    ///
76    /// The string should indicate the location of the failure, but may
77    /// generally be any string.
78    pub fn fail(reason: impl Into<Reason>) -> Self {
79        TestCaseError::Fail(reason.into())
80    }
81}
82
83impl fmt::Display for TestCaseError {
84    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85        match *self {
86            TestCaseError::Reject(ref whence) => {
87                write!(f, "Input rejected at {}", whence)
88            }
89            TestCaseError::Fail(ref why) => write!(f, "Case failed: {}", why),
90        }
91    }
92}
93
94#[cfg(feature = "std")]
95impl<E: ::std::error::Error> From<E> for TestCaseError {
96    fn from(cause: E) -> Self {
97        TestCaseError::fail(cause.to_string())
98    }
99}
100
101/// A failure state from running test cases for a single test.
102#[derive(Debug, Clone, PartialEq, Eq)]
103pub enum TestError<T> {
104    /// The test was aborted for the given reason, for example, due to too many
105    /// inputs having been rejected.
106    Abort(Reason),
107    /// A failing test case was found. The string indicates where and/or why
108    /// the test failed. The `T` is the minimal input found to reproduce the
109    /// failure.
110    Fail(Reason, T),
111}
112
113impl<T: fmt::Debug> fmt::Display for TestError<T> {
114    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115        match *self {
116            TestError::Abort(ref why) => write!(f, "Test aborted: {}", why),
117            TestError::Fail(ref why, ref what) => {
118                writeln!(f, "Test failed: {}.", why)?;
119                write!(f, "minimal failing input: {:#?}", what)
120            }
121        }
122    }
123}
124
125#[cfg(feature = "std")]
126#[allow(deprecated)] // description()
127impl<T: fmt::Debug> ::std::error::Error for TestError<T> {
128    fn description(&self) -> &str {
129        match *self {
130            TestError::Abort(..) => "Abort",
131            TestError::Fail(..) => "Fail",
132        }
133    }
134}