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}
135
136mod private {
137    pub trait Sealed {}
138
139    impl<T, E> Sealed for Result<T, E> {}
140}
141
142/// Extension trait for `Result<T, E>` to provide additional functionality
143/// specifically for prop test cases.
144pub trait ProptestResultExt<T, E>: private::Sealed {
145    /// Converts a `Result<T, E>` into a `Result<T, TestCaseError>`, where the
146    /// `Err` case is transformed into a `TestCaseError::Reject`.
147    ///
148    /// This is intended to be used like the [`prop_assume!`] macro, but for
149    /// fallible computations. If the result is `Err`, the test input is rejected
150    /// and a new input will be generated.
151    ///
152    /// ## Example
153    ///
154    /// ```
155    /// use proptest::prelude::*;
156    ///
157    /// fn test_conversion(a: i32) -> Result<(), TestCaseError> {
158    ///     // Reject the case if `a` cannot be converted to u8 (e.g., negative values)
159    ///     let _unsigned: u8 = a.try_into().prop_assume_ok()?;
160    ///     // ...rest of test...
161    ///     Ok(())
162    /// }
163    ///
164    /// proptest! {
165    ///   #[test]
166    ///   fn test_that_only_works_with_positive_integers(a in -10i32..10i32) {
167    ///     test_conversion(a)?;
168    ///   }
169    /// }
170    /// ```
171    ///
172    /// [`prop_assume!`]: crate::prop_assume
173    fn prop_assume_ok(self) -> Result<T, TestCaseError>
174    where
175        E: fmt::Debug;
176}
177
178impl<T, E> ProptestResultExt<T, E> for Result<T, E> {
179    #[track_caller]
180    fn prop_assume_ok(self) -> Result<T, TestCaseError>
181    where
182        E: fmt::Debug,
183    {
184        let location = core::panic::Location::caller();
185        self.map_err(|err| {
186            TestCaseError::reject(format!("{location}: {err:?}"))
187        })
188    }
189}