]> git.proxmox.com Git - rustc.git/blob - vendor/tracing-attributes/tests/fields.rs
New upstream version 1.49.0+dfsg1
[rustc.git] / vendor / tracing-attributes / tests / fields.rs
1 mod support;
2 use support::*;
3
4 use crate::support::field::mock;
5 use crate::support::span::NewSpan;
6 use tracing::subscriber::with_default;
7 use tracing_attributes::instrument;
8
9 #[instrument(fields(foo = "bar", dsa = true, num = 1))]
10 fn fn_no_param() {}
11
12 #[instrument(fields(foo = "bar"))]
13 fn fn_param(param: u32) {}
14
15 #[instrument(fields(foo = "bar", empty))]
16 fn fn_empty_field() {}
17
18 #[instrument(fields(len = s.len()))]
19 fn fn_expr_field(s: &str) {}
20
21 #[instrument(fields(s.len = s.len(), s.is_empty = s.is_empty()))]
22 fn fn_two_expr_fields(s: &str) {
23 let _ = s;
24 }
25
26 #[instrument(fields(%s, s.len = s.len()))]
27 fn fn_clashy_expr_field(s: &str) {
28 let _ = s;
29 }
30
31 #[instrument(fields(s = "s"))]
32 fn fn_clashy_expr_field2(s: &str) {
33 let _ = s;
34 }
35
36 #[derive(Debug)]
37 struct HasField {
38 my_field: &'static str,
39 }
40
41 impl HasField {
42 #[instrument(fields(my_field = self.my_field), skip(self))]
43 fn self_expr_field(&self) {}
44 }
45
46 #[test]
47 fn fields() {
48 let span = span::mock().with_field(
49 mock("foo")
50 .with_value(&"bar")
51 .and(mock("dsa").with_value(&true))
52 .and(mock("num").with_value(&1))
53 .only(),
54 );
55 run_test(span, || {
56 fn_no_param();
57 });
58 }
59
60 #[test]
61 fn expr_field() {
62 let span = span::mock().with_field(
63 mock("s")
64 .with_value(&tracing::field::debug("hello world"))
65 .and(mock("len").with_value(&"hello world".len()))
66 .only(),
67 );
68 run_test(span, || {
69 fn_expr_field(&"hello world");
70 });
71 }
72
73 #[test]
74 fn two_expr_fields() {
75 let span = span::mock().with_field(
76 mock("s")
77 .with_value(&tracing::field::debug("hello world"))
78 .and(mock("s.len").with_value(&"hello world".len()))
79 .and(mock("s.is_empty").with_value(&false))
80 .only(),
81 );
82 run_test(span, || {
83 fn_two_expr_fields(&"hello world");
84 });
85 }
86
87 #[test]
88 fn clashy_expr_field() {
89 let span = span::mock().with_field(
90 // Overriding the `s` field should record `s` as a `Display` value,
91 // rather than as a `Debug` value.
92 mock("s")
93 .with_value(&tracing::field::display("hello world"))
94 .and(mock("s.len").with_value(&"hello world".len()))
95 .only(),
96 );
97 run_test(span, || {
98 fn_clashy_expr_field(&"hello world");
99 });
100
101 let span = span::mock().with_field(mock("s").with_value(&"s").only());
102 run_test(span, || {
103 fn_clashy_expr_field2(&"hello world");
104 });
105 }
106
107 #[test]
108 fn self_expr_field() {
109 let span = span::mock().with_field(mock("my_field").with_value(&"hello world").only());
110 run_test(span, || {
111 let has_field = HasField {
112 my_field: "hello world",
113 };
114 has_field.self_expr_field();
115 });
116 }
117
118 #[test]
119 fn parameters_with_fields() {
120 let span = span::mock().with_field(
121 mock("foo")
122 .with_value(&"bar")
123 .and(mock("param").with_value(&format_args!("1")))
124 .only(),
125 );
126 run_test(span, || {
127 fn_param(1);
128 });
129 }
130
131 #[test]
132 fn empty_field() {
133 let span = span::mock().with_field(mock("foo").with_value(&"bar").only());
134 run_test(span, || {
135 fn_empty_field();
136 });
137 }
138
139 fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
140 let (subscriber, handle) = subscriber::mock()
141 .new_span(span)
142 .enter(span::mock())
143 .exit(span::mock())
144 .done()
145 .run_with_handle();
146
147 with_default(subscriber, fun);
148 handle.assert_finished();
149 }