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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Core assertion types and utilities.
use std::{marker::PhantomData, sync::Arc};

use crate::{
    matcher::SpanMatcher,
    state::{EntryState, State},
};

enum AssertionCriterion {
    WasCreated,
    WasEntered,
    WasExited,
    WasClosed,
    CreatedTimes(usize),
    EnteredTimes(usize),
    ExitedTimes(usize),
    ClosedTimes(usize),
}

impl AssertionCriterion {
    pub fn assert(&self, state: &Arc<EntryState>) {
        match self {
            AssertionCriterion::WasCreated => assert!(state.num_created() != 0),
            AssertionCriterion::WasEntered => assert!(state.num_entered() != 0),
            AssertionCriterion::WasExited => assert!(state.num_exited() != 0),
            AssertionCriterion::WasClosed => assert!(state.num_closed() != 0),
            AssertionCriterion::CreatedTimes(times) => assert_eq!(state.num_created(), *times),
            AssertionCriterion::EnteredTimes(times) => assert_eq!(state.num_entered(), *times),
            AssertionCriterion::ExitedTimes(times) => assert_eq!(state.num_exited(), *times),
            AssertionCriterion::ClosedTimes(times) => assert_eq!(state.num_closed(), *times),
        }
    }

    pub fn try_assert(&self, state: &Arc<EntryState>) -> bool {
        match self {
            AssertionCriterion::WasCreated => state.num_created() != 0,
            AssertionCriterion::WasEntered => state.num_entered() != 0,
            AssertionCriterion::WasExited => state.num_exited() != 0,
            AssertionCriterion::WasClosed => state.num_closed() != 0,
            AssertionCriterion::CreatedTimes(times) => state.num_created() == *times,
            AssertionCriterion::EnteredTimes(times) => state.num_entered() == *times,
            AssertionCriterion::ExitedTimes(times) => state.num_exited() == *times,
            AssertionCriterion::ClosedTimes(times) => state.num_closed() == *times,
        }
    }
}

/// A specific set of criteria to enforce on matching spans.
///
/// Assertions represent both a span "matcher" -- which controls which spans the criteria are
/// applied to -- and the criteria themselves, which define the behavior to assert against the
/// matching spans.
///
/// ## Matching behavior
///
/// As an `Assertion` can match multiple spans, care must be taken when building the `Assertion` to
/// constrain the matcher correctly.  For example, if you want to focus on a specific span, you
/// would want to use match on the span name at a minimum, and potentially match on the span target
/// if there were other spans with the same name in different modules.  However, if you simply
/// wanted to check if any spans under a specific module path were created -- perhaps to assert that
/// a particular codeflow is being exercised, but not assert _how_ it's being exercised -- then only
/// specifying the span target might suffice.
pub struct Assertion {
    state: Arc<EntryState>,
    criteria: Vec<AssertionCriterion>,
}

impl Assertion {
    /// Asserts that all criteria have been met.
    ///
    /// Uses the "assert" macros from the standard library, so criterion which have not been met
    /// will cause a panic, similar to using the "assert" macros directly.
    ///
    /// For a fallible assertion that can be called over and over without panicking, [`try_assert`]
    /// can be used instead.
    pub fn assert(&self) {
        for criterion in &self.criteria {
            criterion.assert(&self.state);
        }
    }

    /// Attempts to assert that all criteria have been met.
    ///
    /// If any of the criteria have not yet been met, `false` will be returned.  Otherwise, `true`
    /// will be returned.
    ///
    /// If assertions should end your test immediately, [`assert`] can be used instead.
    pub fn try_assert(&self) -> bool {
        for criterion in &self.criteria {
            if !criterion.try_assert(&self.state) {
                return false;
            }
        }

        true
    }
}

/// An [`AssertionBuilder`] which does not yet have a span matcher.
///
/// A matcher consists of either a span name, or the target of a span itself, or potentially both.
/// A span target refers to the `tracing` parlance, where "target" refers to the module path that a
/// span is defined in.
///
/// Additionally, a span matcher can include specific fields that must be present on a span in order
/// to match.
pub struct NoMatcher {
    _p: PhantomData<()>,
}

/// An [`AssertionBuilder`] which has a valid span matcher but does not yet have any assertion
/// criteria.
///
/// Assertion criteria are the actual behavioral matchers, such as "this span must have been entered
/// at least once" or "this span must have been created at least N times".
pub struct NoCriteria {
    _p: PhantomData<()>,
}

/// An [`AssertionBuilder`] which has a valid span matcher and at least one assertion criterion.
pub struct Constrained {
    _p: PhantomData<()>,
}

/// Configures and constructs an [`Assertion`].
///
/// This builder uses a state pattern to ensure that the necessary fields are configured before a
/// valid `Assertion` can be constructed.  You may notice that some methods are only available once
/// other methods have been called.
///
/// You must first define a span matcher, which defines how this assertion is matched to a given
/// span, and then you must specify the assertion criteria itself, which defines the behavior of the
/// span to assert for.
///
/// Once these are defined, an `Assertion` can be constructed by calling [`finalize`].
pub struct AssertionBuilder<S> {
    state: Arc<State>,
    matcher: Option<SpanMatcher>,
    criteria: Vec<AssertionCriterion>,
    _builder_state: PhantomData<fn(S)>,
}

impl AssertionBuilder<NoMatcher> {
    /// Sets the name of the span to match.
    ///
    /// All span matchers, which includes [`with_name`], [`with_target`], and [`with_span_field`],
    /// are additive, which means a span must match all of them to match the assertion overall.
    pub fn with_name<S>(mut self, name: S) -> AssertionBuilder<NoCriteria>
    where
        S: Into<String>,
    {
        let matcher = self.matcher.get_or_insert_with(SpanMatcher::default);
        matcher.set_name(name.into());

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Sets the target of the span to match.
    ///
    /// All span matchers, which includes [`with_name`], [`with_target`], and [`with_span_field`],
    /// are additive, which means a span must match all of them to match the assertion overall.
    pub fn with_target<S>(mut self, target: S) -> AssertionBuilder<NoCriteria>
    where
        S: Into<String>,
    {
        let matcher = self.matcher.get_or_insert_with(SpanMatcher::default);
        matcher.set_target(target.into());

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }
}

impl AssertionBuilder<NoCriteria> {
    /// Sets the name of the span to match.
    ///
    /// All span matchers, which includes [`with_name`], [`with_target`], and [`with_span_field`],
    /// are additive, which means a span must match all of them to match the assertion overall.
    pub fn with_name<S>(mut self, name: S) -> AssertionBuilder<NoCriteria>
    where
        S: Into<String>,
    {
        let matcher = self.matcher.get_or_insert_with(SpanMatcher::default);
        matcher.set_name(name.into());

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Sets the target of the span to match.
    ///
    /// All span matchers, which includes [`with_name`], [`with_target`], and [`with_span_field`],
    /// are additive, which means a span must match all of them to match the assertion overall.
    pub fn with_target<S>(mut self, target: S) -> AssertionBuilder<NoCriteria>
    where
        S: Into<String>,
    {
        let matcher = self.matcher.get_or_insert_with(SpanMatcher::default);
        matcher.set_target(target.into());

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Adds a field which the span must contain to match.
    ///
    /// The field is matched by name.
    ///
    /// All span matchers, which includes [`with_name`], [`with_target`], and [`with_span_field`],
    /// are additive, which means a span must match all of them to match the assertion overall.
    pub fn with_span_field<S>(mut self, field: S) -> AssertionBuilder<NoCriteria>
    where
        S: Into<String>,
    {
        if let Some(matcher) = self.matcher.as_mut() {
            matcher.add_field_exists(field.into());
        }

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was created at least once.
    pub fn was_created(mut self) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::WasCreated);

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was entered at least once.
    pub fn was_entered(mut self) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::WasEntered);

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was exited at least once.
    pub fn was_exited(mut self) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::WasExited);

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was closed at least once.
    pub fn was_closed(mut self) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::WasClosed);

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was created at least `n` times.
    pub fn was_created_many(mut self, n: usize) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::CreatedTimes(n));

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was entered at least `n` times.
    pub fn was_entered_many(mut self, n: usize) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::EnteredTimes(n));

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was exited at least `n` times.
    pub fn was_exited_many(mut self, n: usize) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::ExitedTimes(n));

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }

    /// Asserts that a matching span was closed at least `n` times.
    pub fn was_closed_many(mut self, n: usize) -> AssertionBuilder<Constrained> {
        self.criteria.push(AssertionCriterion::ClosedTimes(n));

        AssertionBuilder {
            state: self.state,
            matcher: self.matcher,
            criteria: self.criteria,
            _builder_state: PhantomData,
        }
    }
}

impl AssertionBuilder<Constrained> {
    /// Asserts that a matching span was created at least once.
    pub fn was_created(mut self) -> Self {
        self.criteria.push(AssertionCriterion::WasCreated);
        self
    }

    /// Asserts that a matching span was entered at least once.
    pub fn was_entered(mut self) -> Self {
        self.criteria.push(AssertionCriterion::WasEntered);
        self
    }

    /// Asserts that a matching span was exited at least once.
    pub fn was_exited(mut self) -> Self {
        self.criteria.push(AssertionCriterion::WasExited);
        self
    }

    /// Asserts that a matching span was closed at least once.
    pub fn was_closed(mut self) -> Self {
        self.criteria.push(AssertionCriterion::WasClosed);
        self
    }

    /// Asserts that a matching span was created at least `n` times.
    pub fn was_created_many(mut self, n: usize) -> Self {
        self.criteria.push(AssertionCriterion::CreatedTimes(n));
        self
    }

    /// Asserts that a matching span was entered at least `n` times.
    pub fn was_entered_many(mut self, n: usize) -> Self {
        self.criteria.push(AssertionCriterion::EnteredTimes(n));
        self
    }

    /// Asserts that a matching span was exited at least `n` times.
    pub fn was_exited_many(mut self, n: usize) -> Self {
        self.criteria.push(AssertionCriterion::ExitedTimes(n));
        self
    }

    /// Asserts that a matching span was closed at least `n` times.
    pub fn was_closed_many(mut self, n: usize) -> Self {
        self.criteria.push(AssertionCriterion::ClosedTimes(n));
        self
    }

    /// Creates the finalized `Assertion`.
    ///
    /// Once finalized, the assertion is live and its state will be updated going forward.
    pub fn finalize(mut self) -> Assertion {
        let matcher = self
            .matcher
            .take()
            .expect("matcher must be present at this point");
        let state = self.state.create_entry(matcher);
        Assertion {
            state,
            criteria: self.criteria,
        }
    }
}

/// Creates and stores all constructed [`Assertion`]s.
#[derive(Default)]
pub struct AssertionRegistry {
    state: Arc<State>,
}

impl AssertionRegistry {
    pub(crate) fn state(&self) -> &Arc<State> {
        &self.state
    }

    /// Creates an [`AssertionBuilder`] for constructing a new [`Assertion`].
    pub fn build(&self) -> AssertionBuilder<NoMatcher> {
        AssertionBuilder {
            state: Arc::clone(&self.state),
            matcher: None,
            criteria: Vec::new(),
            _builder_state: PhantomData,
        }
    }
}