Skip to main content

perl_test_generators/
variable.rs

1//! Generators for Perl variable names.
2//!
3//! Covers sigils (`$`, `@`, `%`), special variables (`$_`, `@_`, `$1`–`$9`),
4//! and package-qualified names (`$Foo::bar`).
5
6use proptest::prelude::*;
7
8/// Valid ASCII identifier characters for variable name body (first char).
9static IDENT_START: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
10
11/// Valid ASCII identifier characters for subsequent positions.
12static IDENT_CONT: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
13
14/// Generate a single Perl identifier (without sigil).
15fn identifier() -> impl Strategy<Value = String> {
16    prop_oneof![
17        // Common short names
18        Just("self".to_string()),
19        Just("class".to_string()),
20        // Random identifier 1–12 chars
21        (
22            prop::sample::select(IDENT_START),
23            prop::collection::vec(prop::sample::select(IDENT_CONT), 0..=11_usize),
24        )
25            .prop_map(|(first, rest)| {
26                let mut s = String::new();
27                s.push(first as char);
28                for b in rest {
29                    s.push(b as char);
30                }
31                s
32            }),
33    ]
34}
35
36/// Generate a Perl package path (`Foo`, `Foo::Bar`, ...).
37fn package_path() -> impl Strategy<Value = String> {
38    prop::collection::vec(identifier(), 1..=4_usize).prop_map(|segments| segments.join("::"))
39}
40
41/// Generate a full Perl variable name including sigil.
42///
43/// Covers scalar (`$x`), array (`@x`), hash (`%x`), special variables
44/// (`$_`, `@_`, `$1`–`$9`), and optionally package-qualified names
45/// (`$Foo::Bar::baz`).
46pub fn variable() -> impl Strategy<Value = String> {
47    prop_oneof![
48        // Special variables
49        Just("$_".to_string()),
50        Just("@_".to_string()),
51        Just("%ENV".to_string()),
52        Just("@ARGV".to_string()),
53        Just("$0".to_string()),
54        (1u32..=9).prop_map(|n| format!("${}", n)),
55        // Simple sigiled variable
56        (prop_oneof![Just('$'), Just('@'), Just('%')], identifier())
57            .prop_map(|(sigil, name)| format!("{}{}", sigil, name)),
58        // Package-qualified
59        (prop_oneof![Just('$'), Just('@'), Just('%')], package_path(), identifier())
60            .prop_map(|(sigil, pkg, name)| format!("{}{}::{}", sigil, pkg, name)),
61    ]
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    proptest! {
69        #[test]
70        fn variable_starts_with_sigil(v in variable()) {
71            assert!(v.starts_with('$') || v.starts_with('@') || v.starts_with('%'));
72        }
73
74        #[test]
75        fn variable_body_is_valid(v in variable()) {
76            let body = &v[1..];
77            assert!(!body.is_empty(), "variable body must not be empty: {}", v);
78        }
79
80        #[test]
81        fn identifier_is_ascii(id in identifier()) {
82            prop_assert!(id.is_ascii(), "identifier must be ASCII: {}", id);
83        }
84
85        #[test]
86        fn package_path_has_no_empty_segments(pkg in package_path()) {
87            for segment in pkg.split("::") {
88                prop_assert!(!segment.is_empty(), "empty package segment in {pkg}");
89            }
90        }
91    }
92}