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