1use tracing::subscriber::with_default;
2use tracing_attributes::instrument;
3use tracing_mock::*;
4
5#[instrument]
6fn default_target() {}
7
8#[instrument(target = "my_target")]
9fn custom_target() {}
10
11mod my_mod {
12 use tracing_attributes::instrument;
13
14 pub const MODULE_PATH: &str = module_path!();
15
16 #[instrument]
17 pub fn default_target() {}
18
19 #[instrument(target = "my_other_target")]
20 pub fn custom_target() {}
21}
22
23#[test]
24fn default_targets() {
25 let (subscriber, handle) = subscriber::mock()
26 .new_span(
27 expect::span()
28 .named("default_target")
29 .with_target(module_path!()),
30 )
31 .enter(
32 expect::span()
33 .named("default_target")
34 .with_target(module_path!()),
35 )
36 .exit(
37 expect::span()
38 .named("default_target")
39 .with_target(module_path!()),
40 )
41 .new_span(
42 expect::span()
43 .named("default_target")
44 .with_target(my_mod::MODULE_PATH),
45 )
46 .enter(
47 expect::span()
48 .named("default_target")
49 .with_target(my_mod::MODULE_PATH),
50 )
51 .exit(
52 expect::span()
53 .named("default_target")
54 .with_target(my_mod::MODULE_PATH),
55 )
56 .only()
57 .run_with_handle();
58
59 with_default(subscriber, || {
60 default_target();
61 my_mod::default_target();
62 });
63
64 handle.assert_finished();
65}
66
67#[test]
68fn custom_targets() {
69 let (subscriber, handle) = subscriber::mock()
70 .new_span(
71 expect::span()
72 .named("custom_target")
73 .with_target("my_target"),
74 )
75 .enter(
76 expect::span()
77 .named("custom_target")
78 .with_target("my_target"),
79 )
80 .exit(
81 expect::span()
82 .named("custom_target")
83 .with_target("my_target"),
84 )
85 .new_span(
86 expect::span()
87 .named("custom_target")
88 .with_target("my_other_target"),
89 )
90 .enter(
91 expect::span()
92 .named("custom_target")
93 .with_target("my_other_target"),
94 )
95 .exit(
96 expect::span()
97 .named("custom_target")
98 .with_target("my_other_target"),
99 )
100 .only()
101 .run_with_handle();
102
103 with_default(subscriber, || {
104 custom_target();
105 my_mod::custom_target();
106 });
107
108 handle.assert_finished();
109}
110