Skip to main content

perl_test_generators/
module.rs

1//! Generators for Perl module paths.
2//!
3//! Produces syntactically valid module names like `Foo`, `Foo::Bar`, `Foo::Bar::Baz`.
4
5use proptest::prelude::*;
6
7/// Single identifier segment (capitalized PascalCase or `UPPER_CASE`).
8fn module_segment() -> impl Strategy<Value = String> {
9    prop_oneof![
10        Just("Foo".to_string()),
11        Just("Bar".to_string()),
12        Just("Baz".to_string()),
13        Just("HTTP".to_string()),
14        Just("IO".to_string()),
15        (
16            prop::char::range('A', 'Z'),
17            prop::collection::vec(
18                prop_oneof![prop::char::range('A', 'Z'), prop::char::range('0', '9'), Just('_'),],
19                0..=7_usize,
20            ),
21        )
22            .prop_map(|(first, rest)| std::iter::once(first).chain(rest).collect::<String>()),
23        (
24            prop::char::range('A', 'Z'),
25            prop::collection::vec(prop::char::range('a', 'z'), 0..=7_usize),
26        )
27            .prop_map(|(first, rest)| std::iter::once(first).chain(rest).collect::<String>()),
28    ]
29}
30
31/// Generate a full module path (1–5 segments joined by `::`).
32pub fn module_path() -> impl Strategy<Value = String> {
33    prop::collection::vec(module_segment(), 1..=5_usize).prop_map(|segs| segs.join("::"))
34}
35
36/// Generate module path segments as a `Vec<String>`.
37pub fn module_path_segments() -> impl Strategy<Value = Vec<String>> {
38    prop::collection::vec(module_segment(), 1..=5_usize)
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    proptest! {
46        #[test]
47        fn module_path_no_empty_segments(path in module_path()) {
48            for seg in path.split("::") {
49                assert!(!seg.is_empty(), "empty segment in {}", path);
50            }
51        }
52
53        #[test]
54        fn module_path_starts_uppercase(path in module_path()) {
55            prop_assert!(!path.is_empty(), "module path must not be empty");
56            let first = path.chars().next().unwrap_or_default();
57            prop_assert!(first.is_ascii_uppercase(), "first char not uppercase: {}", path);
58        }
59
60        #[test]
61        fn segments_non_empty(segs in module_path_segments()) {
62            prop_assert!(!segs.is_empty());
63            for s in &segs {
64                prop_assert!(!s.is_empty());
65            }
66        }
67
68        #[test]
69        fn segments_use_module_identifier_charset(segs in module_path_segments()) {
70            for seg in &segs {
71                let mut chars = seg.chars();
72                let Some(first) = chars.next() else {
73                    prop_assert!(false, "segment cannot be empty");
74                    continue;
75                };
76                prop_assert!(first.is_ascii_uppercase(), "segment must start uppercase: {seg}");
77                for ch in chars {
78                    prop_assert!(
79                        ch.is_ascii_alphanumeric() || ch == '_',
80                        "invalid module segment char '{ch}' in {seg}",
81                    );
82                }
83            }
84        }
85    }
86}