use std::path::{Path, PathBuf};
use cpp_code_analysis::{
DiagnosticCode, DiskFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
fn corpus_root() -> Option<PathBuf> {
let named = std::env::var_os("CPPLS_UE_CORPUS")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\Users\zc\Desktop\UE-like"));
named.join(".cppls.toml").is_file().then_some(named)
}
fn findings_of(session: &Session<DiskFiles>, path: &Path) -> Vec<(DiagnosticCode, String)> {
session
.diagnostics(path)
.map(|found| {
found
.checks
.iter()
.map(|finding| (finding.code, finding.name.clone()))
.collect()
})
.unwrap_or_default()
}
#[test]
#[ignore = "a measurement of a project on disk, like the corpora"]
fn the_ue_like_project() {
let Some(root) = corpus_root() else {
println!("no UE-like project at the default path, so nothing to measure");
return;
};
let mut session = Session::open(
root.clone(),
SessionFiles::new(OpenDocuments::new(), DiskFiles),
WatchFilter::new(&root),
);
let files: Vec<PathBuf> = session
.project_files()
.iter()
.filter(|path| {
path.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| matches!(extension, "h" | "cpp"))
})
.cloned()
.collect();
session.add_project_files(files.clone());
session.index_everything();
let mut steps = 0usize;
while session.pending_work() > 0 && steps < 20_000 {
session.advance(64);
steps += 1;
}
println!("\n--- one line per file");
let mut by_file: std::collections::BTreeMap<String, Vec<(DiagnosticCode, String)>> =
std::collections::BTreeMap::new();
for path in &files {
let name = path
.strip_prefix(&root)
.unwrap_or(path)
.to_string_lossy()
.replace('\\', "/");
session.cook(path);
let errors = session
.diagnostics(path)
.map(|found| found.errors.len())
.unwrap_or(0);
let findings = findings_of(&session, path);
println!(
"{name:<24} {errors} parse error(s), {} finding(s)",
findings.len()
);
by_file.insert(name, findings);
}
println!("\n--- the convention findings");
for (file, findings) in &by_file {
for (code, name) in findings {
if matches!(
code,
DiagnosticCode::UeNaming | DiagnosticCode::UeGenerateBody
) {
println!(" {file}: {code:?} on `{name}`");
}
}
}
for name in ["Source/Game/Clean.h", "Source/Game/Clean.cpp"] {
let findings = by_file.get(name).cloned().unwrap_or_default();
let convention: Vec<&(DiagnosticCode, String)> = findings
.iter()
.filter(|(code, _)| {
matches!(
code,
DiagnosticCode::UeNaming | DiagnosticCode::UeGenerateBody
)
})
.collect();
assert!(
convention.is_empty(),
"`{name}` follows the engine's conventions: {convention:?}"
);
}
let bad = by_file
.get("Source/Game/Bad.h")
.cloned()
.unwrap_or_default();
let naming: Vec<&str> = bad
.iter()
.filter(|(code, _)| *code == DiagnosticCode::UeNaming)
.map(|(_, name)| name.as_str())
.collect();
assert_eq!(
naming,
vec!["MyActor", "isReady", "MyData", "State"],
"every violated prefix is reported, in document order: {bad:?}"
);
let missing: Vec<&str> = bad
.iter()
.filter(|(code, _)| *code == DiagnosticCode::UeGenerateBody)
.map(|(_, name)| name.as_str())
.collect();
assert_eq!(
missing,
vec!["MyActor"],
"the class without GENERATED_BODY() is reported: {bad:?}"
);
}