#![allow(missing_docs)]
use noyalib::{Value, from_slice, from_str};
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig::with_cases(
std::env::var("PROPTEST_CASES")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(256)
))]
#[test]
fn from_str_value_never_panics_on_arbitrary_utf8(s in ".{0,1024}") {
std::panic::catch_unwind(|| {
let _: Result<Value, _> = from_str(&s);
}).expect("from_str must not panic on arbitrary UTF-8");
}
#[test]
fn from_slice_value_never_panics_on_arbitrary_bytes(bytes in prop::collection::vec(any::<u8>(), 0..1024)) {
std::panic::catch_unwind(|| {
let _: Result<Value, _> = from_slice(&bytes);
}).expect("from_slice must not panic on arbitrary bytes");
}
#[test]
fn from_str_typed_never_panics(s in ".{0,256}") {
std::panic::catch_unwind(|| {
let _: Result<i64, _> = from_str(&s);
}).expect("typed from_str must not panic");
}
#[test]
fn from_str_string_target_never_panics(s in ".{0,256}") {
std::panic::catch_unwind(|| {
let _: Result<String, _> = from_str(&s);
}).expect("String-target from_str must not panic");
}
#[test]
fn load_all_never_panics(s in ".{0,512}") {
std::panic::catch_unwind(|| {
let _: Result<Vec<Value>, _> = noyalib::load_all_as(&s);
}).expect("load_all_as must not panic");
}
#[test]
fn cst_parse_document_never_panics(s in ".{0,512}") {
std::panic::catch_unwind(|| {
let _ = noyalib::cst::parse_document(&s);
}).expect("cst::parse_document must not panic");
}
}
const HISTORICAL_PANIC_INPUTS: &[&[u8]] = &[
b"",
b"\xef\xbb\xbf",
b"%YAML 1.2",
b"[",
b"{",
b"[[[[",
b"{{{{",
b"*",
b"&",
b"!",
b":",
b"-",
b"---\n...\n---\n...\n",
b"'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",
b"\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",
b"[[[[[[[[[[[[[[[[[[[[",
b"\xff\xff\xff",
b"\x80\x81\x82",
b"a:\r\n b:\r\n c",
b"\ta:\n\t\tb: 1",
];
#[test]
fn historical_panic_inputs_do_not_panic() {
for (i, bytes) in HISTORICAL_PANIC_INPUTS.iter().enumerate() {
let res = std::panic::catch_unwind(|| {
let _: Result<Value, _> = from_slice(bytes);
});
assert!(
res.is_ok(),
"panic on historical input #{i}: {:?}",
String::from_utf8_lossy(bytes)
);
}
}
#[test]
fn historical_panic_inputs_via_str_when_utf8() {
for (i, bytes) in HISTORICAL_PANIC_INPUTS.iter().enumerate() {
if let Ok(s) = std::str::from_utf8(bytes) {
let res = std::panic::catch_unwind(|| {
let _: Result<Value, _> = from_str(s);
});
assert!(res.is_ok(), "panic on str-cast historical #{i}: {s:?}");
}
}
}