test_case_core/
utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use proc_macro2::{Ident, Span};
use quote::ToTokens;

pub fn escape_test_name(input: impl AsRef<str>) -> Ident {
    if input.as_ref().is_empty() {
        return Ident::new("_empty", Span::call_site());
    }

    let mut last_under = false;
    let mut ident: String = input
        .as_ref()
        .to_ascii_lowercase()
        .chars()
        .filter_map(|c| match c {
            c if c.is_alphanumeric() => {
                last_under = false;
                Some(c.to_ascii_lowercase())
            }
            _ if !last_under => {
                last_under = true;
                Some('_')
            }
            _ => None,
        })
        .collect();

    if !ident.starts_with(|c: char| c == '_' || c.is_ascii_alphabetic()) {
        ident = format!("_{ident}");
    }

    Ident::new(&ident, Span::call_site())
}

pub fn fmt_syn(syn: &(impl ToTokens + Clone)) -> String {
    syn.clone().into_token_stream().to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    mod escape_test_name {
        use super::*;

        #[test]
        fn converts_arbitrary_test_names() {
            assert_eq!(
                escape_test_name("word"),
                Ident::new("word", Span::call_site())
            );
            assert_eq!(
                escape_test_name("a simple sentence"),
                Ident::new("a_simple_sentence", Span::call_site())
            );
            assert_eq!(
                escape_test_name("extra  spaces  inbetween"),
                Ident::new("extra_spaces_inbetween", Span::call_site())
            );
            assert_eq!(
                escape_test_name(" extra end and start spaces "),
                Ident::new("_extra_end_and_start_spaces_", Span::call_site())
            );
            assert_eq!(
                escape_test_name("abcdefghijklmnoqprstuwvxyz1234567890"),
                Ident::new("abcdefghijklmnoqprstuwvxyz1234567890", Span::call_site())
            );
        }

        #[test]
        fn converts_to_lowercase() {
            assert_eq!(
                escape_test_name("ALL UPPER"),
                Ident::new("all_upper", Span::call_site())
            );
            assert_eq!(
                escape_test_name("MiXeD CaSe"),
                Ident::new("mixed_case", Span::call_site())
            );
        }

        #[test]
        fn handles_numeric_first_char() {
            assert_eq!(
                escape_test_name("1test"),
                Ident::new("_1test", Span::call_site())
            );
        }

        #[test]
        fn omits_unicode() {
            assert_eq!(
                escape_test_name("fromāŸ¶to"),
                Ident::new("from_to", Span::call_site())
            );
        }

        #[test]
        fn handles_empty_input() {
            assert_eq!(
                escape_test_name(""),
                Ident::new("_empty", Span::call_site())
            );
        }
    }
}