summaryrefslogtreecommitdiffstats
path: root/vendor/tracing-attributes/tests/fields.rs
blob: c178fbb3d35a98e26e006b805a2a143ce68c432c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use tracing::subscriber::with_default;
use tracing_attributes::instrument;
use tracing_mock::field::mock;
use tracing_mock::span::NewSpan;
use tracing_mock::*;

#[instrument(fields(foo = "bar", dsa = true, num = 1))]
fn fn_no_param() {}

#[instrument(fields(foo = "bar"))]
fn fn_param(param: u32) {}

#[instrument(fields(foo = "bar", empty))]
fn fn_empty_field() {}

#[instrument(fields(len = s.len()))]
fn fn_expr_field(s: &str) {}

#[instrument(fields(s.len = s.len(), s.is_empty = s.is_empty()))]
fn fn_two_expr_fields(s: &str) {
    let _ = s;
}

#[instrument(fields(%s, s.len = s.len()))]
fn fn_clashy_expr_field(s: &str) {
    let _ = s;
}

#[instrument(fields(s = "s"))]
fn fn_clashy_expr_field2(s: &str) {
    let _ = s;
}

#[instrument(fields(s = &s))]
fn fn_string(s: String) {
    let _ = s;
}

#[derive(Debug)]
struct HasField {
    my_field: &'static str,
}

impl HasField {
    #[instrument(fields(my_field = self.my_field), skip(self))]
    fn self_expr_field(&self) {}
}

#[test]
fn fields() {
    let span = span::mock().with_field(
        mock("foo")
            .with_value(&"bar")
            .and(mock("dsa").with_value(&true))
            .and(mock("num").with_value(&1))
            .only(),
    );
    run_test(span, || {
        fn_no_param();
    });
}

#[test]
fn expr_field() {
    let span = span::mock().with_field(
        mock("s")
            .with_value(&"hello world")
            .and(mock("len").with_value(&"hello world".len()))
            .only(),
    );
    run_test(span, || {
        fn_expr_field("hello world");
    });
}

#[test]
fn two_expr_fields() {
    let span = span::mock().with_field(
        mock("s")
            .with_value(&"hello world")
            .and(mock("s.len").with_value(&"hello world".len()))
            .and(mock("s.is_empty").with_value(&false))
            .only(),
    );
    run_test(span, || {
        fn_two_expr_fields("hello world");
    });
}

#[test]
fn clashy_expr_field() {
    let span = span::mock().with_field(
        // Overriding the `s` field should record `s` as a `Display` value,
        // rather than as a `Debug` value.
        mock("s")
            .with_value(&tracing::field::display("hello world"))
            .and(mock("s.len").with_value(&"hello world".len()))
            .only(),
    );
    run_test(span, || {
        fn_clashy_expr_field("hello world");
    });

    let span = span::mock().with_field(mock("s").with_value(&"s").only());
    run_test(span, || {
        fn_clashy_expr_field2("hello world");
    });
}

#[test]
fn self_expr_field() {
    let span = span::mock().with_field(mock("my_field").with_value(&"hello world").only());
    run_test(span, || {
        let has_field = HasField {
            my_field: "hello world",
        };
        has_field.self_expr_field();
    });
}

#[test]
fn parameters_with_fields() {
    let span = span::mock().with_field(
        mock("foo")
            .with_value(&"bar")
            .and(mock("param").with_value(&1u32))
            .only(),
    );
    run_test(span, || {
        fn_param(1);
    });
}

#[test]
fn empty_field() {
    let span = span::mock().with_field(mock("foo").with_value(&"bar").only());
    run_test(span, || {
        fn_empty_field();
    });
}

#[test]
fn string_field() {
    let span = span::mock().with_field(mock("s").with_value(&"hello world").only());
    run_test(span, || {
        fn_string(String::from("hello world"));
    });
}

fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
    let (subscriber, handle) = subscriber::mock()
        .new_span(span)
        .enter(span::mock())
        .exit(span::mock())
        .done()
        .run_with_handle();

    with_default(subscriber, fun);
    handle.assert_finished();
}