Skip to content

Commit

Permalink
mock: document public APIs in the field module (#2443)
Browse files Browse the repository at this point in the history
This change adds documentation to the tracing-mock `field` module and
all the public APIs within it. This includes doctests on all the methods
which serve as examples.

Additionally, the `field::msg` function (which constructs a field with
name "message" and the provided value) was moved to `expect::message`.
This is part of a unification of all expectation constructors inside the
`expect` module.

Refs: #539

Co-authored-by: David Barsky <me@davidbarsky.com>
  • Loading branch information
hds and davidbarsky authored Nov 14, 2023
1 parent ad2bfaa commit 29c494d
Show file tree
Hide file tree
Showing 7 changed files with 390 additions and 33 deletions.
10 changes: 5 additions & 5 deletions tracing-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ Below is an example that checks that an event contains a message:

```rust
use tracing::collect::with_default;
use tracing_mock::{collector, expect, field};
use tracing_mock::{collector, expect};

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

let (collector, handle) = collector::mock()
.event(expect::event().with_fields(field::msg("preparing to shave yaks")))
.event(expect::event().with_fields(expect::message("preparing to shave yaks")))
.only()
.run_with_handle();

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

```rust
use tracing::collect::with_default;
use tracing_mock::{collector, expect, field};
use tracing_mock::{collector, expect};

#[tracing::instrument]
fn yak_shaving(number_of_yaks: u32) {
Expand All @@ -128,15 +128,15 @@ let (collector, handle) = collector::mock()
expect::event().with_fields(
expect::field("number_of_yaks")
.with_value(&yak_count)
.and(field::msg("preparing to shave yaks"))
.and(expect::message("preparing to shave yaks"))
.only(),
),
)
.event(
expect::event().with_fields(
expect::field("all_yaks_shaved")
.with_value(&true)
.and(field::msg("yak shaving completed."))
.and(expect::message("yak shaving completed."))
.only(),
),
)
Expand Down
8 changes: 4 additions & 4 deletions tracing-mock/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//!
//! let (collector, handle) = collector::mock()
//! // Expect a single event with a specified message
//! .event(expect::event().with_fields(field::msg("droids")))
//! .event(expect::event().with_fields(expect::message("droids")))
//! .only()
//! .run_with_handle();
//!
Expand Down Expand Up @@ -40,7 +40,7 @@
//! // Enter a matching span
//! .enter(span.clone())
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(field::msg("collect parting message")))
//! .event(expect::event().with_fields(expect::message("collect parting message")))
//! // Record a value for the field `parting` on a matching span
//! .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
//! // Exit a matching span
Expand Down Expand Up @@ -81,7 +81,7 @@
//! .named("my_span");
//! let (collector, handle) = collector::mock()
//! .enter(span.clone())
//! .event(expect::event().with_fields(field::msg("collect parting message")))
//! .event(expect::event().with_fields(expect::message("collect parting message")))
//! .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
//! .exit(span)
//! .only()
Expand Down Expand Up @@ -221,7 +221,7 @@ pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
/// // Enter a matching span
/// .enter(span.clone())
/// // Record an event with message "collect parting message"
/// .event(expect::event().with_fields(field::msg("collect parting message")))
/// .event(expect::event().with_fields(expect::message("collect parting message")))
/// // Record a value for the field `parting` on a matching span
/// .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
/// // Exit a matching span
Expand Down
2 changes: 1 addition & 1 deletion tracing-mock/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct ExpectedEvent {
}

pub fn msg(message: impl fmt::Display) -> ExpectedEvent {
expect::event().with_fields(field::msg(message))
expect::event().with_fields(expect::message(message))
}

impl ExpectedEvent {
Expand Down
7 changes: 7 additions & 0 deletions tracing-mock/src/expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ where
}
}

pub fn message(message: impl fmt::Display) -> ExpectedField {
ExpectedField {
name: "message".to_string(),
value: ExpectedValue::Debug(message.to_string()),
}
}

pub fn span() -> ExpectedSpan {
ExpectedSpan {
..Default::default()
Expand Down
Loading

0 comments on commit 29c494d

Please sign in to comment.