Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock: document public APIs in the field module #2443

Merged
merged 13 commits into from
Nov 14, 2023
Merged
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
Loading