Skip to content

Commit 33e48c4

Browse files
committed
mock: complete API documentation including expect module (#2494)
There has been interest around publishing tracing-mock to crates.io for some time. In order to make this possible, documentation and some code clean up is needed. The `expect` module, which contains constructor functions for many of the other `tracing-mock` modules needs documentation and examples. This change adds documentation to the `expect` module and all the public APIs within it. This includes doctests on all the methods which serve as examples. The lint for `missing_docs` has been enabled for the entire `tracing-mock` crate! This has been done together with all the other lints that are enabled on the other crates in this project. The `event::msg("message")` constructor was removed, in favor of requiring an explicit construction via `expect::event().with_fields(expect::msg("message"))`. This is appropriate to reduce the API surface that would need to be supported in the future and also because the `event::msg` constructor could be overridden by a subsequent usage of `with_fields`. The shorthand `expect::message()` was renamed to `expect::msg` to make this change less burdensome. The `span::named("name")` constructor was removed, in favor of requiring an explicit construction via `expect::span.with_name("name")`. The latter isn't much longer and since #3097, a string with the name can be passed directly everywhere that an `ExpectedSpan` is required. This change also sets the `missing_docs` lint to warn for the entire `tracing-mock` crate, making it ready to publish (once backported). Refs: #539
1 parent de4ecd5 commit 33e48c4

File tree

17 files changed

+480
-222
lines changed

17 files changed

+480
-222
lines changed

tracing-mock/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# tracing-mock
66

7-
Utilities for testing [`tracing`][tracing] and crates that uses it.
7+
Utilities for testing [`tracing`] and crates that uses it.
88

99
[![Documentation (master)][docs-master-badge]][docs-master-url]
1010
[![MIT licensed][mit-badge]][mit-url]
@@ -71,14 +71,14 @@ Below is an example that checks that an event contains a message:
7171

7272
```rust
7373
use tracing::subscriber::with_default;
74-
use tracing_mock::{subscriber, expect, field};
74+
use tracing_mock::{expect, subscriber};
7575

7676
fn yak_shaving() {
7777
tracing::info!("preparing to shave yaks");
7878
}
7979

8080
let (subscriber, handle) = subscriber::mock()
81-
.event(expect::event().with_fields(expect::message("preparing to shave yaks")))
81+
.event(expect::event().with_fields(expect::msg("preparing to shave yaks")))
8282
.only()
8383
.run_with_handle();
8484

@@ -102,7 +102,7 @@ Below is a slightly more complex example. `tracing-mock` asserts that, in order:
102102

103103
```rust
104104
use tracing::subscriber::with_default;
105-
use tracing_mock::{subscriber, expect};
105+
use tracing_mock::{expect, subscriber};
106106

107107
#[tracing::instrument]
108108
fn yak_shaving(number_of_yaks: u32) {
@@ -128,15 +128,15 @@ let (subscriber, handle) = subscriber::mock()
128128
expect::event().with_fields(
129129
expect::field("number_of_yaks")
130130
.with_value(&yak_count)
131-
.and(expect::message("preparing to shave yaks"))
131+
.and(expect::msg("preparing to shave yaks"))
132132
.only(),
133133
),
134134
)
135135
.event(
136136
expect::event().with_fields(
137137
expect::field("all_yaks_shaved")
138138
.with_value(&true)
139-
.and(expect::message("yak shaving completed."))
139+
.and(expect::msg("yak shaving completed."))
140140
.only(),
141141
),
142142
)

tracing-mock/src/event.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@
2828
//!
2929
//! [`subscriber`]: mod@crate::subscriber
3030
//! [`expect::event`]: fn@crate::expect::event
31-
#![allow(missing_docs)]
31+
use std::fmt;
32+
3233
use crate::{
3334
ancestry::{ActualAncestry, ExpectedAncestry},
34-
expect, field,
35+
field,
3536
metadata::ExpectedMetadata,
3637
span,
3738
};
3839

39-
use std::fmt;
40-
4140
/// An expected event.
4241
///
4342
/// For a detailed description and examples, see the documentation for
@@ -52,10 +51,6 @@ pub struct ExpectedEvent {
5251
pub(super) metadata: ExpectedMetadata,
5352
}
5453

55-
pub fn msg(message: impl fmt::Display) -> ExpectedEvent {
56-
expect::event().with_fields(expect::message(message))
57-
}
58-
5954
impl ExpectedEvent {
6055
/// Sets a name to expect when matching an event.
6156
///
@@ -100,7 +95,7 @@ impl ExpectedEvent {
10095
///
10196
/// ```
10297
/// use tracing::subscriber::with_default;
103-
/// use tracing_mock::{subscriber, expect};
98+
/// use tracing_mock::{expect, subscriber};
10499
///
105100
/// let event = expect::event()
106101
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
@@ -120,7 +115,7 @@ impl ExpectedEvent {
120115
///
121116
/// ```should_panic
122117
/// use tracing::subscriber::with_default;
123-
/// use tracing_mock::{subscriber, expect};
118+
/// use tracing_mock::{expect, subscriber};
124119
///
125120
/// let event = expect::event()
126121
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
@@ -156,7 +151,7 @@ impl ExpectedEvent {
156151
///
157152
/// ```
158153
/// use tracing::subscriber::with_default;
159-
/// use tracing_mock::{subscriber, expect};
154+
/// use tracing_mock::{expect, subscriber};
160155
///
161156
/// let event = expect::event()
162157
/// .at_level(tracing::Level::WARN);
@@ -177,7 +172,7 @@ impl ExpectedEvent {
177172
///
178173
/// ```should_panic
179174
/// use tracing::subscriber::with_default;
180-
/// use tracing_mock::{subscriber, expect};
175+
/// use tracing_mock::{expect, subscriber};
181176
///
182177
/// let event = expect::event()
183178
/// .at_level(tracing::Level::INFO);
@@ -210,7 +205,7 @@ impl ExpectedEvent {
210205
///
211206
/// ```
212207
/// use tracing::subscriber::with_default;
213-
/// use tracing_mock::{subscriber, expect};
208+
/// use tracing_mock::{expect, subscriber};
214209
///
215210
/// let event = expect::event()
216211
/// .with_target("some_target");
@@ -230,7 +225,7 @@ impl ExpectedEvent {
230225
///
231226
/// ```should_panic
232227
/// use tracing::subscriber::with_default;
233-
/// use tracing_mock::{subscriber, expect};
228+
/// use tracing_mock::{expect, subscriber};
234229
///
235230
/// let event = expect::event()
236231
/// .with_target("some_target");
@@ -277,7 +272,7 @@ impl ExpectedEvent {
277272
///
278273
/// ```
279274
/// use tracing::subscriber::with_default;
280-
/// use tracing_mock::{subscriber, expect};
275+
/// use tracing_mock::{expect, subscriber};
281276
///
282277
/// let parent = expect::span()
283278
/// .named("parent_span")
@@ -304,7 +299,7 @@ impl ExpectedEvent {
304299
///
305300
/// ```
306301
/// use tracing::subscriber::with_default;
307-
/// use tracing_mock::{subscriber, expect};
302+
/// use tracing_mock::{expect, subscriber};
308303
///
309304
/// let event = expect::event()
310305
/// .with_ancestry(expect::has_explicit_parent("parent_span"));
@@ -326,7 +321,7 @@ impl ExpectedEvent {
326321
///
327322
/// ```
328323
/// use tracing::subscriber::with_default;
329-
/// use tracing_mock::{subscriber, expect};
324+
/// use tracing_mock::{expect, subscriber};
330325
///
331326
/// let event = expect::event()
332327
/// .with_ancestry(expect::is_explicit_root());
@@ -350,7 +345,7 @@ impl ExpectedEvent {
350345
///
351346
/// ```
352347
/// use tracing::subscriber::with_default;
353-
/// use tracing_mock::{subscriber, expect};
348+
/// use tracing_mock::{expect, subscriber};
354349
///
355350
/// let event = expect::event()
356351
/// .with_ancestry(expect::has_contextual_parent("parent_span"));
@@ -374,7 +369,7 @@ impl ExpectedEvent {
374369
///
375370
/// ```
376371
/// use tracing::subscriber::with_default;
377-
/// use tracing_mock::{subscriber, expect};
372+
/// use tracing_mock::{expect, subscriber};
378373
///
379374
/// let event = expect::event()
380375
/// .with_ancestry(expect::is_contextual_root());
@@ -396,7 +391,7 @@ impl ExpectedEvent {
396391
///
397392
/// ```should_panic
398393
/// use tracing::subscriber::with_default;
399-
/// use tracing_mock::{subscriber, expect};
394+
/// use tracing_mock::{expect, subscriber};
400395
///
401396
/// let event = expect::event()
402397
/// .with_ancestry(expect::has_contextual_parent("parent_span"));

0 commit comments

Comments
 (0)