From c6546d3632275c54d2e5d47e5a3589cb300cd092 Mon Sep 17 00:00:00 2001 From: Hayden Stainsby Date: Fri, 11 Nov 2022 18:53:06 +0100 Subject: [PATCH] mock: change helper functions to `expect::` (#2377) * mock: change helper functions to `expect::` The current format of test expectations in `tracing-mock` isn't ideal. The format `span::expect` requires importing `tracing_mock::` which may conflict with imports from other tracing crates, especially `tracing-core`. So we change the order and move the functions into a module called `expect` so that: * `event::expect` becomes `expect::event` * `span::expect` becomes `expect::span` * `field::expect` becomes `expect::field` This format has two advantages. 1. It reads as natural English, e.g "expect span" 2. It is no longer common to import the modules directly. Regarding point (2), the following format was previously common: ```rust use tracing_mock::field; field::expect(); ``` This import of the `field` module may then conflict with importing the same from `tracing_core`, making it necessary to rename one of the imports. The same code would now be written: ```rust use tracing_mock::expect; expect::field(); ``` Which is less likely to conflict. This change also fixes an unused warning on `MockHandle::new` when the `tracing-subscriber` feature is not enabled. Refs: #539 --- tracing-attributes/tests/err.rs | 4 +- tracing-attributes/tests/levels.rs | 30 +++---- tracing-attributes/tests/ret.rs | 18 ++-- tracing-mock/src/expectation.rs | 21 ----- tracing-mock/src/layer.rs | 2 +- ...d_layer_filters_dont_break_other_layers.rs | 18 ++-- .../tests/env_filter/per_layer.rs | 58 ++++++------- ...d_layer_filters_dont_break_other_layers.rs | 18 ++-- .../layer_filter_interests_are_cached.rs | 6 +- .../tests/layer_filters/filter_scopes.rs | 84 +++++++++---------- .../tests/layer_filters/main.rs | 42 +++++----- .../tests/layer_filters/per_event.rs | 6 +- .../tests/layer_filters/targets.rs | 1 + .../tests/layer_filters/trees.rs | 58 ++++++------- tracing-subscriber/tests/layer_filters/vec.rs | 26 +++--- .../multiple_layer_filter_interests_cached.rs | 22 ++--- ...d_layer_filters_dont_break_other_layers.rs | 18 ++-- tracing/tests/event.rs | 6 +- tracing/tests/span.rs | 18 ++-- tracing/tests/subscriber.rs | 27 +++--- 20 files changed, 231 insertions(+), 252 deletions(-) delete mode 100644 tracing-mock/src/expectation.rs diff --git a/tracing-attributes/tests/err.rs b/tracing-attributes/tests/err.rs index 8cf5d36103..57b387a67b 100644 --- a/tracing-attributes/tests/err.rs +++ b/tracing-attributes/tests/err.rs @@ -64,11 +64,11 @@ fn err_early_return() -> Result { #[test] fn test_early_return() { - let span = span::expect().named("err_early_return"); + let span = expect::span().named("err_early_return"); let (subscriber, handle) = subscriber::mock() .new_span(span.clone()) .enter(span.clone()) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::ERROR)) .exit(span.clone()) .drop_span(span) .only() diff --git a/tracing-attributes/tests/levels.rs b/tracing-attributes/tests/levels.rs index 5e9e14d528..863abab5d5 100644 --- a/tracing-attributes/tests/levels.rs +++ b/tracing-attributes/tests/levels.rs @@ -158,21 +158,21 @@ fn enum_levels() { #[instrument(level = Level::ERROR)] fn error() {} let (subscriber, handle) = subscriber::mock() - .new_span(span::expect().named("trace").at_level(Level::TRACE)) - .enter(span::expect().named("trace").at_level(Level::TRACE)) - .exit(span::expect().named("trace").at_level(Level::TRACE)) - .new_span(span::expect().named("debug").at_level(Level::DEBUG)) - .enter(span::expect().named("debug").at_level(Level::DEBUG)) - .exit(span::expect().named("debug").at_level(Level::DEBUG)) - .new_span(span::expect().named("info").at_level(Level::INFO)) - .enter(span::expect().named("info").at_level(Level::INFO)) - .exit(span::expect().named("info").at_level(Level::INFO)) - .new_span(span::expect().named("warn").at_level(Level::WARN)) - .enter(span::expect().named("warn").at_level(Level::WARN)) - .exit(span::expect().named("warn").at_level(Level::WARN)) - .new_span(span::expect().named("error").at_level(Level::ERROR)) - .enter(span::expect().named("error").at_level(Level::ERROR)) - .exit(span::expect().named("error").at_level(Level::ERROR)) + .new_span(expect::span().named("trace").at_level(Level::TRACE)) + .enter(expect::span().named("trace").at_level(Level::TRACE)) + .exit(expect::span().named("trace").at_level(Level::TRACE)) + .new_span(expect::span().named("debug").at_level(Level::DEBUG)) + .enter(expect::span().named("debug").at_level(Level::DEBUG)) + .exit(expect::span().named("debug").at_level(Level::DEBUG)) + .new_span(expect::span().named("info").at_level(Level::INFO)) + .enter(expect::span().named("info").at_level(Level::INFO)) + .exit(expect::span().named("info").at_level(Level::INFO)) + .new_span(expect::span().named("warn").at_level(Level::WARN)) + .enter(expect::span().named("warn").at_level(Level::WARN)) + .exit(expect::span().named("warn").at_level(Level::WARN)) + .new_span(expect::span().named("error").at_level(Level::ERROR)) + .enter(expect::span().named("error").at_level(Level::ERROR)) + .exit(expect::span().named("error").at_level(Level::ERROR)) .only() .run_with_handle(); diff --git a/tracing-attributes/tests/ret.rs b/tracing-attributes/tests/ret.rs index a93c31dc58..3395532391 100644 --- a/tracing-attributes/tests/ret.rs +++ b/tracing-attributes/tests/ret.rs @@ -69,7 +69,7 @@ fn test_custom_target() { #[test] fn test_custom_target() { let filter: EnvFilter = "my_target=info".parse().expect("filter should parse"); - let span = span::expect() + let span = expect::span() .named("ret_with_target") .with_target("my_target"); @@ -77,8 +77,8 @@ fn test_custom_target() { .new_span(span.clone()) .enter(span.clone()) .event( - event::expect() - .with_fields(field::expect("return").with_value(&tracing::field::debug(42))) + expect::event() + .with_fields(expect::field("return").with_value(&tracing::field::debug(42))) .at_level(Level::INFO) .with_target("my_target"), ) @@ -342,13 +342,13 @@ fn ret_warn_info() -> i32 { #[test] fn test_warn_info() { - let span = span::expect().named("ret_warn_info").at_level(Level::WARN); + let span = expect::span().named("ret_warn_info").at_level(Level::WARN); let (subscriber, handle) = subscriber::mock() .new_span(span.clone()) .enter(span.clone()) .event( - event::expect() - .with_fields(field::expect("return").with_value(&tracing::field::debug(42))) + expect::event() + .with_fields(expect::field("return").with_value(&tracing::field::debug(42))) .at_level(Level::INFO), ) .exit(span.clone()) @@ -367,13 +367,13 @@ fn ret_dbg_warn() -> i32 { #[test] fn test_dbg_warn() { - let span = span::expect().named("ret_dbg_warn").at_level(Level::INFO); + let span = expect::span().named("ret_dbg_warn").at_level(Level::INFO); let (subscriber, handle) = subscriber::mock() .new_span(span.clone()) .enter(span.clone()) .event( - event::expect() - .with_fields(field::expect("return").with_value(&tracing::field::debug(42))) + expect::event() + .with_fields(expect::field("return").with_value(&tracing::field::debug(42))) .at_level(Level::WARN), ) .exit(span.clone()) diff --git a/tracing-mock/src/expectation.rs b/tracing-mock/src/expectation.rs deleted file mode 100644 index cd48728ae7..0000000000 --- a/tracing-mock/src/expectation.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::{ - event::ExpectedEvent, - field::ExpectedFields, - span::{ExpectedSpan, NewSpan}, -}; - -#[derive(Debug, Eq, PartialEq)] -pub(crate) enum Expect { - Event(ExpectedEvent), - FollowsFrom { - consequence: ExpectedSpan, - cause: ExpectedSpan, - }, - Enter(ExpectedSpan), - Exit(ExpectedSpan), - CloneSpan(ExpectedSpan), - DropSpan(ExpectedSpan), - Visit(ExpectedSpan, ExpectedFields), - NewSpan(NewSpan), - Nothing, -} diff --git a/tracing-mock/src/layer.rs b/tracing-mock/src/layer.rs index 85509ab075..c577163dee 100644 --- a/tracing-mock/src/layer.rs +++ b/tracing-mock/src/layer.rs @@ -1,7 +1,7 @@ #![allow(missing_docs, dead_code)] use crate::{ event::ExpectedEvent, - expectation::Expect, + expect::Expect, field::ExpectedFields, span::{ExpectedSpan, NewSpan}, subscriber::MockHandle, diff --git a/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs b/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs index 478b6e9b20..b6d767739d 100644 --- a/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs +++ b/tracing-subscriber/tests/cached_layer_filters_dont_break_other_layers.rs @@ -1,7 +1,7 @@ #![cfg(feature = "registry")] use tracing::Level; use tracing_mock::{ - event, + expect, layer::{self, MockLayer}, subscriber, }; @@ -107,20 +107,20 @@ fn filter() -> LevelFilter { fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } diff --git a/tracing-subscriber/tests/env_filter/per_layer.rs b/tracing-subscriber/tests/env_filter/per_layer.rs index 587d34fb77..229b9ff776 100644 --- a/tracing-subscriber/tests/env_filter/per_layer.rs +++ b/tracing-subscriber/tests/env_filter/per_layer.rs @@ -2,15 +2,15 @@ //! `Layer` filter). #![cfg(feature = "registry")] use super::*; -use tracing_mock::{event, field, layer, span}; +use tracing_mock::{layer, span}; #[test] fn level_filter_event() { let filter: EnvFilter = "info".parse().expect("filter should parse"); let (layer, handle) = layer::mock() - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); @@ -34,16 +34,16 @@ fn same_name_spans() { .expect("filter should parse"); let (layer, handle) = layer::mock() .new_span( - span::expect() + expect::span() .named("foo") .at_level(Level::TRACE) - .with_field(field::expect("bar")), + .with_field(expect::field("bar")), ) .new_span( - span::expect() + expect::span() .named("foo") .at_level(Level::TRACE) - .with_field(field::expect("baz")), + .with_field(expect::field("baz")), ) .only() .run_with_handle(); @@ -62,11 +62,11 @@ fn same_name_spans() { fn level_filter_event_with_target() { let filter: EnvFilter = "info,stuff=debug".parse().expect("filter should parse"); let (layer, handle) = layer::mock() - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::DEBUG).with_target("stuff")) - .event(event::expect().at_level(Level::WARN).with_target("stuff")) - .event(event::expect().at_level(Level::ERROR)) - .event(event::expect().at_level(Level::ERROR).with_target("stuff")) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::DEBUG).with_target("stuff")) + .event(expect::event().at_level(Level::WARN).with_target("stuff")) + .event(expect::event().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::ERROR).with_target("stuff")) .only() .run_with_handle(); @@ -96,7 +96,7 @@ fn level_filter_event_with_target_and_span() { let (layer, handle) = layer::mock() .enter(cool_span.clone()) .event( - event::expect() + expect::event() .at_level(Level::DEBUG) .in_scope(vec![cool_span.clone()]), ) @@ -129,11 +129,11 @@ fn not_order_dependent() { let filter: EnvFilter = "stuff=debug,info".parse().expect("filter should parse"); let (layer, finished) = layer::mock() - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::DEBUG).with_target("stuff")) - .event(event::expect().at_level(Level::WARN).with_target("stuff")) - .event(event::expect().at_level(Level::ERROR)) - .event(event::expect().at_level(Level::ERROR).with_target("stuff")) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::DEBUG).with_target("stuff")) + .event(expect::event().at_level(Level::WARN).with_target("stuff")) + .event(expect::event().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::ERROR).with_target("stuff")) .only() .run_with_handle(); @@ -164,8 +164,8 @@ fn add_directive_enables_event() { filter = filter.add_directive("hello=trace".parse().expect("directive should parse")); let (layer, finished) = layer::mock() - .event(event::expect().at_level(Level::INFO).with_target("hello")) - .event(event::expect().at_level(Level::TRACE).with_target("hello")) + .event(expect::event().at_level(Level::INFO).with_target("hello")) + .event(expect::event().at_level(Level::TRACE).with_target("hello")) .only() .run_with_handle(); @@ -187,21 +187,21 @@ fn span_name_filter_is_dynamic() { let cool_span = span::named("cool_span"); let uncool_span = span::named("uncool_span"); let (layer, finished) = layer::mock() - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) .enter(cool_span.clone()) .event( - event::expect() + expect::event() .at_level(Level::DEBUG) .in_scope(vec![cool_span.clone()]), ) .enter(uncool_span.clone()) .event( - event::expect() + expect::event() .at_level(Level::WARN) .in_scope(vec![uncool_span.clone()]), ) .event( - event::expect() + expect::event() .at_level(Level::DEBUG) .in_scope(vec![uncool_span.clone()]), ) @@ -209,12 +209,12 @@ fn span_name_filter_is_dynamic() { .exit(cool_span) .enter(uncool_span.clone()) .event( - event::expect() + expect::event() .at_level(Level::WARN) .in_scope(vec![uncool_span.clone()]), ) .event( - event::expect() + expect::event() .at_level(Level::ERROR) .in_scope(vec![uncool_span.clone()]), ) @@ -260,7 +260,7 @@ fn multiple_dynamic_filters() { let (layer, handle) = layer::named("layer1") .enter(span.clone()) .event( - event::expect() + expect::event() .at_level(Level::DEBUG) .in_scope(vec![span.clone()]), ) @@ -276,7 +276,7 @@ fn multiple_dynamic_filters() { let (layer, handle) = layer::named("layer2") .enter(span.clone()) .event( - event::expect() + expect::event() .at_level(Level::INFO) .in_scope(vec![span.clone()]), ) diff --git a/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs b/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs index bbb68bd87a..a3ad351580 100644 --- a/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs +++ b/tracing-subscriber/tests/hinted_layer_filters_dont_break_other_layers.rs @@ -1,7 +1,7 @@ #![cfg(feature = "registry")] use tracing::{Level, Metadata, Subscriber}; use tracing_mock::{ - event, layer, + expect, layer, layer::MockLayer, subscriber::{self}, }; @@ -115,20 +115,20 @@ fn filter() -> DynFilterFn { fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } diff --git a/tracing-subscriber/tests/layer_filter_interests_are_cached.rs b/tracing-subscriber/tests/layer_filter_interests_are_cached.rs index c5f86c3cc9..867fdd0717 100644 --- a/tracing-subscriber/tests/layer_filter_interests_are_cached.rs +++ b/tracing-subscriber/tests/layer_filter_interests_are_cached.rs @@ -4,7 +4,7 @@ use std::{ sync::{Arc, Mutex}, }; use tracing::{Level, Subscriber}; -use tracing_mock::{event, layer}; +use tracing_mock::{expect, layer}; use tracing_subscriber::{filter, prelude::*}; #[test] @@ -21,8 +21,8 @@ fn layer_filter_interests_are_cached() { }); let (expect, handle) = layer::mock() - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); diff --git a/tracing-subscriber/tests/layer_filters/filter_scopes.rs b/tracing-subscriber/tests/layer_filters/filter_scopes.rs index b3957e9a30..02d000748f 100644 --- a/tracing-subscriber/tests/layer_filters/filter_scopes.rs +++ b/tracing-subscriber/tests/layer_filters/filter_scopes.rs @@ -1,48 +1,48 @@ use super::*; -use tracing_mock::layer::MockLayer; +use tracing_mock::{event, expect, layer::MockLayer}; #[test] fn filters_span_scopes() { let (debug_layer, debug_handle) = layer::named("debug") - .enter(span::expect().at_level(Level::DEBUG)) - .enter(span::expect().at_level(Level::INFO)) - .enter(span::expect().at_level(Level::WARN)) - .enter(span::expect().at_level(Level::ERROR)) + .enter(expect::span().at_level(Level::DEBUG)) + .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![ - span::expect().at_level(Level::ERROR), - span::expect().at_level(Level::WARN), - span::expect().at_level(Level::INFO), - span::expect().at_level(Level::DEBUG), + 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(span::expect().at_level(Level::ERROR)) - .exit(span::expect().at_level(Level::WARN)) - .exit(span::expect().at_level(Level::INFO)) - .exit(span::expect().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)) + .exit(expect::span().at_level(Level::DEBUG)) .only() .run_with_handle(); let (info_layer, info_handle) = layer::named("info") - .enter(span::expect().at_level(Level::INFO)) - .enter(span::expect().at_level(Level::WARN)) - .enter(span::expect().at_level(Level::ERROR)) + .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![ - span::expect().at_level(Level::ERROR), - span::expect().at_level(Level::WARN), - span::expect().at_level(Level::INFO), + expect::span().at_level(Level::ERROR), + expect::span().at_level(Level::WARN), + expect::span().at_level(Level::INFO), ])) - .exit(span::expect().at_level(Level::ERROR)) - .exit(span::expect().at_level(Level::WARN)) - .exit(span::expect().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)) .only() .run_with_handle(); let (warn_layer, warn_handle) = layer::named("warn") - .enter(span::expect().at_level(Level::WARN)) - .enter(span::expect().at_level(Level::ERROR)) + .enter(expect::span().at_level(Level::WARN)) + .enter(expect::span().at_level(Level::ERROR)) .event(event::msg("hello world").in_scope(vec![ - span::expect().at_level(Level::ERROR), - span::expect().at_level(Level::WARN), + expect::span().at_level(Level::ERROR), + expect::span().at_level(Level::WARN), ])) - .exit(span::expect().at_level(Level::ERROR)) - .exit(span::expect().at_level(Level::WARN)) + .exit(expect::span().at_level(Level::ERROR)) + .exit(expect::span().at_level(Level::WARN)) .only() .run_with_handle(); @@ -70,22 +70,22 @@ fn filters_span_scopes() { fn filters_interleaved_span_scopes() { fn target_layer(target: &'static str) -> (MockLayer, subscriber::MockHandle) { layer::named(format!("target_{}", target)) - .enter(span::expect().with_target(target)) - .enter(span::expect().with_target(target)) + .enter(expect::span().with_target(target)) + .enter(expect::span().with_target(target)) .event(event::msg("hello world").in_scope(vec![ - span::expect().with_target(target), - span::expect().with_target(target), + expect::span().with_target(target), + expect::span().with_target(target), ])) .event( event::msg("hello to my target") .in_scope(vec![ - span::expect().with_target(target), - span::expect().with_target(target), + expect::span().with_target(target), + expect::span().with_target(target), ]) .with_target(target), ) - .exit(span::expect().with_target(target)) - .exit(span::expect().with_target(target)) + .exit(expect::span().with_target(target)) + .exit(expect::span().with_target(target)) .only() .run_with_handle() } @@ -93,14 +93,14 @@ fn filters_interleaved_span_scopes() { let (a_layer, a_handle) = target_layer("a"); let (b_layer, b_handle) = target_layer("b"); let (all_layer, all_handle) = layer::named("all") - .enter(span::expect().with_target("b")) - .enter(span::expect().with_target("a")) + .enter(expect::span().with_target("b")) + .enter(expect::span().with_target("a")) .event(event::msg("hello world").in_scope(vec![ - span::expect().with_target("a"), - span::expect().with_target("b"), + expect::span().with_target("a"), + expect::span().with_target("b"), ])) - .exit(span::expect().with_target("a")) - .exit(span::expect().with_target("b")) + .exit(expect::span().with_target("a")) + .exit(expect::span().with_target("b")) .only() .run_with_handle(); diff --git a/tracing-subscriber/tests/layer_filters/main.rs b/tracing-subscriber/tests/layer_filters/main.rs index 5b8812f713..170074e8b3 100644 --- a/tracing-subscriber/tests/layer_filters/main.rs +++ b/tracing-subscriber/tests/layer_filters/main.rs @@ -8,26 +8,26 @@ mod trees; mod vec; use tracing::{level_filters::LevelFilter, Level}; -use tracing_mock::{event, layer, span, subscriber}; +use tracing_mock::{event, expect, layer, subscriber}; use tracing_subscriber::{filter, prelude::*, Layer}; #[test] fn basic_subscriber_filters() { let (trace_layer, trace_handle) = layer::named("trace") - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let (debug_layer, debug_handle) = layer::named("debug") - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let (info_layer, info_handle) = layer::named("info") - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); @@ -49,20 +49,20 @@ fn basic_subscriber_filters() { #[test] fn basic_subscriber_filters_spans() { let (trace_layer, trace_handle) = layer::named("trace") - .new_span(span::expect().at_level(Level::TRACE)) - .new_span(span::expect().at_level(Level::DEBUG)) - .new_span(span::expect().at_level(Level::INFO)) + .new_span(expect::span().at_level(Level::TRACE)) + .new_span(expect::span().at_level(Level::DEBUG)) + .new_span(expect::span().at_level(Level::INFO)) .only() .run_with_handle(); let (debug_layer, debug_handle) = layer::named("debug") - .new_span(span::expect().at_level(Level::DEBUG)) - .new_span(span::expect().at_level(Level::INFO)) + .new_span(expect::span().at_level(Level::DEBUG)) + .new_span(expect::span().at_level(Level::INFO)) .only() .run_with_handle(); let (info_layer, info_handle) = layer::named("info") - .new_span(span::expect().at_level(Level::INFO)) + .new_span(expect::span().at_level(Level::INFO)) .only() .run_with_handle(); @@ -84,9 +84,9 @@ fn basic_subscriber_filters_spans() { #[test] fn global_filters_subscribers_still_work() { let (expect, handle) = layer::mock() - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); @@ -107,8 +107,8 @@ fn global_filters_subscribers_still_work() { #[test] fn global_filter_interests_are_cached() { let (expect, handle) = layer::mock() - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); @@ -135,9 +135,9 @@ fn global_filter_interests_are_cached() { #[test] fn global_filters_affect_subscriber_filters() { let (expect, handle) = layer::named("debug") - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); diff --git a/tracing-subscriber/tests/layer_filters/per_event.rs b/tracing-subscriber/tests/layer_filters/per_event.rs index 9eb9855426..897c9f2df5 100644 --- a/tracing-subscriber/tests/layer_filters/per_event.rs +++ b/tracing-subscriber/tests/layer_filters/per_event.rs @@ -1,5 +1,5 @@ use tracing::Level; -use tracing_mock::{event, layer}; +use tracing_mock::{expect, layer}; use tracing_subscriber::{field::Visit, layer::Filter, prelude::*}; struct FilterEvent; @@ -42,8 +42,8 @@ impl Filter for FilterEvent { #[test] fn per_layer_event_field_filtering() { let (expect, handle) = layer::mock() - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); diff --git a/tracing-subscriber/tests/layer_filters/targets.rs b/tracing-subscriber/tests/layer_filters/targets.rs index 56f6448822..19bcff1489 100644 --- a/tracing-subscriber/tests/layer_filters/targets.rs +++ b/tracing-subscriber/tests/layer_filters/targets.rs @@ -1,4 +1,5 @@ use super::*; +use tracing_mock::event; use tracing_subscriber::{ filter::{filter_fn, Targets}, prelude::*, diff --git a/tracing-subscriber/tests/layer_filters/trees.rs b/tracing-subscriber/tests/layer_filters/trees.rs index 1418fc40fa..a0dbbbe907 100644 --- a/tracing-subscriber/tests/layer_filters/trees.rs +++ b/tracing-subscriber/tests/layer_filters/trees.rs @@ -1,11 +1,11 @@ use super::*; -use tracing_mock::layer::MockLayer; +use tracing_mock::{event, expect, layer::MockLayer}; #[test] fn basic_trees() { let (with_target, with_target_handle) = layer::named("info_with_target") .event( - event::expect() + expect::event() .at_level(Level::INFO) .with_target("my_target"), ) @@ -14,12 +14,12 @@ fn basic_trees() { let (info, info_handle) = layer::named("info") .event( - event::expect() + expect::event() .at_level(Level::INFO) .with_target(module_path!()), ) .event( - event::expect() + expect::event() .at_level(Level::INFO) .with_target("my_target"), ) @@ -28,18 +28,18 @@ fn basic_trees() { let (all, all_handle) = layer::named("all") .event( - event::expect() + expect::event() .at_level(Level::INFO) .with_target(module_path!()), ) - .event(event::expect().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::TRACE)) .event( - event::expect() + expect::event() .at_level(Level::INFO) .with_target("my_target"), ) .event( - event::expect() + expect::event() .at_level(Level::TRACE) .with_target("my_target"), ) @@ -69,11 +69,11 @@ fn basic_trees() { fn filter_span_scopes() { fn target_layer(target: &'static str) -> (MockLayer, subscriber::MockHandle) { layer::named(format!("target_{}", target)) - .enter(span::expect().with_target(target).at_level(Level::INFO)) + .enter(expect::span().with_target(target).at_level(Level::INFO)) .event(event::msg("hello world").in_scope(vec![ - span::expect().with_target(target).at_level(Level::INFO), + expect::span().with_target(target).at_level(Level::INFO), ])) - .exit(span::expect().with_target(target).at_level(Level::INFO)) + .exit(expect::span().with_target(target).at_level(Level::INFO)) .only() .run_with_handle() } @@ -81,28 +81,28 @@ fn filter_span_scopes() { let (a_layer, a_handle) = target_layer("a"); let (b_layer, b_handle) = target_layer("b"); let (info_layer, info_handle) = layer::named("info") - .enter(span::expect().with_target("b").at_level(Level::INFO)) - .enter(span::expect().with_target("a").at_level(Level::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![ - span::expect().with_target("a").at_level(Level::INFO), - span::expect().with_target("b").at_level(Level::INFO), + expect::span().with_target("a").at_level(Level::INFO), + expect::span().with_target("b").at_level(Level::INFO), ])) - .exit(span::expect().with_target("a").at_level(Level::INFO)) - .exit(span::expect().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() .run_with_handle(); let full_scope = vec![ - span::expect().with_target("b").at_level(Level::TRACE), - span::expect().with_target("a").at_level(Level::INFO), - span::expect().with_target("b").at_level(Level::INFO), - span::expect().with_target("a").at_level(Level::TRACE), + expect::span().with_target("b").at_level(Level::TRACE), + expect::span().with_target("a").at_level(Level::INFO), + expect::span().with_target("b").at_level(Level::INFO), + expect::span().with_target("a").at_level(Level::TRACE), ]; let (all_layer, all_handle) = layer::named("all") - .enter(span::expect().with_target("a").at_level(Level::TRACE)) - .enter(span::expect().with_target("b").at_level(Level::INFO)) - .enter(span::expect().with_target("a").at_level(Level::INFO)) - .enter(span::expect().with_target("b").at_level(Level::TRACE)) + .enter(expect::span().with_target("a").at_level(Level::TRACE)) + .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") @@ -114,10 +114,10 @@ fn filter_span_scopes() { .with_target("b") .in_scope(full_scope), ) - .exit(span::expect().with_target("b").at_level(Level::TRACE)) - .exit(span::expect().with_target("a").at_level(Level::INFO)) - .exit(span::expect().with_target("b").at_level(Level::INFO)) - .exit(span::expect().with_target("a").at_level(Level::TRACE)) + .exit(expect::span().with_target("b").at_level(Level::TRACE)) + .exit(expect::span().with_target("a").at_level(Level::INFO)) + .exit(expect::span().with_target("b").at_level(Level::INFO)) + .exit(expect::span().with_target("a").at_level(Level::TRACE)) .only() .run_with_handle(); diff --git a/tracing-subscriber/tests/layer_filters/vec.rs b/tracing-subscriber/tests/layer_filters/vec.rs index 884ec60a95..3555e19545 100644 --- a/tracing-subscriber/tests/layer_filters/vec.rs +++ b/tracing-subscriber/tests/layer_filters/vec.rs @@ -1,26 +1,26 @@ use super::*; use tracing::Subscriber; -use tracing_mock::layer::MockLayer; +use tracing_mock::{expect, layer::MockLayer}; #[test] fn with_filters_unboxed() { let (trace_layer, trace_handle) = layer::named("trace") - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let trace_layer = trace_layer.with_filter(LevelFilter::TRACE); let (debug_layer, debug_handle) = layer::named("debug") - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG); let (info_layer, info_handle) = layer::named("info") - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let info_layer = info_layer.with_filter(LevelFilter::INFO); @@ -41,22 +41,22 @@ fn with_filters_unboxed() { #[test] fn with_filters_boxed() { let (unfiltered_layer, unfiltered_handle) = layer::named("unfiltered") - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let unfiltered_layer = unfiltered_layer.boxed(); let (debug_layer, debug_handle) = layer::named("debug") - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG).boxed(); let (target_layer, target_handle) = layer::named("target") - .event(event::expect().at_level(Level::INFO)) + .event(expect::event().at_level(Level::INFO)) .only() .run_with_handle(); let target_layer = target_layer diff --git a/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs b/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs index 6f9ee0c310..553c5371a2 100644 --- a/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs +++ b/tracing-subscriber/tests/multiple_layer_filter_interests_cached.rs @@ -4,7 +4,7 @@ use std::{ sync::{Arc, Mutex}, }; use tracing::{Level, Subscriber}; -use tracing_mock::{event, layer}; +use tracing_mock::{expect, layer}; use tracing_subscriber::{filter, prelude::*}; #[test] @@ -23,12 +23,12 @@ fn multiple_layer_filter_interests_are_cached() { let seen_info = seen_info2; let (info_layer, info_handle) = layer::named("info") - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); let info_layer = info_layer.with_filter(filter); @@ -47,10 +47,10 @@ fn multiple_layer_filter_interests_are_cached() { let seen_warn = seen_warn2; let (warn_layer, warn_handle) = layer::named("warn") - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle(); let warn_layer = warn_layer.with_filter(filter); diff --git a/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs b/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs index 9078b66b35..a0f3b51423 100644 --- a/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs +++ b/tracing-subscriber/tests/unhinted_layer_filters_dont_break_other_layers.rs @@ -1,7 +1,7 @@ #![cfg(feature = "registry")] use tracing::Level; use tracing_mock::{ - event, + expect, layer::{self, MockLayer}, subscriber, }; @@ -107,20 +107,20 @@ fn filter() -> DynFilterFn { fn unfiltered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::TRACE)) - .event(event::expect().at_level(Level::DEBUG)) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::TRACE)) + .event(expect::event().at_level(Level::DEBUG)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } fn filtered(name: &str) -> (MockLayer, subscriber::MockHandle) { layer::named(name) - .event(event::expect().at_level(Level::INFO)) - .event(event::expect().at_level(Level::WARN)) - .event(event::expect().at_level(Level::ERROR)) + .event(expect::event().at_level(Level::INFO)) + .event(expect::event().at_level(Level::WARN)) + .event(expect::event().at_level(Level::ERROR)) .only() .run_with_handle() } diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 3a85d1b4f0..d8a98a6163 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -502,10 +502,10 @@ fn constant_field_name() { #[test] fn string_field() { let (subscriber, handle) = subscriber::mock() - .event(event::expect().with_fields(field::expect("my_string").with_value(&"hello").only())) + .event(expect::event().with_fields(expect::field("my_string").with_value(&"hello").only())) .event( - event::expect().with_fields( - field::expect("my_string") + expect::event().with_fields( + expect::field("my_string") .with_value(&"hello world!") .only(), ), diff --git a/tracing/tests/span.rs b/tracing/tests/span.rs index b1d6d63bfa..0431d3a1f3 100644 --- a/tracing/tests/span.rs +++ b/tracing/tests/span.rs @@ -341,10 +341,10 @@ fn entered_api() { #[test] fn entered() { let (subscriber, handle) = subscriber::mock() - .enter(span::expect().named("foo")) - .event(event::expect()) - .exit(span::expect().named("foo")) - .drop_span(span::expect().named("foo")) + .enter(expect::span().named("foo")) + .event(expect::event()) + .exit(expect::span().named("foo")) + .drop_span(expect::span().named("foo")) .only() .run_with_handle(); with_default(subscriber, || { @@ -359,10 +359,10 @@ fn entered() { #[test] fn entered_api() { let (subscriber, handle) = subscriber::mock() - .enter(span::expect().named("foo")) - .event(event::expect()) - .exit(span::expect().named("foo")) - .drop_span(span::expect().named("foo")) + .enter(expect::span().named("foo")) + .event(expect::event()) + .exit(expect::span().named("foo")) + .drop_span(expect::span().named("foo")) .only() .run_with_handle(); with_default(subscriber, || { @@ -528,7 +528,7 @@ fn float_values() { fn add_field_after_new_span() { let (collector, handle) = subscriber::mock() .new_span( - span::expect() + expect::span() .named("foo") .with_fields(expect::field("bar").with_value(&5) .and(expect::field("baz").with_value).only()), diff --git a/tracing/tests/subscriber.rs b/tracing/tests/subscriber.rs index 0357d61ac7..f676efeee8 100644 --- a/tracing/tests/subscriber.rs +++ b/tracing/tests/subscriber.rs @@ -11,8 +11,7 @@ use tracing::{ subscriber::{with_default, Interest, Subscriber}, Event, Level, Metadata, }; - -use tracing_mock::*; +use tracing_mock::{expect, subscriber}; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[test] @@ -61,15 +60,15 @@ fn event_macros_dont_infinite_loop() { fn boxed_subscriber() { let (subscriber, handle) = subscriber::mock() .new_span( - span::expect().named("foo").with_field( - field::expect("bar") + expect::span().named("foo").with_field( + expect::field("bar") .with_value(&display("hello from my span")) .only(), ), ) - .enter(span::expect().named("foo")) - .exit(span::expect().named("foo")) - .drop_span(span::expect().named("foo")) + .enter(expect::span().named("foo")) + .exit(expect::span().named("foo")) + .drop_span(expect::span().named("foo")) .only() .run_with_handle(); let subscriber: Box = Box::new(subscriber); @@ -94,18 +93,18 @@ fn arced_subscriber() { let (subscriber, handle) = subscriber::mock() .new_span( - span::expect().named("foo").with_field( - field::expect("bar") + expect::span().named("foo").with_field( + expect::field("bar") .with_value(&display("hello from my span")) .only(), ), ) - .enter(span::expect().named("foo")) - .exit(span::expect().named("foo")) - .drop_span(span::expect().named("foo")) + .enter(expect::span().named("foo")) + .exit(expect::span().named("foo")) + .drop_span(expect::span().named("foo")) .event( - event::expect() - .with_fields(field::expect("message").with_value(&display("hello from my event"))), + expect::event() + .with_fields(expect::field("message").with_value(&display("hello from my event"))), ) .only() .run_with_handle();