]>
git.proxmox.com Git - rustc.git/blob - vendor/tracing-attributes/tests/fields.rs
4 use crate::support
::field
::mock
;
5 use crate::support
::span
::NewSpan
;
6 use tracing
::subscriber
::with_default
;
7 use tracing_attributes
::instrument
;
9 #[instrument(fields(foo = "bar", dsa = true, num = 1))]
12 #[instrument(fields(foo = "bar"))]
13 fn fn_param(param
: u32) {}
15 #[instrument(fields(foo = "bar", empty))]
16 fn fn_empty_field() {}
18 #[instrument(fields(len = s.len()))]
19 fn fn_expr_field(s
: &str) {}
21 #[instrument(fields(s.len = s.len(), s.is_empty = s.is_empty()))]
22 fn fn_two_expr_fields(s
: &str) {
26 #[instrument(fields(%s, s.len = s.len()))]
27 fn fn_clashy_expr_field(s
: &str) {
31 #[instrument(fields(s = "s"))]
32 fn fn_clashy_expr_field2(s
: &str) {
38 my_field
: &'
static str,
42 #[instrument(fields(my_field = self.my_field), skip(self))]
43 fn self_expr_field(&self) {}
48 let span
= span
::mock().with_field(
51 .and(mock("dsa").with_value(&true))
52 .and(mock("num").with_value(&1))
62 let span
= span
::mock().with_field(
64 .with_value(&tracing
::field
::debug("hello world"))
65 .and(mock("len").with_value(&"hello world".len()))
69 fn_expr_field(&"hello world");
74 fn two_expr_fields() {
75 let span
= span
::mock().with_field(
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))
83 fn_two_expr_fields(&"hello world");
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.
93 .with_value(&tracing
::field
::display("hello world"))
94 .and(mock("s.len").with_value(&"hello world".len()))
98 fn_clashy_expr_field(&"hello world");
101 let span
= span
::mock().with_field(mock("s").with_value(&"s").only());
103 fn_clashy_expr_field2(&"hello world");
108 fn self_expr_field() {
109 let span
= span
::mock().with_field(mock("my_field").with_value(&"hello world").only());
111 let has_field
= HasField
{
112 my_field
: "hello world",
114 has_field
.self_expr_field();
119 fn parameters_with_fields() {
120 let span
= span
::mock().with_field(
123 .and(mock("param").with_value(&format_args
!("1")))
133 let span
= span
::mock().with_field(mock("foo").with_value(&"bar").only());
139 fn run_test
<F
: FnOnce() -> T
, T
>(span
: NewSpan
, fun
: F
) {
140 let (subscriber
, handle
) = subscriber
::mock()
147 with_default(subscriber
, fun
);
148 handle
.assert_finished();