use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
file::coordinates::FileOffset,
};
const SOURCE: &str = "\
#define WIDTH 10
#define MAX(a, b) ((a) > (b) ? (a) : (b))
void f() {
int n = MAX;
int w = WIDTH;
}
";
fn session() -> (Session<MemoryFiles>, std::path::PathBuf) {
let memory = MemoryFiles::new().with_file("/p/a.cpp", SOURCE);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([std::path::PathBuf::from("/p/a.cpp")]);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
(session, std::path::PathBuf::from("/p/a.cpp"))
}
#[test]
fn a_macro_is_offered_by_completion() {
let (session, path) = session();
let view = session.view(&path).expect("the file is cooked");
let after = SOURCE.find("MAX;").expect("the fixture uses it") + 3;
let set = session.completions(&view, FileOffset::new(after));
let offered: Vec<String> = set.items.iter().map(|item| item.label.clone()).collect();
println!("offered {} item(s): {offered:?}", offered.len());
assert!(
offered.iter().any(|label| label == "MAX"),
"`MAX` is defined in this file and should be offered: {offered:?}"
);
let after_wid = SOURCE.find("WIDTH;").expect("the fixture uses it") + 3;
let set = session.completions(&view, FileOffset::new(after_wid));
let offered: Vec<String> = set.items.iter().map(|item| item.label.clone()).collect();
assert!(
offered.iter().any(|label| label == "WIDTH"),
"…and so should `WIDTH`: {offered:?}"
);
}
#[test]
fn a_macros_use_has_a_hover() {
let (session, path) = session();
let view = session.view(&path).expect("the file is cooked");
let at = SOURCE.find("MAX;").expect("the fixture uses it") + 1;
let found = session.macro_at_a_file_offset(&view, FileOffset::new(at));
println!("macro_at_a_file_offset at the use: {found:?}");
assert!(
matches!(found, cpp_code_analysis::Known::Yes(_)),
"a macro's use has a definition to point at"
);
}
#[test]
fn an_uncalled_function_like_macro_survives_the_cook() {
let (session, path) = session();
let view = session.view(&path).expect("the file is cooked");
let at = SOURCE.find("MAX;").expect("the fixture uses it") + 1;
let reading = view
.reading_offset_of(FileOffset::new(at))
.expect("the offset maps");
println!("file offset {at} → reading {}", reading.get());
assert!(
view.source.contains("MAX;"),
"`MAX` is not called, so the cook keeps its name in the rendering"
);
}
#[test]
fn completion_at_four_positions() {
let (session, path) = session();
let view = session.view(&path).expect("the file is cooked");
let ask = |label: &str, at: usize| {
let set = session.completions(&view, FileOffset::new(at));
let labels: Vec<String> = set.items.iter().map(|item| item.label.clone()).collect();
println!(
"{label:<28} offset {at:>3} → {} item(s){}{}",
labels.len(),
if labels.is_empty() { "" } else { ": " },
labels
.iter()
.take(6)
.cloned()
.collect::<Vec<_>>()
.join(", ")
);
};
ask("after `MAX` in `= MAX;`", SOURCE.find("MAX;").unwrap() + 3);
ask("inside `MA`", SOURCE.find("MAX;").unwrap() + 2);
ask(
"after `WIDTH` in `= WIDTH;`",
SOURCE.find("WIDTH;").unwrap() + 5,
);
ask("start of `int w`", SOURCE.find(" int w").unwrap() + 4);
ask(
"inside the body, after `{`",
SOURCE.find("void f() {").unwrap() + 10,
);
}
#[test]
fn a_headers_macro_is_offered() {
use cpp_code_analysis::{
CompilerConfig, MemoryFiles, OpenDocuments, Session, SessionFiles, WatchFilter,
};
let header = "#define FEATURE_ENABLED 1\n";
let source = "#include \"config.h\"\nvoid f() {\n int n = FEATURE;\n}\n";
let memory = MemoryFiles::new()
.with_file("/p/config.h", header)
.with_file("/p/a.cpp", source);
let providers = SessionFiles::new(OpenDocuments::new(), memory);
let mut session = Session::with_config(
"/p",
providers,
WatchFilter::new("/p"),
CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from("/p/a.cpp"),
std::path::PathBuf::from("/p/config.h"),
]);
session.want_cooked_reading(std::path::Path::new("/p/a.cpp"));
session.index_everything();
let view = session.view("/p/a.cpp").expect("the file is cooked");
let at = source.find("FEATURE;").expect("the fixture uses it") + 3;
let set = session.completions(&view, FileOffset::new(at));
let labels: Vec<String> = set.items.iter().map(|item| item.label.clone()).collect();
println!("offered at `FEATURE`: {labels:?}");
assert!(
labels.iter().any(|label| label == "FEATURE_ENABLED"),
"a header's `#define` is a name this file can use: {labels:?}"
);
}
#[test]
fn a_function_like_macro_is_offered_with_its_parenthesis() {
let (session, path) = session();
let view = session.view(&path).expect("the file is cooked");
let at = SOURCE.find("MAX;").expect("the fixture uses it") + 3;
let set = session.completions(&view, FileOffset::new(at));
let item = set
.items
.iter()
.find(|item| item.label == "MAX")
.expect("`MAX` is offered");
println!("MAX: {item:?}");
let written = format!("{item:?}");
assert!(
written.contains("insert_text") || written.contains("MAX("),
"the item says the macro takes arguments: {written}"
);
}