xref: /aosp_15_r20/external/pigweed/pw_log/rust/backend_tests.rs (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker #[cfg(test)]
15*61c4878aSAndroid Build Coastguard Worker // Untyped prints code rely on as casts to annotate type information.
16*61c4878aSAndroid Build Coastguard Worker #[allow(clippy::unnecessary_cast)]
17*61c4878aSAndroid Build Coastguard Worker mod tests {
18*61c4878aSAndroid Build Coastguard Worker     use crate::run_with_capture;
19*61c4878aSAndroid Build Coastguard Worker     use pw_log_backend::{pw_log_backend, pw_logf_backend};
20*61c4878aSAndroid Build Coastguard Worker     use pw_log_backend_api::LogLevel;
21*61c4878aSAndroid Build Coastguard Worker 
22*61c4878aSAndroid Build Coastguard Worker     #[test]
no_argument_log_line_prints_to_stdout()23*61c4878aSAndroid Build Coastguard Worker     fn no_argument_log_line_prints_to_stdout() {
24*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
25*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "test")),
26*61c4878aSAndroid Build Coastguard Worker             "[INF] test\n"
27*61c4878aSAndroid Build Coastguard Worker         );
28*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
29*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test")),
30*61c4878aSAndroid Build Coastguard Worker             "[INF] test\n"
31*61c4878aSAndroid Build Coastguard Worker         );
32*61c4878aSAndroid Build Coastguard Worker     }
33*61c4878aSAndroid Build Coastguard Worker 
34*61c4878aSAndroid Build Coastguard Worker     #[test]
integer_argument_prints_to_stdout()35*61c4878aSAndroid Build Coastguard Worker     fn integer_argument_prints_to_stdout() {
36*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
37*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %d", -1)),
38*61c4878aSAndroid Build Coastguard Worker             "[INF] test -1\n",
39*61c4878aSAndroid Build Coastguard Worker         );
40*61c4878aSAndroid Build Coastguard Worker     }
41*61c4878aSAndroid Build Coastguard Worker 
42*61c4878aSAndroid Build Coastguard Worker     #[test]
unsigned_argument_prints_to_stdout()43*61c4878aSAndroid Build Coastguard Worker     fn unsigned_argument_prints_to_stdout() {
44*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
45*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %u", 1u32)),
46*61c4878aSAndroid Build Coastguard Worker             "[INF] test 1\n",
47*61c4878aSAndroid Build Coastguard Worker         );
48*61c4878aSAndroid Build Coastguard Worker     }
49*61c4878aSAndroid Build Coastguard Worker 
50*61c4878aSAndroid Build Coastguard Worker     #[test]
string_argument_prints_to_stdout()51*61c4878aSAndroid Build Coastguard Worker     fn string_argument_prints_to_stdout() {
52*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
53*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %s", "test")),
54*61c4878aSAndroid Build Coastguard Worker             "[INF] test test\n",
55*61c4878aSAndroid Build Coastguard Worker         );
56*61c4878aSAndroid Build Coastguard Worker     }
57*61c4878aSAndroid Build Coastguard Worker     #[test]
character_argument_prints_to_stdout()58*61c4878aSAndroid Build Coastguard Worker     fn character_argument_prints_to_stdout() {
59*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
60*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %c", 'c')),
61*61c4878aSAndroid Build Coastguard Worker             "[INF] test c\n",
62*61c4878aSAndroid Build Coastguard Worker         );
63*61c4878aSAndroid Build Coastguard Worker     }
64*61c4878aSAndroid Build Coastguard Worker 
65*61c4878aSAndroid Build Coastguard Worker     #[test]
untyped_i32_argument_prints_to_stdout()66*61c4878aSAndroid Build Coastguard Worker     fn untyped_i32_argument_prints_to_stdout() {
67*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
68*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", -1 as i32)),
69*61c4878aSAndroid Build Coastguard Worker             "[INF] test -1\n",
70*61c4878aSAndroid Build Coastguard Worker         );
71*61c4878aSAndroid Build Coastguard Worker 
72*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
73*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", -1 as i32)),
74*61c4878aSAndroid Build Coastguard Worker             "[INF] test -1\n",
75*61c4878aSAndroid Build Coastguard Worker         );
76*61c4878aSAndroid Build Coastguard Worker     }
77*61c4878aSAndroid Build Coastguard Worker     #[test]
untyped_u32_argument_prints_to_stdout()78*61c4878aSAndroid Build Coastguard Worker     fn untyped_u32_argument_prints_to_stdout() {
79*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
80*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", 1 as u32)),
81*61c4878aSAndroid Build Coastguard Worker             "[INF] test 1\n",
82*61c4878aSAndroid Build Coastguard Worker         );
83*61c4878aSAndroid Build Coastguard Worker 
84*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
85*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", 1 as u32)),
86*61c4878aSAndroid Build Coastguard Worker             "[INF] test 1\n",
87*61c4878aSAndroid Build Coastguard Worker         );
88*61c4878aSAndroid Build Coastguard Worker     }
89*61c4878aSAndroid Build Coastguard Worker 
90*61c4878aSAndroid Build Coastguard Worker     #[test]
untyped_str_argument_prints_to_stdout()91*61c4878aSAndroid Build Coastguard Worker     fn untyped_str_argument_prints_to_stdout() {
92*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
93*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", "Pigweed" as &str)),
94*61c4878aSAndroid Build Coastguard Worker             "[INF] test Pigweed\n",
95*61c4878aSAndroid Build Coastguard Worker         );
96*61c4878aSAndroid Build Coastguard Worker 
97*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
98*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", "Pigweed" as &str)),
99*61c4878aSAndroid Build Coastguard Worker             "[INF] test Pigweed\n",
100*61c4878aSAndroid Build Coastguard Worker         );
101*61c4878aSAndroid Build Coastguard Worker     }
102*61c4878aSAndroid Build Coastguard Worker 
103*61c4878aSAndroid Build Coastguard Worker     #[test]
untyped_hex_integer_argument_prints_to_stdout()104*61c4878aSAndroid Build Coastguard Worker     fn untyped_hex_integer_argument_prints_to_stdout() {
105*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
106*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:x}", 0xdecafbad as u32)),
107*61c4878aSAndroid Build Coastguard Worker             "[INF] decafbad\n",
108*61c4878aSAndroid Build Coastguard Worker         );
109*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
110*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:X}!", 0xdecafbad as u32)),
111*61c4878aSAndroid Build Coastguard Worker             "[INF] DECAFBAD!\n",
112*61c4878aSAndroid Build Coastguard Worker         );
113*61c4878aSAndroid Build Coastguard Worker     }
114*61c4878aSAndroid Build Coastguard Worker 
115*61c4878aSAndroid Build Coastguard Worker     #[test]
typed_min_fields_width_and_zero_padding_formats_correctly()116*61c4878aSAndroid Build Coastguard Worker     fn typed_min_fields_width_and_zero_padding_formats_correctly() {
117*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
118*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "%8x", 0xcafe as u32)),
119*61c4878aSAndroid Build Coastguard Worker             "[INF]     cafe\n",
120*61c4878aSAndroid Build Coastguard Worker         );
121*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
122*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_logf_backend!(LogLevel::Info, "%08X!", 0xcafe as u32)),
123*61c4878aSAndroid Build Coastguard Worker             "[INF] 0000CAFE!\n",
124*61c4878aSAndroid Build Coastguard Worker         );
125*61c4878aSAndroid Build Coastguard Worker     }
126*61c4878aSAndroid Build Coastguard Worker 
127*61c4878aSAndroid Build Coastguard Worker     #[test]
untyped_min_fields_width_and_zero_padding_formats_correctly()128*61c4878aSAndroid Build Coastguard Worker     fn untyped_min_fields_width_and_zero_padding_formats_correctly() {
129*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
130*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:8x}", 0xcafe as u32)),
131*61c4878aSAndroid Build Coastguard Worker             "[INF]     cafe\n",
132*61c4878aSAndroid Build Coastguard Worker         );
133*61c4878aSAndroid Build Coastguard Worker         assert_eq!(
134*61c4878aSAndroid Build Coastguard Worker             run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:08X}!", 0xcafe as u32)),
135*61c4878aSAndroid Build Coastguard Worker             "[INF] 0000CAFE!\n",
136*61c4878aSAndroid Build Coastguard Worker         );
137*61c4878aSAndroid Build Coastguard Worker     }
138*61c4878aSAndroid Build Coastguard Worker }
139