|
1 | 1 | mod error;
|
2 | 2 | mod format;
|
| 3 | +mod rust_fmt_argument_max_padding; |
| 4 | + |
| 5 | +use crate::format::TimeFormatter; |
| 6 | +use crate::{Error, Time}; |
| 7 | + |
| 8 | +include!("../mock.rs.in"); |
| 9 | + |
| 10 | +fn get_format_err(time: &MockTime<'_>, format: &str) -> Error { |
| 11 | + TimeFormatter::new(time, format) |
| 12 | + .fmt(&mut &mut [0u8; 100][..]) |
| 13 | + .unwrap_err() |
| 14 | +} |
| 15 | + |
| 16 | +fn get_format_err_bytes(time: &MockTime<'_>, format: &[u8]) -> Error { |
| 17 | + TimeFormatter::new(time, format) |
| 18 | + .fmt(&mut &mut [0u8; 100][..]) |
| 19 | + .unwrap_err() |
| 20 | +} |
| 21 | + |
| 22 | +fn check_format(time: &MockTime<'_>, format: &str, expected: &str) { |
| 23 | + const SIZE: usize = 100; |
| 24 | + let mut buf = [0u8; SIZE]; |
| 25 | + let mut cursor = &mut buf[..]; |
| 26 | + |
| 27 | + TimeFormatter::new(time, format).fmt(&mut cursor).unwrap(); |
| 28 | + let written = SIZE - cursor.len(); |
| 29 | + let data = core::str::from_utf8(&buf[..written]).unwrap(); |
| 30 | + |
| 31 | + assert_eq!(data, expected); |
| 32 | +} |
| 33 | + |
| 34 | +fn check_all(times: &[MockTime<'_>], format: &str, all_expected: &[&str]) { |
| 35 | + assert_eq!(times.len(), all_expected.len()); |
| 36 | + for (time, expected) in times.iter().zip(all_expected) { |
| 37 | + check_format(time, format, expected); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[test] |
| 42 | +#[should_panic] |
| 43 | +#[rustfmt::skip] |
| 44 | +fn test_check_format_panics_on_error() { |
| 45 | + let time = MockTime { year: 1111, ..Default::default() }; |
| 46 | + |
| 47 | + check_format(&time, "'%Y'", "'1112'"); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +#[should_panic] |
| 52 | +#[rustfmt::skip] |
| 53 | +fn test_check_all_panics_on_error() { |
| 54 | + let times = [ |
| 55 | + MockTime { year: -1111, ..Default::default() }, |
| 56 | + MockTime { year: -11, ..Default::default() }, |
| 57 | + MockTime { year: 1, ..Default::default() }, |
| 58 | + MockTime { year: 1111, ..Default::default() }, |
| 59 | + ]; |
| 60 | + |
| 61 | + check_all(×, "'%Y'", &["'-1111'", "'-0011'", "'0001'", "'1112'"]); |
| 62 | +} |
0 commit comments