Debian Codegraph @

third_party/rust/tracing-attributes/tests/fields.rs

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