Skip to content

Commit

Permalink
mock: change helper functions to expect::<thing>
Browse files Browse the repository at this point in the history
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: #539
  • Loading branch information
hds committed Nov 11, 2022
1 parent 2786be7 commit 4de00e0
Show file tree
Hide file tree
Showing 44 changed files with 783 additions and 784 deletions.
70 changes: 35 additions & 35 deletions tracing-attributes/tests/async_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ fn repro_1831_2() -> impl Future<Output = Result<(), Infallible>> {
#[test]
fn async_fn_only_enters_for_polls() {
let (collector, handle) = collector::mock()
.new_span(span::expect().named("test_async_fn"))
.enter(span::expect().named("test_async_fn"))
.event(event::expect().with_fields(field::expect("awaiting").with_value(&true)))
.exit(span::expect().named("test_async_fn"))
.enter(span::expect().named("test_async_fn"))
.exit(span::expect().named("test_async_fn"))
.drop_span(span::expect().named("test_async_fn"))
.new_span(expect::span().named("test_async_fn"))
.enter(expect::span().named("test_async_fn"))
.event(expect::event().with_fields(expect::field("awaiting").with_value(&true)))
.exit(expect::span().named("test_async_fn"))
.enter(expect::span().named("test_async_fn"))
.exit(expect::span().named("test_async_fn"))
.drop_span(expect::span().named("test_async_fn"))
.only()
.run_with_handle();
with_default(collector, || {
Expand All @@ -111,14 +111,14 @@ fn async_fn_nested() {
tracing::trace!(nested = true);
}

let span = span::expect().named("test_async_fns_nested");
let span2 = span::expect().named("test_async_fns_nested_other");
let span = expect::span().named("test_async_fns_nested");
let span2 = expect::span().named("test_async_fns_nested_other");
let (collector, handle) = collector::mock()
.new_span(span.clone())
.enter(span.clone())
.new_span(span2.clone())
.enter(span2.clone())
.event(event::expect().with_fields(field::expect("nested").with_value(&true)))
.event(expect::event().with_fields(expect::field("nested").with_value(&true)))
.exit(span2.clone())
.drop_span(span2)
.exit(span.clone())
Expand Down Expand Up @@ -185,24 +185,24 @@ fn async_fn_with_async_trait() {
}
}

let span = span::expect().named("foo");
let span2 = span::expect().named("bar");
let span3 = span::expect().named("baz");
let span = expect::span().named("foo");
let span2 = expect::span().named("bar");
let span3 = expect::span().named("baz");
let (collector, handle) = collector::mock()
.new_span(
span.clone()
.with_field(field::expect("self"))
.with_field(field::expect("v")),
.with_field(expect::field("self"))
.with_field(expect::field("v")),
)
.enter(span.clone())
.new_span(span3.clone())
.enter(span3.clone())
.event(event::expect().with_fields(field::expect("val").with_value(&2u64)))
.event(expect::event().with_fields(expect::field("val").with_value(&2u64)))
.exit(span3.clone())
.drop_span(span3)
.new_span(span2.clone().with_field(field::expect("self")))
.new_span(span2.clone().with_field(expect::field("self")))
.enter(span2.clone())
.event(event::expect().with_fields(field::expect("val").with_value(&5u64)))
.event(expect::event().with_fields(expect::field("val").with_value(&5u64)))
.exit(span2.clone())
.drop_span(span2)
.exit(span.clone())
Expand Down Expand Up @@ -243,15 +243,15 @@ fn async_fn_with_async_trait_and_fields_expressions() {
async fn call(&mut self, _v: usize) {}
}

let span = span::expect().named("call");
let span = expect::span().named("call");
let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("_v")
expect::field("_v")
.with_value(&5usize)
.and(field::expect("test").with_value(&tracing::field::debug(10)))
.and(field::expect("val").with_value(&42u64))
.and(field::expect("val2").with_value(&42u64)),
.and(expect::field("test").with_value(&tracing::field::debug(10)))
.and(expect::field("val").with_value(&42u64))
.and(expect::field("val2").with_value(&42u64)),
),
)
.enter(span.clone())
Expand Down Expand Up @@ -309,26 +309,26 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
}

//let span = span::mock().named("call");
let span2 = span::expect().named("call_with_self");
let span3 = span::expect().named("call_with_mut_self");
let span4 = span::expect().named("sync_fun");
let span2 = expect::span().named("call_with_self");
let span3 = expect::span().named("call_with_mut_self");
let span4 = expect::span().named("sync_fun");
let (collector, handle) = collector::mock()
/*.new_span(span.clone()
.with_field(
field::expect("Self").with_value(&"TestImpler")))
expect::field("Self").with_value(&"TestImpler")))
.enter(span.clone())
.exit(span.clone())
.drop_span(span)*/
.new_span(
span2
.clone()
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
)
.enter(span2.clone())
.new_span(
span4
.clone()
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
)
.enter(span4.clone())
.exit(span4)
Expand All @@ -337,7 +337,7 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
.new_span(
span3
.clone()
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
)
.enter(span3.clone())
.exit(span3.clone())
Expand Down Expand Up @@ -377,7 +377,7 @@ fn out_of_scope_fields() {
}
}

let span = span::expect().named("call");
let span = expect::span().named("call");
let (collector, handle) = collector::mock()
.new_span(span.clone())
.enter(span.clone())
Expand Down Expand Up @@ -408,8 +408,8 @@ fn manual_impl_future() {
}
}

let span = span::expect().named("manual_impl_future");
let poll_event = || event::expect().with_fields(field::expect("poll").with_value(&true));
let span = expect::span().named("manual_impl_future");
let poll_event = || expect::event().with_fields(expect::field("poll").with_value(&true));

let (collector, handle) = collector::mock()
// await manual_impl_future
Expand Down Expand Up @@ -439,8 +439,8 @@ fn manual_box_pin() {
})
}

let span = span::expect().named("manual_box_pin");
let poll_event = || event::expect().with_fields(field::expect("poll").with_value(&true));
let span = expect::span().named("manual_box_pin");
let poll_event = || expect::event().with_fields(expect::field("poll").with_value(&true));

let (collector, handle) = collector::mock()
// await manual_box_pin
Expand Down
42 changes: 21 additions & 21 deletions tracing-attributes/tests/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ fn destructure_tuples() {
#[instrument]
fn my_fn((arg1, arg2): (usize, usize)) {}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("arg1")
expect::field("arg1")
.with_value(&format_args!("1"))
.and(field::expect("arg2").with_value(&format_args!("2")))
.and(expect::field("arg2").with_value(&format_args!("2")))
.only(),
),
)
Expand All @@ -36,16 +36,16 @@ fn destructure_nested_tuples() {
#[instrument]
fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("arg1")
expect::field("arg1")
.with_value(&format_args!("1"))
.and(field::expect("arg2").with_value(&format_args!("2")))
.and(field::expect("arg3").with_value(&format_args!("3")))
.and(field::expect("arg4").with_value(&format_args!("4")))
.and(expect::field("arg2").with_value(&format_args!("2")))
.and(expect::field("arg3").with_value(&format_args!("3")))
.and(expect::field("arg4").with_value(&format_args!("4")))
.only(),
),
)
Expand All @@ -67,12 +67,12 @@ fn destructure_refs() {
#[instrument]
fn my_fn(&arg1: &usize) {}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone()
.with_field(field::expect("arg1").with_value(&1usize).only()),
.with_field(expect::field("arg1").with_value(&1usize).only()),
)
.enter(span.clone())
.exit(span.clone())
Expand All @@ -94,14 +94,14 @@ fn destructure_tuple_structs() {
#[instrument]
fn my_fn(Foo(arg1, arg2): Foo) {}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("arg1")
expect::field("arg1")
.with_value(&format_args!("1"))
.and(field::expect("arg2").with_value(&format_args!("2")))
.and(expect::field("arg2").with_value(&format_args!("2")))
.only(),
),
)
Expand Down Expand Up @@ -135,14 +135,14 @@ fn destructure_structs() {
let _ = (arg1, arg2);
}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("arg1")
expect::field("arg1")
.with_value(&format_args!("1"))
.and(field::expect("arg2").with_value(&format_args!("2")))
.and(expect::field("arg2").with_value(&format_args!("2")))
.only(),
),
)
Expand Down Expand Up @@ -180,16 +180,16 @@ fn destructure_everything() {
let _ = (arg1, arg2, arg3, arg4);
}

let span = span::expect().named("my_fn");
let span = expect::span().named("my_fn");

let (collector, handle) = collector::mock()
.new_span(
span.clone().with_field(
field::expect("arg1")
expect::field("arg1")
.with_value(&format_args!("1"))
.and(field::expect("arg2").with_value(&format_args!("2")))
.and(field::expect("arg3").with_value(&format_args!("3")))
.and(field::expect("arg4").with_value(&format_args!("4")))
.and(expect::field("arg2").with_value(&format_args!("2")))
.and(expect::field("arg3").with_value(&format_args!("3")))
.and(expect::field("arg4").with_value(&format_args!("4")))
.only(),
),
)
Expand Down
Loading

0 comments on commit 4de00e0

Please sign in to comment.