#![cfg(all(feature = "grammars", feature = "lang-python"))]
#![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
use panproto_parse::ParserRegistry;
fn registry() -> ParserRegistry {
ParserRegistry::new()
}
fn with_big_stack<F: FnOnce() + Send + 'static>(inner: F) {
std::thread::Builder::new()
.stack_size(32 * 1024 * 1024)
.spawn(inner)
.expect("spawn")
.join()
.expect("worker panicked");
}
fn assert_string_fixed_point(src: &'static [u8], must_contain: &'static str) {
with_big_stack(move || {
let reg = registry();
let s1 = reg
.parse_with_protocol("python", src, "x.py")
.expect("parse");
let e1 = reg.emit_pretty_with_protocol("python", &s1).expect("emit1");
let s2 = reg
.parse_with_protocol("python", &e1, "x.py")
.expect("reparse");
let e2 = reg.emit_pretty_with_protocol("python", &s2).expect("emit2");
let e1s = String::from_utf8_lossy(&e1).into_owned();
let e2s = String::from_utf8_lossy(&e2).into_owned();
assert_eq!(
e1, e2,
"Python emit must be a fixed point.\ne1: {e1s}\ne2: {e2s}"
);
assert_eq!(
e1.len(),
e2.len(),
"string literal grew across emit cycle.\ne1: {e1s}\ne2: {e2s}"
);
assert!(
e1s.contains(must_contain),
"expected {must_contain:?} in emit; got {e1s}"
);
});
}
#[test]
fn python_single_quoted_string_is_fixed_point() {
assert_string_fixed_point(b"x = 'hello'\n", "'hello'");
}
#[test]
fn python_double_quoted_string_is_fixed_point() {
assert_string_fixed_point(b"x = \"hello\"\n", "\"hello\"");
}
#[test]
fn python_fstring_is_fixed_point() {
assert_string_fixed_point(b"x = f\"hello\"\n", "\"hello\"");
}