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: complete API documentation including expect module #2494

Merged
merged 27 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a5e7058
mock: document public API in collector module
hds Nov 16, 2022
39045ed
don't make MockCollector::drop_span deprecated
hds Nov 18, 2022
fe92324
I was wondering whether missing docs were checked...
hds Nov 18, 2022
28b25fc
Merge branch 'master' of github.com:tokio-rs/tracing into hds/mock-co…
hds Jan 4, 2023
b2816a2
added negative examples to most methods
hds Jan 5, 2023
299f093
typo fix
hds Jan 5, 2023
d37a121
Merge branch 'master' of github.com:tokio-rs/tracing into hds/mock-co…
hds Jan 5, 2023
757d877
Apply suggestions from code review
hds Jan 9, 2023
55901c8
Expanded collector module examples and other changes
hds Jan 10, 2023
f24c2ed
Merge branch 'hds/mock-collector-docs' of github.com:tokio-rs/tracing…
hds Jan 10, 2023
ca6b72a
Update tracing-mock/src/collector.rs
hds Jan 10, 2023
6408d2d
mock: document public APIs in `span` module
hds Jan 11, 2023
5a2d685
mock: document public APIs in the `field` module
hds Jan 12, 2023
df486f9
Ported some comments from #2446
hds Jan 18, 2023
7264d25
Merge branch 'master' into hds/mock-collector-docs
davidbarsky Jan 27, 2023
170d4fc
Merge branch 'master' of github.com:tokio-rs/tracing into hds/mock-do…
hds Jan 27, 2023
6b5740e
Merge branch 'master' into hds/mock-collector-docs
hds Jan 30, 2023
02a303b
Merge branch 'master' into hds/mock-docs-span
hds Jan 30, 2023
b7e2728
Merge branch 'master' of github.com:tokio-rs/tracing into hds/mock-do…
hds Jan 30, 2023
9041a28
Update subscriber tests with new `expect::message` function
hds Jan 30, 2023
7e4c950
rename `Span::with_field` to `Span::with_fields` in subscriber docs
hds Jan 30, 2023
7aabbbb
Merge branch 'hds/mock-docs-span' into hds/mock-collector-docs
hds Feb 6, 2023
ffb2cef
Merge branch 'hds/mock-docs-field' into hds/mock-collector-docs
hds Feb 6, 2023
dfa827b
mock: complete API documentation including expect module
hds Feb 8, 2023
c467577
Merge remote-tracking branch 'origin/master' into hds/mock-docs-expect
hds Oct 30, 2024
b7f8b52
fix error in expect::span docs
hds Nov 1, 2024
2377baf
enable all the normal warnings for tracing crates
hds Nov 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tracing-mock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# tracing-mock

Utilities for testing [`tracing`][tracing] and crates that uses it.
Utilities for testing [`tracing`] and crates that uses it.

[![Documentation (master)][docs-master-badge]][docs-master-url]
[![MIT licensed][mit-badge]][mit-url]
Expand Down Expand Up @@ -78,7 +78,7 @@ fn yak_shaving() {
}

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

Expand Down Expand Up @@ -128,15 +128,15 @@ let (collector, handle) = collector::mock()
expect::event().with_fields(
expect::field("number_of_yaks")
.with_value(&yak_count)
.and(expect::message("preparing to shave yaks"))
.and(expect::msg("preparing to shave yaks"))
.only(),
),
)
.event(
expect::event().with_fields(
expect::field("all_yaks_shaved")
.with_value(&true)
.and(expect::message("yak shaving completed."))
.and(expect::msg("yak shaving completed."))
.only(),
),
)
Expand Down Expand Up @@ -173,4 +173,4 @@ This project is licensed under the [MIT license][mit-url].

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Tracing by you, shall be licensed as MIT, without any additional
terms or conditions.
terms or conditions.
24 changes: 13 additions & 11 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(expect::message("droids")))
//! .event(expect::event().with_fields(expect::msg("droids")))
//! .only()
//! .run_with_handle();
//!
Expand Down Expand Up @@ -40,7 +40,7 @@
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(expect::message("collect parting message")))
//! .event(expect::event().with_fields(expect::msg("collect parting message")))
//! // Record a value for the field `parting` on a matching span
//! .record(&span, 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)
//! .event(expect::event().with_fields(expect::message("collect parting message")))
//! .event(expect::event().with_fields(expect::msg("collect parting message")))
//! .record(&span, expect::field("parting").with_value(&"goodbye world!"))
//! .exit(span)
//! .only()
Expand Down Expand Up @@ -137,13 +137,6 @@
//!
//! [`Collect`]: trait@tracing::Collect
//! [`MockCollector`]: struct@crate::collector::MockCollector
use crate::{
ancestry::get_ancestry,
event::ExpectedEvent,
expect::Expect,
field::ExpectedFields,
span::{ActualSpan, ExpectedSpan, NewSpan},
};
use std::{
collections::{HashMap, VecDeque},
sync::{
Expand All @@ -152,13 +145,22 @@ use std::{
},
thread,
};

use tracing::{
collect::Interest,
level_filters::LevelFilter,
span::{self, Attributes, Id},
Collect, Event, Metadata,
};

use crate::{
ancestry::get_ancestry,
event::ExpectedEvent,
expect::Expect,
field::ExpectedFields,
span::{ActualSpan, ExpectedSpan, NewSpan},
};

pub(crate) struct SpanState {
id: Id,
name: &'static str,
Expand Down Expand Up @@ -223,7 +225,7 @@ pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
/// // Enter a matching span
/// .enter(&span)
/// // Record an event with message "collect parting message"
/// .event(expect::event().with_fields(expect::message("collect parting message")))
/// .event(expect::event().with_fields(expect::msg("collect parting message")))
/// // Record a value for the field `parting` on a matching span
/// .record(&span, expect::field("parting").with_value(&"goodbye world!"))
/// // Exit a matching span
Expand Down
11 changes: 3 additions & 8 deletions tracing-mock/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@
//!
//! [`collector`]: mod@crate::collector
//! [`expect::event`]: fn@crate::expect::event
#![allow(missing_docs)]
use std::fmt;

use crate::{
ancestry::{ActualAncestry, ExpectedAncestry},
expect, field,
field,
metadata::ExpectedMetadata,
span,
};

use std::fmt;

/// An expected event.
///
/// For a detailed description and examples, see the documentation for
Expand All @@ -52,10 +51,6 @@ pub struct ExpectedEvent {
pub(super) metadata: ExpectedMetadata,
}

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

impl ExpectedEvent {
/// Sets a name to expect when matching an event.
///
Expand Down
205 changes: 198 additions & 7 deletions tracing-mock/src/expect.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
//! Construct expectations for traces which should be received
//!
//! This module contains constructors for expectations defined
//! in the [`event`], [`span`], and [`field`] modules.
//!
//! # Examples
//!
//! ```
//! use tracing_mock::{collector, expect};
//!
//! let (collector, handle) = collector::mock()
//! // Expect an event with message
//! .event(expect::event().with_fields(expect::msg("message")))
//! .only()
//! .run_with_handle();
//!
//! tracing::collect::with_default(collector, || {
//! tracing::info!("message");
//! });
//!
//! handle.assert_finished();
//! ```
use std::fmt;

use crate::{
Expand All @@ -23,12 +45,141 @@ pub(crate) enum Expect {
Nothing,
}

/// Create a new [`ExpectedEvent`].
///
/// For details on how to add additional assertions to the expected
/// event, see the [`event`] module and the [`ExpectedEvent`] struct.
///
/// # Examples
///
/// ```
/// use tracing_mock::{collector, expect};
///
/// let (collector, handle) = collector::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
///
/// If we expect an event and instead record something else, the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{collector, expect};
///
/// let (collector, handle) = collector::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// });
///
/// handle.assert_finished();
/// ```
pub fn event() -> ExpectedEvent {
ExpectedEvent {
..Default::default()
}
}

/// Construct a new [`ExpectedSpan`].
///
/// For details on how to add additional assertions to the expected
/// span, see the [`span`] module and the [`ExpectedSpan`] and
/// [`NewSpan`] structs.
///
/// # Examples
///
/// ```
/// use tracing_mock::{collector, expect};
///
/// let (collector, handle) = collector::mock()
/// .new_span(expect::span())
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// });
///
/// handle.assert_finished();
/// ```
///
/// If we expect an event and instead record something else, the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{collector, expect};
///
/// let (collector, handle) = collector::mock()
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
pub fn span() -> ExpectedSpan {
ExpectedSpan {
..Default::default()
}
}

/// Construct a new [`ExpectedField`].
///
/// For details on how to set the value of the expected field and
/// how to expect multiple fields, see the [`field`] module and the
/// [`ExpectedField`] and [`ExpectedFields`] structs.
/// span, see the [`span`] module and the [`ExpectedSpan`] and
/// [`NewSpan`] structs.
///
/// # Examples
///
/// ```
/// use tracing_mock::{collector, expect};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (collector, handle) = collector::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different field value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{collector, expect};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (collector, handle) = collector::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!(field.name = "different_field_value");
/// });
///
/// handle.assert_finished();
/// ```
pub fn field<K>(name: K) -> ExpectedField
where
String: From<K>,
Expand All @@ -39,19 +190,59 @@ where
}
}

pub fn message(message: impl fmt::Display) -> ExpectedField {
/// Construct a new message [`ExpectedField`].
///
/// For details on how to set the value of the message field and
/// how to expect multiple fields, see the [`field`] module and the
/// [`ExpectedField`] and [`ExpectedFields`] structs.
///
/// This is equivalent to
/// `expect::field("message").with_value(message)`.
///
/// # Examples
///
/// ```
/// use tracing_mock::{collector, expect};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (collector, handle) = collector::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!("message");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different message value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{collector, expect};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (collector, handle) = collector::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::collect::with_default(collector, || {
/// tracing::info!("different message");
/// });
///
/// handle.assert_finished();
/// ```
pub fn msg(message: impl fmt::Display) -> ExpectedField {
ExpectedField {
name: "message".to_string(),
value: ExpectedValue::Debug(message.to_string()),
}
}

pub fn span() -> ExpectedSpan {
ExpectedSpan {
..Default::default()
}
}

/// Returns a new, unset `ExpectedId`.
///
/// The `ExpectedId` needs to be attached to a [`NewSpan`] or an
Expand Down
Loading
Loading