perl_test_generators/
variable.rs1use proptest::prelude::*;
7
8static IDENT_START: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
10
11static IDENT_CONT: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
13
14fn identifier() -> impl Strategy<Value = String> {
16 prop_oneof![
17 Just("self".to_string()),
19 Just("class".to_string()),
20 (
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
36fn package_path() -> impl Strategy<Value = String> {
38 prop::collection::vec(identifier(), 1..=4_usize).prop_map(|segments| segments.join("::"))
39}
40
41pub fn variable() -> impl Strategy<Value = String> {
47 prop_oneof![
48 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 (prop_oneof![Just('$'), Just('@'), Just('%')], identifier())
57 .prop_map(|(sigil, name)| format!("{}{}", sigil, name)),
58 (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}