1 | use tracing::subscriber::with_default; |
2 | use tracing_attributes::instrument; |
3 | use tracing_mock::*; |
4 | |
5 | #[test] |
6 | fn destructure_tuples() { |
7 | #[instrument ] |
8 | fn my_fn((arg1, arg2): (usize, usize)) {} |
9 | |
10 | let span = expect::span().named("my_fn" ); |
11 | |
12 | let (subscriber, handle) = subscriber::mock() |
13 | .new_span( |
14 | span.clone().with_field( |
15 | expect::field("arg1" ) |
16 | .with_value(&format_args!("1" )) |
17 | .and(expect::field("arg2" ).with_value(&format_args!("2" ))) |
18 | .only(), |
19 | ), |
20 | ) |
21 | .enter(span.clone()) |
22 | .exit(span.clone()) |
23 | .drop_span(span) |
24 | .only() |
25 | .run_with_handle(); |
26 | |
27 | with_default(subscriber, || { |
28 | my_fn((1, 2)); |
29 | }); |
30 | |
31 | handle.assert_finished(); |
32 | } |
33 | |
34 | #[test] |
35 | fn destructure_nested_tuples() { |
36 | #[instrument ] |
37 | fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {} |
38 | |
39 | let span = expect::span().named("my_fn" ); |
40 | |
41 | let (subscriber, handle) = subscriber::mock() |
42 | .new_span( |
43 | span.clone().with_field( |
44 | expect::field("arg1" ) |
45 | .with_value(&format_args!("1" )) |
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" ))) |
49 | .only(), |
50 | ), |
51 | ) |
52 | .enter(span.clone()) |
53 | .exit(span.clone()) |
54 | .drop_span(span) |
55 | .only() |
56 | .run_with_handle(); |
57 | |
58 | with_default(subscriber, || { |
59 | my_fn(((1, 2), (3, 4))); |
60 | }); |
61 | |
62 | handle.assert_finished(); |
63 | } |
64 | |
65 | #[test] |
66 | fn destructure_refs() { |
67 | #[instrument ] |
68 | fn my_fn(&arg1: &usize) {} |
69 | |
70 | let span = expect::span().named("my_fn" ); |
71 | |
72 | let (subscriber, handle) = subscriber::mock() |
73 | .new_span( |
74 | span.clone() |
75 | .with_field(expect::field("arg1" ).with_value(&1usize).only()), |
76 | ) |
77 | .enter(span.clone()) |
78 | .exit(span.clone()) |
79 | .drop_span(span) |
80 | .only() |
81 | .run_with_handle(); |
82 | |
83 | with_default(subscriber, || { |
84 | my_fn(&1); |
85 | }); |
86 | |
87 | handle.assert_finished(); |
88 | } |
89 | |
90 | #[test] |
91 | fn destructure_tuple_structs() { |
92 | struct Foo(usize, usize); |
93 | |
94 | #[instrument ] |
95 | fn my_fn(Foo(arg1, arg2): Foo) {} |
96 | |
97 | let span = expect::span().named("my_fn" ); |
98 | |
99 | let (subscriber, handle) = subscriber::mock() |
100 | .new_span( |
101 | span.clone().with_field( |
102 | expect::field("arg1" ) |
103 | .with_value(&format_args!("1" )) |
104 | .and(expect::field("arg2" ).with_value(&format_args!("2" ))) |
105 | .only(), |
106 | ), |
107 | ) |
108 | .enter(span.clone()) |
109 | .exit(span.clone()) |
110 | .drop_span(span) |
111 | .only() |
112 | .run_with_handle(); |
113 | |
114 | with_default(subscriber, || { |
115 | my_fn(Foo(1, 2)); |
116 | }); |
117 | |
118 | handle.assert_finished(); |
119 | } |
120 | |
121 | #[test] |
122 | fn destructure_structs() { |
123 | struct Foo { |
124 | bar: usize, |
125 | baz: usize, |
126 | } |
127 | |
128 | #[instrument ] |
129 | fn my_fn( |
130 | Foo { |
131 | bar: arg1, |
132 | baz: arg2, |
133 | }: Foo, |
134 | ) { |
135 | let _ = (arg1, arg2); |
136 | } |
137 | |
138 | let span = expect::span().named("my_fn" ); |
139 | |
140 | let (subscriber, handle) = subscriber::mock() |
141 | .new_span( |
142 | span.clone().with_field( |
143 | expect::field("arg1" ) |
144 | .with_value(&format_args!("1" )) |
145 | .and(expect::field("arg2" ).with_value(&format_args!("2" ))) |
146 | .only(), |
147 | ), |
148 | ) |
149 | .enter(span.clone()) |
150 | .exit(span.clone()) |
151 | .drop_span(span) |
152 | .only() |
153 | .run_with_handle(); |
154 | |
155 | with_default(subscriber, || { |
156 | my_fn(Foo { bar: 1, baz: 2 }); |
157 | }); |
158 | |
159 | handle.assert_finished(); |
160 | } |
161 | |
162 | #[test] |
163 | fn destructure_everything() { |
164 | struct Foo { |
165 | bar: Bar, |
166 | baz: (usize, usize), |
167 | qux: NoDebug, |
168 | } |
169 | struct Bar((usize, usize)); |
170 | struct NoDebug; |
171 | |
172 | #[instrument ] |
173 | fn my_fn( |
174 | &Foo { |
175 | bar: Bar((arg1, arg2)), |
176 | baz: (arg3, arg4), |
177 | .. |
178 | }: &Foo, |
179 | ) { |
180 | let _ = (arg1, arg2, arg3, arg4); |
181 | } |
182 | |
183 | let span = expect::span().named("my_fn" ); |
184 | |
185 | let (subscriber, handle) = subscriber::mock() |
186 | .new_span( |
187 | span.clone().with_field( |
188 | expect::field("arg1" ) |
189 | .with_value(&format_args!("1" )) |
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" ))) |
193 | .only(), |
194 | ), |
195 | ) |
196 | .enter(span.clone()) |
197 | .exit(span.clone()) |
198 | .drop_span(span) |
199 | .only() |
200 | .run_with_handle(); |
201 | |
202 | with_default(subscriber, || { |
203 | let foo = Foo { |
204 | bar: Bar((1, 2)), |
205 | baz: (3, 4), |
206 | qux: NoDebug, |
207 | }; |
208 | let _ = foo.qux; // to eliminate unused field warning |
209 | my_fn(&foo); |
210 | }); |
211 | |
212 | handle.assert_finished(); |
213 | } |
214 | |