Skip to main content

smart_format/case/
uppercase.rs

1use crate::prelude::*;
2
3pub struct Uppercase<T: fmt::Display>(pub T);
4
5impl<T: fmt::Display> fmt::Display for Uppercase<T> {
6    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7        use fmt::Write;
8
9        self.0.format_with(|s: Option<&str>| {
10            let Some(s) = s else { return Ok(()) };
11
12            for c in s.chars().flat_map(char::to_uppercase) {
13                f.write_char(c)?;
14            }
15
16            Ok(())
17        })
18    }
19}
20
21#[cfg(test)]
22mod tests {
23    use super::super::tests::WORD_CASES;
24    use super::*;
25
26    #[test]
27    fn to_upper() {
28        fn test_case(expected: &str, sample: &str) {
29            assert_eq!(expected, Uppercase(sample).to_string());
30        }
31
32        for &word in WORD_CASES {
33            test_case("WORD", word);
34        }
35    }
36
37    #[test]
38    fn empty() {
39        assert_eq!("", Uppercase("").to_string());
40    }
41
42    #[test]
43    fn single_char() {
44        assert_eq!("A", Uppercase("a").to_string());
45    }
46
47    #[test]
48    fn german_eszett_to_upper() {
49        // Unicode default uppercasing: ß → SS
50        assert_eq!("SS", Uppercase("ß").to_string());
51    }
52
53    #[test]
54    fn cyrillic() {
55        assert_eq!("ПРИВІТ", Uppercase("привіт").to_string());
56    }
57}