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
This commit is contained in:
Hayden Stainsby 2024-11-02 12:25:10 +01:00
parent 760a2123dd
commit 96c0e297f1
17 changed files with 480 additions and 222 deletions

View File

@ -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]
@ -71,14 +71,14 @@ Below is an example that checks that an event contains a message:
```rust
use tracing::subscriber::with_default;
use tracing_mock::{subscriber, expect, field};
use tracing_mock::{expect, subscriber};
fn yak_shaving() {
tracing::info!("preparing to shave yaks");
}
let (subscriber, handle) = subscriber::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();
@ -102,7 +102,7 @@ Below is a slightly more complex example. `tracing-mock` asserts that, in order:
```rust
use tracing::subscriber::with_default;
use tracing_mock::{subscriber, expect};
use tracing_mock::{expect, subscriber};
#[tracing::instrument]
fn yak_shaving(number_of_yaks: u32) {
@ -128,7 +128,7 @@ let (subscriber, handle) = subscriber::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(),
),
)
@ -136,7 +136,7 @@ let (subscriber, handle) = subscriber::mock()
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(),
),
)

View File

@ -28,16 +28,15 @@
//!
//! [`subscriber`]: mod@crate::subscriber
//! [`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
@ -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.
///
@ -100,7 +95,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
@ -120,7 +115,7 @@ impl ExpectedEvent {
///
/// ```should_panic
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
@ -156,7 +151,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .at_level(tracing::Level::WARN);
@ -177,7 +172,7 @@ impl ExpectedEvent {
///
/// ```should_panic
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .at_level(tracing::Level::INFO);
@ -210,7 +205,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_target("some_target");
@ -230,7 +225,7 @@ impl ExpectedEvent {
///
/// ```should_panic
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_target("some_target");
@ -277,7 +272,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let parent = expect::span()
/// .named("parent_span")
@ -304,7 +299,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_ancestry(expect::has_explicit_parent("parent_span"));
@ -326,7 +321,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_ancestry(expect::is_explicit_root());
@ -350,7 +345,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_ancestry(expect::has_contextual_parent("parent_span"));
@ -374,7 +369,7 @@ impl ExpectedEvent {
///
/// ```
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_ancestry(expect::is_contextual_root());
@ -396,7 +391,7 @@ impl ExpectedEvent {
///
/// ```should_panic
/// use tracing::subscriber::with_default;
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_ancestry(expect::has_contextual_parent("parent_span"));

View File

@ -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::{expect, subscriber};
//!
//! let (subscriber, handle) = subscriber::mock()
//! // Expect an event with message
//! .event(expect::event().with_fields(expect::msg("message")))
//! .only()
//! .run_with_handle();
//!
//! tracing::subscriber::with_default(subscriber, || {
//! tracing::info!("message");
//! });
//!
//! handle.assert_finished();
//! ```
use std::fmt;
use crate::{
@ -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::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// 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::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// 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::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .new_span(expect::span())
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// });
///
/// handle.assert_finished();
/// ```
///
/// If we expect to enter a span and instead record something else, the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// 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::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different field value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "different_field_value");
/// });
///
/// handle.assert_finished();
/// ```
pub fn field<K>(name: K) -> ExpectedField
where
String: From<K>,
@ -39,34 +190,74 @@ 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::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!("message");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different message value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// 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
/// [`ExpectedSpan`] passed to [`MockCollector::new_span`] to
/// [`ExpectedSpan`] passed to [`MockSubscriber::new_span`] to
/// ensure that it gets set. When the a clone of the same
/// `ExpectedSpan` is attached to an [`ExpectedSpan`] and passed to
/// any other method on [`MockCollector`] that accepts it, it will
/// any other method on [`MockSubscriber`] that accepts it, it will
/// ensure that it is exactly the same span used across those
/// distinct expectations.
///
/// For more details on how to use this struct, see the documentation
/// on [`ExpectedSpan::with_id`].
///
/// [`MockCollector`]: struct@crate::collector::MockCollector
/// [`MockCollector::new_span`]: fn@crate::collector::MockCollector::new_span
/// [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
/// [`MockSubscriber::new_span`]: fn@crate::subscriber::MockSubscriber::new_span
pub fn id() -> ExpectedId {
ExpectedId::new_unset()
}

View File

@ -14,7 +14,7 @@
//! specific name, without any expectation about the value:
//!
//! ```
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let event = expect::event()
//! .with_fields(expect::field("field_name"));
@ -34,7 +34,7 @@
//! each of them:
//!
//! ```
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let event = expect::event().with_fields(
//! expect::field("string_field")
@ -63,7 +63,7 @@
//! different:
//!
//! ```should_panic
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let event = expect::event()
//! .with_fields(expect::field("field_name").with_value(&"value"));
@ -81,6 +81,8 @@
//!
//! [`subscriber`]: mod@crate::subscriber
//! [`expect::field`]: fn@crate::expect::field
use std::{collections::HashMap, fmt};
use tracing::{
callsite,
callsite::Callsite,
@ -88,8 +90,6 @@ use tracing::{
metadata::Kind,
};
use std::{collections::HashMap, fmt};
/// An expectation for multiple fields.
///
/// For a detailed description and examples, see the documentation for
@ -153,12 +153,13 @@ impl PartialEq for ExpectedValue {
impl ExpectedField {
/// Sets the value to expect when matching this field.
///
/// If the recorded value for this field diffs, the expectation will fail.
/// If the recorded value for this field is different, the
/// expectation will fail.
///
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field_name").with_value(&"value"));
@ -177,7 +178,7 @@ impl ExpectedField {
/// A different value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field_name").with_value(&"value"));
@ -208,7 +209,7 @@ impl ExpectedField {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
@ -233,7 +234,7 @@ impl ExpectedField {
/// If the second field is not present, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
@ -268,27 +269,30 @@ impl ExpectedField {
///
/// # Examples
///
/// Check that only a single field is recorded.
/// The following test passes despite the recorded event having
/// fields that were not expected because `only` was not
/// used:
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field").with_value(&"value").only());
/// .with_fields(expect::field("field").with_value(&"value"));
///
/// let (subscriber, handle) = subscriber::mock().event(event).run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field = "value");
/// tracing::info!(field = "value", another_field = 42,);
/// });
///
/// handle.assert_finished();
/// ```
///
/// The following example fails because a second field is recorded.
/// If we include `only` on the `ExpectedField` then the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field").with_value(&"value").only());
@ -323,9 +327,9 @@ impl From<ExpectedField> for ExpectedFields {
impl ExpectedFields {
/// Adds an additional [`ExpectedField`] to be matched.
///
/// _All_ fields must match for the expectation to pass. If any of
/// them are not present, if any of the values differs, the
/// expectation will fail.
/// All fields must match, if any of them are not present, or if
/// the value for any field is different, the expectation will
/// fail.
///
/// This method performs the same function as
/// [`ExpectedField::and`], but applies in the case where there are
@ -334,7 +338,7 @@ impl ExpectedFields {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
@ -362,7 +366,7 @@ impl ExpectedFields {
/// event, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
@ -391,7 +395,7 @@ impl ExpectedFields {
self
}
/// Asserts that no fields other than those specified should be
/// Indicates that no fields other than those specified should be
/// expected.
///
/// This method performs the same function as
@ -400,16 +404,16 @@ impl ExpectedFields {
///
/// # Examples
///
/// Check that only two fields are recorded on the event.
/// The following test will pass, even though additional fields are
/// recorded on the event.
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
/// .with_value(&"value")
/// .and(expect::field("another_field").with_value(&42))
/// .only(),
/// .and(expect::field("another_field").with_value(&42)),
/// );
///
/// let (subscriber, handle) = subscriber::mock()
@ -420,16 +424,18 @@ impl ExpectedFields {
/// tracing::info!(
/// field = "value",
/// another_field = 42,
/// a_third_field = true,
/// );
/// });
///
/// handle.assert_finished();
/// ```
///
/// The following example fails because a third field is recorded.
/// If we include `only` on the `ExpectedFields` then the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::field("field")
@ -558,7 +564,7 @@ impl<'a> Visit for CheckVisitor<'a> {
}
impl<'a> CheckVisitor<'a> {
pub fn finish(self) {
pub(crate) fn finish(self) {
assert!(
self.expect.fields.is_empty(),
"[{}] {}missing {}",

View File

@ -12,7 +12,7 @@
//!
//! let (layer, handle) = layer::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")))
//! .run_with_handle();
//!
//! // Use `set_default` to apply the `MockSubscriber` until the end
@ -33,7 +33,7 @@
//! their respective fields:
//!
//! ```
//! use tracing_mock::{expect, field, layer};
//! use tracing_mock::{expect, layer};
//! use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
//!
//! let span = expect::span()
@ -42,7 +42,7 @@
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(expect::message("say hello")))
//! .event(expect::event().with_fields(expect::msg("say hello")))
//! // Exit a matching span
//! .exit(&span)
//! // Expect no further messages to be recorded
@ -84,7 +84,7 @@
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "collect parting message"
//! .event(expect::event().with_fields(expect::message("say hello")))
//! .event(expect::event().with_fields(expect::msg("say hello")))
//! // Exit a matching span
//! .exit(&span)
//! // Expect no further messages to be recorded
@ -115,13 +115,12 @@
//! ```
//!
//! [`Layer`]: trait@tracing_subscriber::layer::Layer
use crate::{
ancestry::{get_ancestry, ActualAncestry, HasAncestry},
event::ExpectedEvent,
expect::Expect,
span::{ActualSpan, ExpectedSpan, NewSpan},
subscriber::MockHandle,
use std::{
collections::VecDeque,
fmt,
sync::{Arc, Mutex},
};
use tracing_core::{
span::{Attributes, Id, Record},
Event, Subscriber,
@ -131,10 +130,12 @@ use tracing_subscriber::{
registry::{LookupSpan, SpanRef},
};
use std::{
collections::VecDeque,
fmt,
sync::{Arc, Mutex},
use crate::{
ancestry::{get_ancestry, ActualAncestry, HasAncestry},
event::ExpectedEvent,
expect::Expect,
span::{ActualSpan, ExpectedSpan, NewSpan},
subscriber::MockHandle,
};
/// Create a [`MockLayerBuilder`] used to construct a
@ -155,7 +156,7 @@ use std::{
/// // Enter a matching span
/// .enter(&span)
/// // Record an event with message "collect parting message"
/// .event(expect::event().with_fields(expect::message("say hello")))
/// .event(expect::event().with_fields(expect::msg("say hello")))
/// // Exit a matching span
/// .exit(&span)
/// // Expect no further messages to be recorded
@ -207,7 +208,7 @@ pub fn mock() -> MockLayerBuilder {
///
/// # Examples
///
/// The example from [`named`] could be rewritten as:
/// The example from [`MockLayerBuilder::named`] could be rewritten as:
///
/// ```should_panic
/// use tracing_mock::{expect, layer};
@ -258,6 +259,7 @@ pub fn named(name: impl std::fmt::Display) -> MockLayerBuilder {
///
/// [`layer`]: mod@crate::layer
#[derive(Debug)]
pub struct MockLayerBuilder {
expected: VecDeque<Expect>,
name: String,

View File

@ -1,4 +1,39 @@
#![doc = include_str!("../README.md")]
#![cfg_attr(
docsrs,
// Allows displaying cfgs/feature flags in the documentation.
feature(doc_cfg),
// Fail the docs build if any intra-docs links are broken
deny(rustdoc::broken_intra_doc_links),
)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png",
html_favicon_url = "https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/favicon.ico",
issue_tracker_base_url = "https://github.com/tokio-rs/tracing/issues/"
)]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub,
bad_style,
dead_code,
improper_ctypes,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_interfaces,
private_bounds,
unconditional_recursion,
unused,
unused_allocation,
unused_comparisons,
unused_parens,
while_true
)]
pub mod ancestry;
pub mod event;
pub mod expect;

View File

@ -1,4 +1,5 @@
use std::fmt;
use tracing_core::Metadata;
#[derive(Clone, Debug, Eq, PartialEq, Default)]

View File

@ -11,7 +11,7 @@
//! # Examples
//!
//! ```
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let span = expect::span()
//! .named("interesting_span")
@ -52,7 +52,7 @@
//! The following example asserts the name, level, parent, and fields of the span:
//!
//! ```
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let span = expect::span()
//! .named("interesting_span")
@ -87,7 +87,7 @@
//! the following test will fail due to a mismatch in the spans' names:
//!
//! ```should_panic
//! use tracing_mock::{subscriber, expect};
//! use tracing_mock::{expect, subscriber};
//!
//! let span = expect::span()
//! .named("interesting_span")
@ -109,13 +109,6 @@
//! [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
//! [`subscriber`]: mod@crate::subscriber
//! [`expect::span`]: fn@crate::expect::span
#![allow(missing_docs)]
use crate::{
ancestry::{ActualAncestry, ExpectedAncestry},
expect,
field::ExpectedFields,
metadata::ExpectedMetadata,
};
use std::{
error, fmt,
sync::{
@ -124,6 +117,12 @@ use std::{
},
};
use crate::{
ancestry::{ActualAncestry, ExpectedAncestry},
field::ExpectedFields,
metadata::ExpectedMetadata,
};
/// A mock span.
///
/// This is intended for use with the mock subscriber API in the
@ -180,13 +179,6 @@ pub struct NewSpan {
pub(crate) ancestry: Option<ExpectedAncestry>,
}
pub fn named<I>(name: I) -> ExpectedSpan
where
I: Into<String>,
{
expect::span().named(name)
}
pub(crate) struct ActualSpan {
id: tracing_core::span::Id,
metadata: Option<&'static tracing_core::Metadata<'static>>,
@ -244,7 +236,7 @@ impl ExpectedSpan {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span().named("span name");
///
@ -282,7 +274,7 @@ impl ExpectedSpan {
/// When the span name is different, the assertion will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span().named("span name");
///
@ -334,7 +326,7 @@ impl ExpectedSpan {
/// second:
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
/// let id1 = expect::id();
/// let span1 = expect::span().named("span").with_id(id1.clone());
/// let id2 = expect::id();
@ -367,7 +359,7 @@ impl ExpectedSpan {
/// example can be used.
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
/// let id1 = expect::id();
/// let id2 = expect::id();
///
@ -397,7 +389,7 @@ impl ExpectedSpan {
/// fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
/// let id1 = expect::id();
/// let span1 = expect::span().named("span").with_id(id1.clone());
/// let id2 = expect::id();
@ -443,7 +435,7 @@ impl ExpectedSpan {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO);
@ -464,7 +456,7 @@ impl ExpectedSpan {
/// recorded at any other level:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO);
@ -498,7 +490,7 @@ impl ExpectedSpan {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_target("some_target");
@ -518,7 +510,7 @@ impl ExpectedSpan {
/// The test will fail if the target is different:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_target("some_target");
@ -571,7 +563,7 @@ impl ExpectedSpan {
/// An explicit or contextual parent can be matched on an `ExpectedSpan`.
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let parent = expect::span()
/// .named("parent_span")
@ -599,7 +591,7 @@ impl ExpectedSpan {
/// [`ExpectedId`] can be passed to match a span with that Id.
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_ancestry(expect::has_explicit_parent("parent_span"));
@ -620,7 +612,7 @@ impl ExpectedSpan {
/// In the following example, the expected span is an explicit root:
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_ancestry(expect::is_explicit_root());
@ -641,7 +633,7 @@ impl ExpectedSpan {
/// `parent_span`:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let parent_span = expect::span().named("parent_span");
/// let span = expect::span()
@ -666,7 +658,7 @@ impl ExpectedSpan {
/// a contextually-determined root:
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_ancestry(expect::is_contextual_root());
@ -687,7 +679,7 @@ impl ExpectedSpan {
/// `parent_span`:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let parent_span = expect::span().named("parent_span");
/// let span = expect::span()
@ -741,7 +733,7 @@ impl ExpectedSpan {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
@ -760,7 +752,7 @@ impl ExpectedSpan {
/// A different field value will cause the expectation to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));

View File

@ -8,11 +8,11 @@
//! # Examples
//!
//! ```
//! use tracing_mock::{subscriber, expect, field};
//! use tracing_mock::{expect, subscriber, field};
//!
//! let (subscriber, handle) = subscriber::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();
//!
@ -32,7 +32,7 @@
//! their respective fields:
//!
//! ```
//! use tracing_mock::{subscriber, expect, field};
//! use tracing_mock::{expect, subscriber, field};
//!
//! let span = expect::span()
//! .named("my_span");
@ -40,11 +40,11 @@
//! // Enter a matching span
//! .enter(&span)
//! // Record an event with message "subscriber parting message"
//! .event(expect::event().with_fields(expect::message("subscriber parting message")))
//! .event(expect::event().with_fields(expect::msg("subscriber parting message")))
//! // Record a value for the field `parting` on a matching span
//! .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
//! .record(&span, expect::field("parting").with_value(&"goodbye world!"))
//! // Exit a matching span
//! .exit(&span)
//! .exit(span)
//! // Expect no further messages to be recorded
//! .only()
//! // Return the subscriber and handle
@ -75,13 +75,13 @@
//! span before recording an event, the test will fail:
//!
//! ```should_panic
//! use tracing_mock::{subscriber, expect, field};
//! use tracing_mock::{expect, subscriber, field};
//!
//! let span = expect::span()
//! .named("my_span");
//! let (subscriber, handle) = subscriber::mock()
//! .enter(&span)
//! .event(expect::event().with_fields(expect::message("subscriber parting message")))
//! .event(expect::event().with_fields(expect::msg("collect parting message")))
//! .record(&span, expect::field("parting").with_value(&"goodbye world!"))
//! .exit(span)
//! .only()
@ -137,13 +137,6 @@
//!
//! [`Subscriber`]: trait@tracing::Subscriber
//! [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
use crate::{
ancestry::get_ancestry,
event::ExpectedEvent,
expect::Expect,
field::ExpectedFields,
span::{ActualSpan, ExpectedSpan, NewSpan},
};
use std::{
collections::{HashMap, VecDeque},
sync::{
@ -159,6 +152,14 @@ use tracing::{
Event, Metadata, Subscriber,
};
use crate::{
ancestry::get_ancestry,
event::ExpectedEvent,
expect::Expect,
field::ExpectedFields,
span::{ActualSpan, ExpectedSpan, NewSpan},
};
pub(crate) struct SpanState {
id: Id,
name: &'static str,
@ -188,6 +189,7 @@ struct Running<F: Fn(&Metadata<'_>) -> bool> {
/// for the methods and the [`subscriber`] module.
///
/// [`subscriber`]: mod@crate::subscriber
#[derive(Debug)]
pub struct MockSubscriber<F: Fn(&Metadata<'_>) -> bool> {
expected: VecDeque<Expect>,
max_level: Option<LevelFilter>,
@ -204,6 +206,7 @@ pub struct MockSubscriber<F: Fn(&Metadata<'_>) -> bool> {
/// module documentation.
///
/// [`subscriber`]: mod@crate::subscriber
#[derive(Debug)]
pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
/// Create a new [`MockSubscriber`].
@ -215,7 +218,7 @@ pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
///
///
/// ```
/// use tracing_mock::{subscriber, expect, field};
/// use tracing_mock::{expect, subscriber, field};
///
/// let span = expect::span()
/// .named("my_span");
@ -223,11 +226,11 @@ pub struct MockHandle(Arc<Mutex<VecDeque<Expect>>>, String);
/// // Enter a matching span
/// .enter(&span)
/// // Record an event with message "subscriber parting message"
/// .event(expect::event().with_fields(expect::message("subscriber parting message")))
/// .event(expect::event().with_fields(expect::msg("subscriber parting message")))
/// // Record a value for the field `parting` on a matching span
/// .record(span.clone(), expect::field("parting").with_value(&"goodbye world!"))
/// .record(&span, expect::field("parting").with_value(&"goodbye world!"))
/// // Exit a matching span
/// .exit(&span)
/// .exit(span)
/// // Expect no further messages to be recorded
/// .only()
/// // Return the subscriber and handle
@ -293,7 +296,7 @@ where
/// event, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber_1, handle_1) = subscriber::mock()
/// .named("subscriber-1")
@ -348,7 +351,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
@ -364,7 +367,7 @@ where
/// A span is entered before the event, causing the test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
@ -402,7 +405,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -423,7 +426,7 @@ where
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -462,7 +465,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -485,7 +488,7 @@ where
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -529,7 +532,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -551,7 +554,7 @@ where
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -591,7 +594,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -612,7 +615,7 @@ where
/// test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .at_level(tracing::Level::INFO)
@ -674,7 +677,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let cause = expect::span().named("cause");
/// let consequence = expect::span().named("consequence");
@ -698,7 +701,7 @@ where
/// this test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let cause = expect::span().named("cause");
/// let consequence = expect::span().named("consequence");
@ -744,7 +747,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .named("my_span");
@ -768,7 +771,7 @@ where
/// causing the test to fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let span = expect::span()
/// .named("my_span");
@ -809,7 +812,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .with_filter(|meta| meta.level() <= &tracing::Level::WARN)
@ -852,7 +855,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .with_max_level_hint(tracing::Level::INFO)
@ -888,7 +891,7 @@ where
/// expect a single event, but receive three:
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
@ -906,7 +909,7 @@ where
/// After including `only`, the test will fail:
///
/// ```should_panic
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
@ -963,7 +966,7 @@ where
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// // subscriber and handle are returned from `run_with_handle()`
/// let (subscriber, handle) = subscriber::mock()
@ -1326,7 +1329,7 @@ impl MockHandle {
/// # Examples
///
/// ```
/// use tracing_mock::{subscriber, expect};
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())

View File

@ -3,7 +3,7 @@
mod per_layer;
use tracing::{self, subscriber::with_default, Level};
use tracing_mock::{expect, layer, span, subscriber};
use tracing_mock::{expect, layer, subscriber};
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
prelude::*,
@ -94,18 +94,17 @@ fn level_filter_event_with_target_and_span_global() {
.parse()
.expect("filter should parse");
let cool_span = span::named("cool_span");
let uncool_span = span::named("uncool_span");
let cool_span = expect::span().named("cool_span");
let (layer, handle) = layer::mock()
.enter(cool_span.clone())
.enter(&cool_span)
.event(
expect::event()
.at_level(Level::DEBUG)
.in_scope(vec![cool_span.clone()]),
)
.exit(cool_span)
.enter(uncool_span.clone())
.exit(uncool_span)
.enter("uncool_span")
.exit("uncool_span")
.only()
.run_with_handle();
@ -330,7 +329,7 @@ mod per_layer_filter {
.parse()
.expect("filter should parse");
let cool_span = span::named("cool_span");
let cool_span = expect::span().named("cool_span");
let (layer, handle) = layer::mock()
.enter(cool_span.clone())
.event(
@ -422,8 +421,8 @@ mod per_layer_filter {
let filter: EnvFilter = "info,[cool_span]=debug"
.parse()
.expect("filter should parse");
let cool_span = span::named("cool_span");
let uncool_span = span::named("uncool_span");
let cool_span = expect::span().named("cool_span");
let uncool_span = expect::span().named("uncool_span");
let (layer, finished) = layer::mock()
.event(expect::event().at_level(Level::INFO))
.enter(cool_span.clone())
@ -493,7 +492,7 @@ mod per_layer_filter {
// Test that multiple dynamic (span) filters only apply to the layers
// they're attached to.
let (layer1, handle1) = {
let span = span::named("span1");
let span = expect::span().named("span1");
let filter: EnvFilter = "[span1]=debug".parse().expect("filter 1 should parse");
let (layer, handle) = layer::named("layer1")
.enter(span.clone())
@ -509,7 +508,7 @@ mod per_layer_filter {
};
let (layer2, handle2) = {
let span = span::named("span2");
let span = expect::span().named("span2");
let filter: EnvFilter = "[span2]=info".parse().expect("filter 2 should parse");
let (layer, handle) = layer::named("layer2")
.enter(span.clone())

View File

@ -2,7 +2,7 @@
//! `Layer` filter).
#![cfg(feature = "registry")]
use super::*;
use tracing_mock::{layer, span};
use tracing_mock::{expect, layer};
#[test]
fn level_filter_event() {
@ -92,7 +92,7 @@ fn level_filter_event_with_target_and_span() {
.parse()
.expect("filter should parse");
let cool_span = span::named("cool_span");
let cool_span = expect::span().named("cool_span");
let (layer, handle) = layer::mock()
.enter(cool_span.clone())
.event(
@ -184,8 +184,8 @@ fn span_name_filter_is_dynamic() {
let filter: EnvFilter = "info,[cool_span]=debug"
.parse()
.expect("filter should parse");
let cool_span = span::named("cool_span");
let uncool_span = span::named("uncool_span");
let cool_span = expect::span().named("cool_span");
let uncool_span = expect::span().named("uncool_span");
let (layer, finished) = layer::mock()
.event(expect::event().at_level(Level::INFO))
.enter(cool_span.clone())
@ -255,7 +255,7 @@ fn multiple_dynamic_filters() {
// Test that multiple dynamic (span) filters only apply to the layers
// they're attached to.
let (layer1, handle1) = {
let span = span::named("span1");
let span = expect::span().named("span1");
let filter: EnvFilter = "[span1]=debug".parse().expect("filter 1 should parse");
let (layer, handle) = layer::named("layer1")
.enter(span.clone())
@ -271,7 +271,7 @@ fn multiple_dynamic_filters() {
};
let (layer2, handle2) = {
let span = span::named("span2");
let span = expect::span().named("span2");
let filter: EnvFilter = "[span2]=info".parse().expect("filter 2 should parse");
let (layer, handle) = layer::named("layer2")
.enter(span.clone())

View File

@ -1,5 +1,5 @@
use super::*;
use tracing_mock::{event, expect, layer::MockLayer};
use tracing_mock::{expect, layer::MockLayer};
#[test]
fn filters_span_scopes() {
@ -8,12 +8,16 @@ fn filters_span_scopes() {
.enter(expect::span().at_level(Level::INFO))
.enter(expect::span().at_level(Level::WARN))
.enter(expect::span().at_level(Level::ERROR))
.event(event::msg("hello world").in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
expect::span().at_level(Level::INFO),
expect::span().at_level(Level::DEBUG),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
expect::span().at_level(Level::INFO),
expect::span().at_level(Level::DEBUG),
]),
)
.exit(expect::span().at_level(Level::ERROR))
.exit(expect::span().at_level(Level::WARN))
.exit(expect::span().at_level(Level::INFO))
@ -24,11 +28,15 @@ fn filters_span_scopes() {
.enter(expect::span().at_level(Level::INFO))
.enter(expect::span().at_level(Level::WARN))
.enter(expect::span().at_level(Level::ERROR))
.event(event::msg("hello world").in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
expect::span().at_level(Level::INFO),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
expect::span().at_level(Level::INFO),
]),
)
.exit(expect::span().at_level(Level::ERROR))
.exit(expect::span().at_level(Level::WARN))
.exit(expect::span().at_level(Level::INFO))
@ -37,10 +45,14 @@ fn filters_span_scopes() {
let (warn_layer, warn_handle) = layer::named("warn")
.enter(expect::span().at_level(Level::WARN))
.enter(expect::span().at_level(Level::ERROR))
.event(event::msg("hello world").in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().at_level(Level::ERROR),
expect::span().at_level(Level::WARN),
]),
)
.exit(expect::span().at_level(Level::ERROR))
.exit(expect::span().at_level(Level::WARN))
.only()
@ -72,12 +84,17 @@ fn filters_interleaved_span_scopes() {
layer::named(format!("target_{}", target))
.enter(expect::span().with_target(target))
.enter(expect::span().with_target(target))
.event(event::msg("hello world").in_scope(vec![
expect::span().with_target(target),
expect::span().with_target(target),
]))
.event(
event::msg("hello to my target")
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().with_target(target),
expect::span().with_target(target),
]),
)
.event(
expect::event()
.with_fields(expect::msg("hello to my target"))
.in_scope(vec![
expect::span().with_target(target),
expect::span().with_target(target),
@ -95,10 +112,14 @@ fn filters_interleaved_span_scopes() {
let (all_layer, all_handle) = layer::named("all")
.enter(expect::span().with_target("b"))
.enter(expect::span().with_target("a"))
.event(event::msg("hello world").in_scope(vec![
expect::span().with_target("a"),
expect::span().with_target("b"),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().with_target("a"),
expect::span().with_target("b"),
]),
)
.exit(expect::span().with_target("a"))
.exit(expect::span().with_target("b"))
.only()

View File

@ -9,7 +9,7 @@ mod trees;
mod vec;
use tracing::{level_filters::LevelFilter, Level};
use tracing_mock::{event, expect, layer, subscriber};
use tracing_mock::{expect, layer, subscriber};
use tracing_subscriber::{filter, prelude::*, Layer};
#[test]
@ -159,18 +159,18 @@ fn global_filters_affect_subscriber_filters() {
#[test]
fn filter_fn() {
let (all, all_handle) = layer::named("all_targets")
.event(event::msg("hello foo"))
.event(event::msg("hello bar"))
.event(expect::event().with_fields(expect::msg("hello foo")))
.event(expect::event().with_fields(expect::msg("hello bar")))
.only()
.run_with_handle();
let (foo, foo_handle) = layer::named("foo_target")
.event(event::msg("hello foo"))
.event(expect::event().with_fields(expect::msg("hello foo")))
.only()
.run_with_handle();
let (bar, bar_handle) = layer::named("bar_target")
.event(event::msg("hello bar"))
.event(expect::event().with_fields(expect::msg("hello bar")))
.only()
.run_with_handle();

View File

@ -1,5 +1,4 @@
use super::*;
use tracing_mock::event;
use tracing_subscriber::{
filter::{filter_fn, Targets},
prelude::*,
@ -39,7 +38,7 @@ fn inner_layer_short_circuits() {
// evaluation, we aren't left with a "dirty" per-layer filter state.
let (layer, handle) = layer::mock()
.event(event::msg("hello world"))
.event(expect::event().with_fields(expect::msg("hello world")))
.only()
.run_with_handle();

View File

@ -1,5 +1,5 @@
use super::*;
use tracing_mock::{event, expect, layer::MockLayer};
use tracing_mock::{expect, layer::MockLayer};
#[test]
fn basic_trees() {
@ -70,9 +70,13 @@ fn filter_span_scopes() {
fn target_layer(target: &'static str) -> (MockLayer, subscriber::MockHandle) {
layer::named(format!("target_{}", target))
.enter(expect::span().with_target(target).at_level(Level::INFO))
.event(event::msg("hello world").in_scope(vec![
expect::span().with_target(target).at_level(Level::INFO),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![expect::span()
.with_target(target)
.at_level(Level::INFO)]),
)
.exit(expect::span().with_target(target).at_level(Level::INFO))
.only()
.run_with_handle()
@ -83,10 +87,14 @@ fn filter_span_scopes() {
let (info_layer, info_handle) = layer::named("info")
.enter(expect::span().with_target("b").at_level(Level::INFO))
.enter(expect::span().with_target("a").at_level(Level::INFO))
.event(event::msg("hello world").in_scope(vec![
expect::span().with_target("a").at_level(Level::INFO),
expect::span().with_target("b").at_level(Level::INFO),
]))
.event(
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(vec![
expect::span().with_target("a").at_level(Level::INFO),
expect::span().with_target("b").at_level(Level::INFO),
]),
)
.exit(expect::span().with_target("a").at_level(Level::INFO))
.exit(expect::span().with_target("b").at_level(Level::INFO))
.only()
@ -103,14 +111,20 @@ fn filter_span_scopes() {
.enter(expect::span().with_target("b").at_level(Level::INFO))
.enter(expect::span().with_target("a").at_level(Level::INFO))
.enter(expect::span().with_target("b").at_level(Level::TRACE))
.event(event::msg("hello world").in_scope(full_scope.clone()))
.event(
event::msg("hello to my target")
expect::event()
.with_fields(expect::msg("hello world"))
.in_scope(full_scope.clone()),
)
.event(
expect::event()
.with_fields(expect::msg("hello to my target"))
.with_target("a")
.in_scope(full_scope.clone()),
)
.event(
event::msg("hello to my target")
expect::event()
.with_fields(expect::msg("hello to my target"))
.with_target("b")
.in_scope(full_scope),
)

View File

@ -86,7 +86,7 @@ fn message_without_delims() {
.and(
expect::field("question").with_value(&"life, the universe, and everything"),
)
.and(expect::message(format_args!(
.and(expect::msg(format_args!(
"hello from my event! tricky? {:?}!",
true
)))
@ -115,7 +115,7 @@ fn string_message_without_delims() {
.and(
expect::field("question").with_value(&"life, the universe, and everything"),
)
.and(expect::message(format_args!("hello from my event")))
.and(expect::msg(format_args!("hello from my event")))
.only(),
),
)

View File

@ -1,18 +1,18 @@
#![cfg(feature = "std")]
use tracing_mock::*;
use tracing_mock::{expect, subscriber};
#[test]
fn scoped_clobbers_global() {
// Reproduces https://github.com/tokio-rs/tracing/issues/2050
let (scoped, scoped_handle) = subscriber::mock()
.event(event::msg("before global"))
.event(event::msg("before drop"))
.event(expect::event().with_fields(expect::msg("before global")))
.event(expect::event().with_fields(expect::msg("before drop")))
.only()
.run_with_handle();
let (global, global_handle) = subscriber::mock()
.event(event::msg("after drop"))
.event(expect::event().with_fields(expect::msg("after drop")))
.only()
.run_with_handle();