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
use crate::call_pattern::InputIndex;
use crate::debug::{self, filter_questionmark};
use crate::mismatch::{Mismatch, MismatchKind};
use crate::output::Output;
use crate::{call_pattern::MatchingFn, call_pattern::MatchingFnDebug, *};

/// The evaluation of a [MockFn].
///
/// Used to tell trait implementations whether to do perform their own evaluation of a call.
///
/// The output is generic, because both owned and referenced output are supported.
pub enum Evaluation<'u, 'i, F: MockFn> {
    /// Function evaluated to its output.
    Evaluated(<F::Output<'u> as Output<'u, F::Response>>::Type),
    /// Function not yet evaluated.
    Skipped(F::Inputs<'i>),
}

impl<'u, 'i, F: MockFn> Evaluation<'u, 'i, F> {
    /// Unwrap the `Evaluated` variant, or panic.
    /// The unimock instance must be passed in order to register that an eventual panic happened.
    pub fn unwrap(self, unimock: &Unimock) -> <F::Output<'u> as Output<'u, F::Response>>::Type {
        match self {
            Self::Evaluated(output) => output,
            Self::Skipped(_) => panic!(
                "{}",
                unimock
                    .shared_state
                    .prepare_panic(error::MockError::CannotUnmock { name: F::NAME })
            ),
        }
    }
}

/// A builder for argument matchers.
pub struct Matching<F: MockFn> {
    pub(crate) mock_fn: std::marker::PhantomData<F>,
    pub(crate) matching_fn: Option<MatchingFn<F>>,
    pub(crate) matching_fn_debug: Option<MatchingFnDebug<F>>,
    pub(crate) matcher_debug: Option<debug::InputMatcherDebug>,
}

impl<F> Matching<F>
where
    F: MockFn,
{
    pub(crate) fn new() -> Self {
        Self {
            mock_fn: std::marker::PhantomData,
            matching_fn: None,
            matching_fn_debug: None,
            matcher_debug: None,
        }
    }

    /// Set the matching function, with debug capabilities.
    ///
    /// The function should accept a reference to inputs as argument, and return a boolean answer representing match or no match.
    ///
    /// The function also receives a [MismatchReporter]
    #[inline]
    pub fn debug_func<M>(&mut self, matching_fn: M)
    where
        M: (for<'i> Fn(&F::Inputs<'i>, &mut MismatchReporter) -> bool) + Send + Sync + 'static,
    {
        self.matching_fn_debug = Some(MatchingFnDebug(Box::new(matching_fn)));
    }

    /// Set the matching function.
    ///
    /// The function should accept a reference to inputs as argument, and return a boolean answer representing match or no match.
    #[inline]
    pub fn func<M>(&mut self, matching_fn: M)
    where
        M: (for<'i> Fn(&F::Inputs<'i>) -> bool) + Send + Sync + 'static,
    {
        self.matching_fn = Some(MatchingFn(Box::new(matching_fn)));
    }

    /// Register debug info on the matching builder.
    ///
    /// This way, a mismatch may be easier to debug, as the debug info can be printed as part of panic messages.
    pub fn pat_debug(&mut self, pat_debug: &'static str, file: &'static str, line: u32) {
        self.matcher_debug = Some(debug::InputMatcherDebug {
            pat_debug,
            file,
            line,
        });
    }
}

/// A reporter used in call pattern matchers in case of mismatched inputs.
///
/// This is a diagnostics tool leading to higher quality error messages.
///
/// Used by the [matching] macro.
pub struct MismatchReporter {
    enabled: bool,
    pub(crate) mismatches: Vec<(InputIndex, Mismatch)>,
}

impl MismatchReporter {
    pub(crate) fn new_enabled() -> Self {
        Self {
            enabled: true,
            mismatches: vec![],
        }
    }

    pub(crate) fn new_disabled() -> Self {
        Self {
            enabled: false,
            mismatches: vec![],
        }
    }

    /// Whether debugging is enabled
    pub fn enabled(&self) -> bool {
        self.enabled
    }

    /// Register failure to match a pattern
    #[deprecated]
    pub fn pat_fail(
        &mut self,
        input_index: usize,
        actual: impl Into<String>,
        expected: impl Into<String>,
    ) {
        self.pat_fail_opt_debug(input_index, filter_questionmark(actual.into()), expected);
    }

    /// Register failure to match a pattern
    pub fn pat_fail_opt_debug(
        &mut self,
        input_index: usize,
        actual: Option<impl Into<String>>,
        expected: impl Into<String>,
    ) {
        self.mismatches.push((
            InputIndex(input_index),
            Mismatch {
                kind: MismatchKind::Pattern,
                actual: actual.map(|dbg| dbg.into()),
                expected: expected.into(),
            },
        ));
    }

    /// Register failure for an eq check
    #[deprecated]
    pub fn eq_fail(
        &mut self,
        input_index: usize,
        actual: impl Into<String>,
        expected: impl Into<String>,
    ) {
        self.eq_fail_opt_debug(input_index, filter_questionmark(actual.into()), expected);
    }

    /// Register failure to match a pattern
    pub fn eq_fail_opt_debug(
        &mut self,
        input_index: usize,
        actual: Option<impl Into<String>>,
        expected: impl Into<String>,
    ) {
        self.mismatches.push((
            InputIndex(input_index),
            Mismatch {
                kind: MismatchKind::Eq,
                actual: actual.map(|dbg| dbg.into()),
                expected: expected.into(),
            },
        ));
    }

    /// Register failure for an ne check
    #[deprecated]
    pub fn ne_fail(
        &mut self,
        input_index: usize,
        actual: impl Into<String>,
        expected: impl Into<String>,
    ) {
        self.ne_fail_opt_debug(input_index, filter_questionmark(actual.into()), expected);
    }

    /// Register failure for an ne check
    pub fn ne_fail_opt_debug(
        &mut self,
        input_index: usize,
        actual: Option<impl Into<String>>,
        expected: impl Into<String>,
    ) {
        self.mismatches.push((
            InputIndex(input_index),
            Mismatch {
                kind: MismatchKind::Ne,
                actual: actual.map(|dbg| dbg.into()),
                expected: expected.into(),
            },
        ));
    }
}

/// Evaluate a [MockFn] given some inputs, to produce its output.
#[track_caller]
pub fn eval<'u, 'i, F>(unimock: &'u Unimock, inputs: F::Inputs<'i>) -> Evaluation<'u, 'i, F>
where
    F: MockFn + 'static,
{
    unimock.handle_error(eval::eval(&unimock.shared_state, inputs))
}

/// Trait for computing the proper [std::fmt::Debug] representation of a value.
pub trait ProperDebug {
    /// Format a debug representation.
    fn unimock_try_debug(&self) -> String;

    /// Optionally format a debug representation.
    fn unimock_try_debug_opt(&self) -> Option<String>;
}

/// Fallback trait (using autoref specialization) for returning `"?"` when the implementing value does not implement [std::fmt::Debug].
pub trait NoDebug {
    /// Format a debug representation.
    fn unimock_try_debug(&self) -> String;

    /// Optionally format a debug representation.
    fn unimock_try_debug_opt(&self) -> Option<String>;
}

// Autoref specialization:
// https://github.com/dtolnay/case-studies/blob/master/autoref-specialization/README.md

impl<T: std::fmt::Debug> ProperDebug for T {
    fn unimock_try_debug(&self) -> String {
        format!("{self:?}")
    }

    fn unimock_try_debug_opt(&self) -> Option<String> {
        Some(format!("{self:?}"))
    }
}

impl<T> NoDebug for &T {
    fn unimock_try_debug(&self) -> String {
        "?".to_string()
    }

    fn unimock_try_debug_opt(&self) -> Option<String> {
        None
    }
}

/// Take a vector of strings, comma separate and put within parentheses.
pub fn format_inputs(inputs: &[String]) -> String {
    let joined = inputs.join(", ");
    format!("({joined})")
}

/// Convert any type implementing `AsRef<str>` to a `&str`.
pub fn as_str_ref<T>(input: &T) -> &str
where
    T: AsRef<str>,
{
    input.as_ref()
}

/// Convert any type implementing `AsRef<[I]>` to a `&[I]`.
pub fn as_slice<T, I>(input: &T) -> &[I]
where
    T: AsRef<[I]>,
{
    input.as_ref()
}