use proptest::prelude::*;
static IDENT_START: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
static IDENT_CONT: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
fn identifier() -> impl Strategy<Value = String> {
prop_oneof![
Just("self".to_string()),
Just("class".to_string()),
(
prop::sample::select(IDENT_START),
prop::collection::vec(prop::sample::select(IDENT_CONT), 0..=11_usize),
)
.prop_map(|(first, rest)| {
let mut s = String::new();
s.push(first as char);
for b in rest {
s.push(b as char);
}
s
}),
]
}
fn package_path() -> impl Strategy<Value = String> {
prop::collection::vec(identifier(), 1..=4_usize).prop_map(|segments| segments.join("::"))
}
pub fn variable() -> impl Strategy<Value = String> {
prop_oneof![
Just("$_".to_string()),
Just("@_".to_string()),
Just("%ENV".to_string()),
Just("@ARGV".to_string()),
Just("$0".to_string()),
(1u32..=9).prop_map(|n| format!("${}", n)),
(prop_oneof![Just('$'), Just('@'), Just('%')], identifier())
.prop_map(|(sigil, name)| format!("{}{}", sigil, name)),
(prop_oneof![Just('$'), Just('@'), Just('%')], package_path(), identifier())
.prop_map(|(sigil, pkg, name)| format!("{}{}::{}", sigil, pkg, name)),
]
}
#[cfg(test)]
mod tests {
use super::*;
proptest! {
#[test]
fn variable_starts_with_sigil(v in variable()) {
assert!(v.starts_with('$') || v.starts_with('@') || v.starts_with('%'));
}
#[test]
fn variable_body_is_valid(v in variable()) {
let body = &v[1..];
assert!(!body.is_empty(), "variable body must not be empty: {}", v);
}
#[test]
fn identifier_is_ascii(id in identifier()) {
prop_assert!(id.is_ascii(), "identifier must be ASCII: {}", id);
}
#[test]
fn package_path_has_no_empty_segments(pkg in package_path()) {
for segment in pkg.split("::") {
prop_assert!(!segment.is_empty(), "empty package segment in {pkg}");
}
}
}
}