use std::path::PathBuf;
use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
#[test]
fn a_scope_opened_by_another_files_body_is_in_the_rendering() {
let files = MemoryFiles::new()
.with_file(
"/p/open.h",
"#pragma once\n#define _STD_BEGIN namespace std {\n#define _STD_END }\n",
)
.with_file(
"/p/widget.h",
"#include \"open.h\"\n_STD_BEGIN\nstruct Widget {\n int size;\n};\n_STD_END\n",
)
.with_file(
"/p/main.cpp",
"#include \"widget.h\"\nint f() { std::Widget w; return w.size; }\n",
);
let mut session = Session::with_config(
"/p",
SessionFiles::new(OpenDocuments::new(), files),
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([
PathBuf::from("/p/open.h"),
PathBuf::from("/p/widget.h"),
PathBuf::from("/p/main.cpp"),
]);
session.index_everything();
session.cook("/p/widget.h").expect("widget.h cooks");
let rendered = session
.rendering_of("/p/widget.h")
.expect("widget.h has a rendering");
println!("--- rendering of widget.h ---\n{}", rendered.text);
let summary = session
.index()
.summary(std::path::Path::new("/p/widget.h"))
.expect("indexed");
let widget = summary
.declarations
.iter()
.find(|fact| fact.name == "Widget")
.expect("Widget is a fact");
println!("Widget: scope {:?}, guard {:?}", widget.scope, widget.guard);
println!("macro_readings: {}", summary.macro_readings.len());
let empty = cpp_code_analysis::MemoryFiles::new();
let config = CompilerConfig::default();
let indexer = cpp_code_analysis::FileIndexer::new(&empty, &config);
let read = indexer.index_rendering(
std::path::Path::new("/p/widget.h"),
&rendered,
cpp_code_analysis::SummaryKey::new(0, 0),
);
let from_the_rendering = read
.summary
.declarations
.iter()
.find(|fact| fact.name == "Widget");
println!(
"from the rendering: Widget scope {:?}, {} declaration(s), {} include(s)",
from_the_rendering.and_then(|fact| fact.scope.clone()),
read.summary.declarations.len(),
read.summary.includes.len()
);
assert!(
rendered.text.contains("namespace std"),
"the body of _STD_BEGIN is expanded: {}",
rendered.text
);
}