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 mod tests { 16 use crate::run_with_capture; 17 use pw_log_backend::{pw_log_backend, pw_logf_backend}; 18 use pw_log_backend_api::LogLevel; 19 20 #[test] no_argument_log_line_prints_to_stdout()21 fn no_argument_log_line_prints_to_stdout() { 22 assert_eq!( 23 run_with_capture(|| pw_log_backend!(LogLevel::Info, "test")), 24 "[INF] test\n" 25 ); 26 assert_eq!( 27 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test")), 28 "[INF] test\n" 29 ); 30 } 31 32 #[test] integer_argument_prints_to_stdout()33 fn integer_argument_prints_to_stdout() { 34 assert_eq!( 35 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %d", -1)), 36 "[INF] test -1\n", 37 ); 38 } 39 40 #[test] unsigned_argument_prints_to_stdout()41 fn unsigned_argument_prints_to_stdout() { 42 assert_eq!( 43 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %u", 1u32)), 44 "[INF] test 1\n", 45 ); 46 } 47 48 #[test] string_argument_prints_to_stdout()49 fn string_argument_prints_to_stdout() { 50 assert_eq!( 51 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %s", "test")), 52 "[INF] test test\n", 53 ); 54 } 55 #[test] character_argument_prints_to_stdout()56 fn character_argument_prints_to_stdout() { 57 assert_eq!( 58 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %c", 'c')), 59 "[INF] test c\n", 60 ); 61 } 62 63 #[test] untyped_i32_argument_prints_to_stdout()64 fn untyped_i32_argument_prints_to_stdout() { 65 assert_eq!( 66 run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", -1 as i32)), 67 "[INF] test -1\n", 68 ); 69 70 assert_eq!( 71 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", -1 as i32)), 72 "[INF] test -1\n", 73 ); 74 } 75 #[test] untyped_u32_argument_prints_to_stdout()76 fn untyped_u32_argument_prints_to_stdout() { 77 assert_eq!( 78 run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", 1 as u32)), 79 "[INF] test 1\n", 80 ); 81 82 assert_eq!( 83 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", 1 as u32)), 84 "[INF] test 1\n", 85 ); 86 } 87 88 #[test] untyped_str_argument_prints_to_stdout()89 fn untyped_str_argument_prints_to_stdout() { 90 assert_eq!( 91 run_with_capture(|| pw_log_backend!(LogLevel::Info, "test {}", "Pigweed" as &str)), 92 "[INF] test Pigweed\n", 93 ); 94 95 assert_eq!( 96 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "test %v", "Pigweed" as &str)), 97 "[INF] test Pigweed\n", 98 ); 99 } 100 101 #[test] untyped_hex_integer_argument_prints_to_stdout()102 fn untyped_hex_integer_argument_prints_to_stdout() { 103 assert_eq!( 104 run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:x}", 0xdecafbad as u32)), 105 "[INF] decafbad\n", 106 ); 107 assert_eq!( 108 run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:X}!", 0xdecafbad as u32)), 109 "[INF] DECAFBAD!\n", 110 ); 111 } 112 113 #[test] typed_min_fields_width_and_zero_padding_formats_correctly()114 fn typed_min_fields_width_and_zero_padding_formats_correctly() { 115 assert_eq!( 116 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "%8x", 0xcafe as u32)), 117 "[INF] cafe\n", 118 ); 119 assert_eq!( 120 run_with_capture(|| pw_logf_backend!(LogLevel::Info, "%08X!", 0xcafe as u32)), 121 "[INF] 0000CAFE!\n", 122 ); 123 } 124 125 #[test] untyped_min_fields_width_and_zero_padding_formats_correctly()126 fn untyped_min_fields_width_and_zero_padding_formats_correctly() { 127 assert_eq!( 128 run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:8x}", 0xcafe as u32)), 129 "[INF] cafe\n", 130 ); 131 assert_eq!( 132 run_with_capture(|| pw_log_backend!(LogLevel::Info, "{:08X}!", 0xcafe as u32)), 133 "[INF] 0000CAFE!\n", 134 ); 135 } 136 } 137