[core] Tests: WinPath, GameId and join properties; error Display
This commit is contained in:
@@ -5,6 +5,66 @@
|
||||
//! `WinPath` and `GameId`: the two validated value types everything else is built from.
|
||||
|
||||
use arena_core::{GameId, ParseError, Source, WinPath, limits};
|
||||
use proptest::prelude::*;
|
||||
|
||||
proptest! {
|
||||
#[test]
|
||||
fn winpath_never_panics_and_normal_form_is_stable(s in r"([A-Za-z]:)?[\\/a-zA-Z0-9 .:<>|?*\x00-\x1f¹~$-]{0,40}") {
|
||||
if let Ok(p) = WinPath::parse(&s) {
|
||||
let again = WinPath::parse(p.as_str()).unwrap();
|
||||
prop_assert_eq!(&again, &p);
|
||||
prop_assert!(p.components().all(|c| c != ".." && c != "." && !c.contains('\0')));
|
||||
prop_assert!(!p.as_str().contains('/'));
|
||||
prop_assert!(p.as_str().get(1..3) == Some(":\\"));
|
||||
if let Some(parent) = p.parent() {
|
||||
prop_assert!(p.starts_with(&parent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn winpath_join_never_climbs_out(rel in r"[a-zA-Z.\\/]{0,20}") {
|
||||
let base = WinPath::parse(r"C:\Games\X").unwrap();
|
||||
if let Ok(j) = base.join(&rel) {
|
||||
prop_assert!(j.starts_with(&base));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn game_id_round_trips_or_refuses(s in r"[a-z]{0,7}:?[a-zA-Z0-9:._ -]{0,20}") {
|
||||
if let Ok(id) = s.parse::<GameId>() {
|
||||
prop_assert_eq!(id.to_string(), s.clone());
|
||||
prop_assert!(s.len() <= limits::MAX_GAME_ID);
|
||||
prop_assert!(s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b":._-".contains(&b)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_display_without_input_text() {
|
||||
let all = [
|
||||
ParseError::TooLarge,
|
||||
ParseError::TooDeep,
|
||||
ParseError::NotUtf8,
|
||||
ParseError::Syntax { at: 7 },
|
||||
ParseError::Missing("name"),
|
||||
ParseError::WrongType("appid"),
|
||||
ParseError::BadPath("installdir"),
|
||||
ParseError::BadId,
|
||||
ParseError::Duplicate("key"),
|
||||
ParseError::TooLong("value"),
|
||||
ParseError::TooMany("entries"),
|
||||
ParseError::BadValue("url"),
|
||||
ParseError::NotAGame("Epic add-on"),
|
||||
];
|
||||
for e in all {
|
||||
let text = e.to_string();
|
||||
assert!(!text.is_empty());
|
||||
let as_error: &dyn std::error::Error = &e;
|
||||
assert_eq!(as_error.to_string(), text);
|
||||
}
|
||||
assert_eq!(ParseError::Syntax { at: 7 }.to_string(), "syntax error at byte 7");
|
||||
}
|
||||
|
||||
fn p(s: &str) -> WinPath {
|
||||
WinPath::parse(s).unwrap()
|
||||
|
||||
Reference in New Issue
Block a user