Skip to content

Commit 22c1ed9

Browse files
authored
mock: change helper functions to expect::<thing> (#2377)
* 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: #539
1 parent 2786be7 commit 22c1ed9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+783
-784
lines changed

tracing-attributes/tests/async_fn.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ fn repro_1831_2() -> impl Future<Output = Result<(), Infallible>> {
8484
#[test]
8585
fn async_fn_only_enters_for_polls() {
8686
let (collector, handle) = collector::mock()
87-
.new_span(span::expect().named("test_async_fn"))
88-
.enter(span::expect().named("test_async_fn"))
89-
.event(event::expect().with_fields(field::expect("awaiting").with_value(&true)))
90-
.exit(span::expect().named("test_async_fn"))
91-
.enter(span::expect().named("test_async_fn"))
92-
.exit(span::expect().named("test_async_fn"))
93-
.drop_span(span::expect().named("test_async_fn"))
87+
.new_span(expect::span().named("test_async_fn"))
88+
.enter(expect::span().named("test_async_fn"))
89+
.event(expect::event().with_fields(expect::field("awaiting").with_value(&true)))
90+
.exit(expect::span().named("test_async_fn"))
91+
.enter(expect::span().named("test_async_fn"))
92+
.exit(expect::span().named("test_async_fn"))
93+
.drop_span(expect::span().named("test_async_fn"))
9494
.only()
9595
.run_with_handle();
9696
with_default(collector, || {
@@ -111,14 +111,14 @@ fn async_fn_nested() {
111111
tracing::trace!(nested = true);
112112
}
113113

114-
let span = span::expect().named("test_async_fns_nested");
115-
let span2 = span::expect().named("test_async_fns_nested_other");
114+
let span = expect::span().named("test_async_fns_nested");
115+
let span2 = expect::span().named("test_async_fns_nested_other");
116116
let (collector, handle) = collector::mock()
117117
.new_span(span.clone())
118118
.enter(span.clone())
119119
.new_span(span2.clone())
120120
.enter(span2.clone())
121-
.event(event::expect().with_fields(field::expect("nested").with_value(&true)))
121+
.event(expect::event().with_fields(expect::field("nested").with_value(&true)))
122122
.exit(span2.clone())
123123
.drop_span(span2)
124124
.exit(span.clone())
@@ -185,24 +185,24 @@ fn async_fn_with_async_trait() {
185185
}
186186
}
187187

188-
let span = span::expect().named("foo");
189-
let span2 = span::expect().named("bar");
190-
let span3 = span::expect().named("baz");
188+
let span = expect::span().named("foo");
189+
let span2 = expect::span().named("bar");
190+
let span3 = expect::span().named("baz");
191191
let (collector, handle) = collector::mock()
192192
.new_span(
193193
span.clone()
194-
.with_field(field::expect("self"))
195-
.with_field(field::expect("v")),
194+
.with_field(expect::field("self"))
195+
.with_field(expect::field("v")),
196196
)
197197
.enter(span.clone())
198198
.new_span(span3.clone())
199199
.enter(span3.clone())
200-
.event(event::expect().with_fields(field::expect("val").with_value(&2u64)))
200+
.event(expect::event().with_fields(expect::field("val").with_value(&2u64)))
201201
.exit(span3.clone())
202202
.drop_span(span3)
203-
.new_span(span2.clone().with_field(field::expect("self")))
203+
.new_span(span2.clone().with_field(expect::field("self")))
204204
.enter(span2.clone())
205-
.event(event::expect().with_fields(field::expect("val").with_value(&5u64)))
205+
.event(expect::event().with_fields(expect::field("val").with_value(&5u64)))
206206
.exit(span2.clone())
207207
.drop_span(span2)
208208
.exit(span.clone())
@@ -243,15 +243,15 @@ fn async_fn_with_async_trait_and_fields_expressions() {
243243
async fn call(&mut self, _v: usize) {}
244244
}
245245

246-
let span = span::expect().named("call");
246+
let span = expect::span().named("call");
247247
let (collector, handle) = collector::mock()
248248
.new_span(
249249
span.clone().with_field(
250-
field::expect("_v")
250+
expect::field("_v")
251251
.with_value(&5usize)
252-
.and(field::expect("test").with_value(&tracing::field::debug(10)))
253-
.and(field::expect("val").with_value(&42u64))
254-
.and(field::expect("val2").with_value(&42u64)),
252+
.and(expect::field("test").with_value(&tracing::field::debug(10)))
253+
.and(expect::field("val").with_value(&42u64))
254+
.and(expect::field("val2").with_value(&42u64)),
255255
),
256256
)
257257
.enter(span.clone())
@@ -309,26 +309,26 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
309309
}
310310

311311
//let span = span::mock().named("call");
312-
let span2 = span::expect().named("call_with_self");
313-
let span3 = span::expect().named("call_with_mut_self");
314-
let span4 = span::expect().named("sync_fun");
312+
let span2 = expect::span().named("call_with_self");
313+
let span3 = expect::span().named("call_with_mut_self");
314+
let span4 = expect::span().named("sync_fun");
315315
let (collector, handle) = collector::mock()
316316
/*.new_span(span.clone()
317317
.with_field(
318-
field::expect("Self").with_value(&"TestImpler")))
318+
expect::field("Self").with_value(&"TestImpler")))
319319
.enter(span.clone())
320320
.exit(span.clone())
321321
.drop_span(span)*/
322322
.new_span(
323323
span2
324324
.clone()
325-
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
325+
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
326326
)
327327
.enter(span2.clone())
328328
.new_span(
329329
span4
330330
.clone()
331-
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
331+
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
332332
)
333333
.enter(span4.clone())
334334
.exit(span4)
@@ -337,7 +337,7 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
337337
.new_span(
338338
span3
339339
.clone()
340-
.with_field(field::expect("Self").with_value(&std::any::type_name::<TestImpl>())),
340+
.with_field(expect::field("Self").with_value(&std::any::type_name::<TestImpl>())),
341341
)
342342
.enter(span3.clone())
343343
.exit(span3.clone())
@@ -377,7 +377,7 @@ fn out_of_scope_fields() {
377377
}
378378
}
379379

380-
let span = span::expect().named("call");
380+
let span = expect::span().named("call");
381381
let (collector, handle) = collector::mock()
382382
.new_span(span.clone())
383383
.enter(span.clone())
@@ -408,8 +408,8 @@ fn manual_impl_future() {
408408
}
409409
}
410410

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

414414
let (collector, handle) = collector::mock()
415415
// await manual_impl_future
@@ -439,8 +439,8 @@ fn manual_box_pin() {
439439
})
440440
}
441441

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

445445
let (collector, handle) = collector::mock()
446446
// await manual_box_pin

tracing-attributes/tests/destructuring.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ fn destructure_tuples() {
77
#[instrument]
88
fn my_fn((arg1, arg2): (usize, usize)) {}
99

10-
let span = span::expect().named("my_fn");
10+
let span = expect::span().named("my_fn");
1111

1212
let (collector, handle) = collector::mock()
1313
.new_span(
1414
span.clone().with_field(
15-
field::expect("arg1")
15+
expect::field("arg1")
1616
.with_value(&format_args!("1"))
17-
.and(field::expect("arg2").with_value(&format_args!("2")))
17+
.and(expect::field("arg2").with_value(&format_args!("2")))
1818
.only(),
1919
),
2020
)
@@ -36,16 +36,16 @@ fn destructure_nested_tuples() {
3636
#[instrument]
3737
fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {}
3838

39-
let span = span::expect().named("my_fn");
39+
let span = expect::span().named("my_fn");
4040

4141
let (collector, handle) = collector::mock()
4242
.new_span(
4343
span.clone().with_field(
44-
field::expect("arg1")
44+
expect::field("arg1")
4545
.with_value(&format_args!("1"))
46-
.and(field::expect("arg2").with_value(&format_args!("2")))
47-
.and(field::expect("arg3").with_value(&format_args!("3")))
48-
.and(field::expect("arg4").with_value(&format_args!("4")))
46+
.and(expect::field("arg2").with_value(&format_args!("2")))
47+
.and(expect::field("arg3").with_value(&format_args!("3")))
48+
.and(expect::field("arg4").with_value(&format_args!("4")))
4949
.only(),
5050
),
5151
)
@@ -67,12 +67,12 @@ fn destructure_refs() {
6767
#[instrument]
6868
fn my_fn(&arg1: &usize) {}
6969

70-
let span = span::expect().named("my_fn");
70+
let span = expect::span().named("my_fn");
7171

7272
let (collector, handle) = collector::mock()
7373
.new_span(
7474
span.clone()
75-
.with_field(field::expect("arg1").with_value(&1usize).only()),
75+
.with_field(expect::field("arg1").with_value(&1usize).only()),
7676
)
7777
.enter(span.clone())
7878
.exit(span.clone())
@@ -94,14 +94,14 @@ fn destructure_tuple_structs() {
9494
#[instrument]
9595
fn my_fn(Foo(arg1, arg2): Foo) {}
9696

97-
let span = span::expect().named("my_fn");
97+
let span = expect::span().named("my_fn");
9898

9999
let (collector, handle) = collector::mock()
100100
.new_span(
101101
span.clone().with_field(
102-
field::expect("arg1")
102+
expect::field("arg1")
103103
.with_value(&format_args!("1"))
104-
.and(field::expect("arg2").with_value(&format_args!("2")))
104+
.and(expect::field("arg2").with_value(&format_args!("2")))
105105
.only(),
106106
),
107107
)
@@ -135,14 +135,14 @@ fn destructure_structs() {
135135
let _ = (arg1, arg2);
136136
}
137137

138-
let span = span::expect().named("my_fn");
138+
let span = expect::span().named("my_fn");
139139

140140
let (collector, handle) = collector::mock()
141141
.new_span(
142142
span.clone().with_field(
143-
field::expect("arg1")
143+
expect::field("arg1")
144144
.with_value(&format_args!("1"))
145-
.and(field::expect("arg2").with_value(&format_args!("2")))
145+
.and(expect::field("arg2").with_value(&format_args!("2")))
146146
.only(),
147147
),
148148
)
@@ -180,16 +180,16 @@ fn destructure_everything() {
180180
let _ = (arg1, arg2, arg3, arg4);
181181
}
182182

183-
let span = span::expect().named("my_fn");
183+
let span = expect::span().named("my_fn");
184184

185185
let (collector, handle) = collector::mock()
186186
.new_span(
187187
span.clone().with_field(
188-
field::expect("arg1")
188+
expect::field("arg1")
189189
.with_value(&format_args!("1"))
190-
.and(field::expect("arg2").with_value(&format_args!("2")))
191-
.and(field::expect("arg3").with_value(&format_args!("3")))
192-
.and(field::expect("arg4").with_value(&format_args!("4")))
190+
.and(expect::field("arg2").with_value(&format_args!("2")))
191+
.and(expect::field("arg3").with_value(&format_args!("3")))
192+
.and(expect::field("arg4").with_value(&format_args!("4")))
193193
.only(),
194194
),
195195
)

0 commit comments

Comments
 (0)