//! Outline and folding snapshots for the PHP fixtures in `tests/fixtures`. //! //! Each `NAME.php` has a `NAME.snap` next to it holding the rendered //! outline and folding ranges. Run with `LUTRA_BLESS=1` to rewrite the //! snapshots after an intended change, then review the diff. #![allow( clippy::unwrap_used, clippy::indexing_slicing, clippy::string_slice, reason = "tests may panic" )] use std::fmt::Write as _; use std::path::Path; use lutra_syntax::{OutlineItem, Parser, Range, folding, outline}; /// Renders a range as `line:column-line:column`. fn range(range: &Range) -> String { format!( "{}:{}-{}:{}", range.start.line, range.start.column, range.end.line, range.end.column ) } /// Renders outline items one per line, children indented. fn render_items(items: &[OutlineItem], depth: usize, out: &mut String) { for item in items { let detail = item .detail .as_deref() .map(|detail| format!(" {detail}")) .unwrap_or_default(); writeln!( out, "{}{} {}{} @{} sel {}", " ".repeat(depth), item.kind.as_str(), item.name, detail, range(&item.range), range(&item.selection_range) ) .unwrap(); render_items(&item.children, depth + 1, out); } } /// Parses `name.php` and compares the rendering with `name.snap`. fn check(name: &str) { let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures"); let source = std::fs::read_to_string(dir.join(format!("{name}.php"))).unwrap(); let mut parser = Parser::new().unwrap(); let tree = parser.parse(&source); let mut rendered = String::from("# outline\n"); render_items(&outline(&tree), 0, &mut rendered); rendered.push_str("# folding\n"); for fold in folding(&tree) { writeln!(rendered, "{}", range(&fold)).unwrap(); } let snapshot = dir.join(format!("{name}.snap")); if std::env::var_os("LUTRA_BLESS").is_some() { std::fs::write(&snapshot, &rendered).unwrap(); return; } let expected = std::fs::read_to_string(&snapshot) .unwrap_or_default() .replace("\r\n", "\n"); assert_eq!(rendered, expected, "snapshot {name}.snap differs"); } #[test] fn braced_namespaces() { check("braced_namespaces"); } #[test] fn unbraced_namespace() { check("unbraced_namespace"); } #[test] fn scripts_and_anonymous() { check("scripts_and_anonymous"); } #[test] fn broken() { check("broken"); } #[test] fn unicode_positions() { check("unicode_positions"); }