Skip to content

Commit

Permalink
mock: change helper functions to expect::<thing> (tokio-rs#2377)
Browse files Browse the repository at this point in the history
* mock: change helper functions to `expect::<thing>`

The current format of test expectations in `tracing-mock` isn't ideal.
The format `span::expect` requires importing `tracing_mock::<thing>` 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: tokio-rs#539
  • Loading branch information
hds authored and kaffarell committed May 22, 2024
1 parent 2e208ad commit c6546d3
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 252 deletions.
4 changes: 2 additions & 2 deletions tracing-attributes/tests/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ fn err_early_return() -> Result<u8, TryFromIntError> {

#[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()
Expand Down
30 changes: 15 additions & 15 deletions tracing-attributes/tests/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
18 changes: 9 additions & 9 deletions tracing-attributes/tests/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ 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");

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)
.with_target("my_target"),
)
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down
21 changes: 0 additions & 21 deletions tracing-mock/src/expectation.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tracing-mock/src/layer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(missing_docs, dead_code)]
use crate::{
event::ExpectedEvent,
expectation::Expect,
expect::Expect,
field::ExpectedFields,
span::{ExpectedSpan, NewSpan},
subscriber::MockHandle,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "registry")]
use tracing::Level;
use tracing_mock::{
event,
expect,
layer::{self, MockLayer},
subscriber,
};
Expand Down Expand Up @@ -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()
}
58 changes: 29 additions & 29 deletions tracing-subscriber/tests/env_filter/per_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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()]),
)
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand All @@ -187,34 +187,34 @@ 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()]),
)
.exit(uncool_span.clone())
.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()]),
)
Expand Down Expand Up @@ -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()]),
)
Expand All @@ -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()]),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "registry")]
use tracing::{Level, Metadata, Subscriber};
use tracing_mock::{
event, layer,
expect, layer,
layer::MockLayer,
subscriber::{self},
};
Expand Down Expand Up @@ -115,20 +115,20 @@ fn filter<S>() -> DynFilterFn<S> {

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()
}
6 changes: 3 additions & 3 deletions tracing-subscriber/tests/layer_filter_interests_are_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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();

Expand Down
Loading

0 comments on commit c6546d3

Please sign in to comment.