use crate::file::coordinates::ReadingOffset;
use crate::file::paths::MemoryFiles;
use crate::index::StepOutcome;
use crate::session::{Session, queue_key};
use crate::symbol::Known;
use std::path::{Path, PathBuf};
use super::*;
#[test]
pub(crate) fn a_project_can_decline_to_read_its_other_roots() {
let shared = "struct Shared { int size; };\n";
let a = "#include \"shared.h\"\n";
let b = "#include \"shared.h\"\nint from_b = 1;\n";
let files = MemoryFiles::new()
.with_file("/p/shared.h", shared)
.with_file("/p/a.cpp", a)
.with_file("/p/b.cpp", b)
.with_file(
"/p/.cppls.toml",
"[index]\nread_the_whole_project = false\n",
);
let fixture = Memory::new("a-project-declines", &files);
let mut session = crate::Session::with_config(
"/p",
fixture.providers.clone(),
crate::WatchFilter::new("/p"),
crate::CompilerConfig::default(),
);
session.add_project_files([
std::path::PathBuf::from("/p/a.cpp"),
std::path::PathBuf::from("/p/b.cpp"),
]);
session.did_open("/p/a.cpp", a);
session.index_everything();
session.pretend_a_pause(std::time::Duration::from_millis(1_100));
let first = session
.read_a_program_now()
.expect("the reader's own program");
assert_eq!(first.root, std::path::Path::new("/p/a.cpp"));
session.pretend_a_pause(std::time::Duration::from_secs(60));
assert!(
session.read_a_program_now().is_none(),
"the project declined its unopened roots"
);
assert!(
session.the_wait_before_a_program_may_be_read().is_none(),
"so the pump has nothing to wait for"
);
assert!(
!session
.index()
.the_program_that_read(std::path::Path::new("/p/b.cpp"))
.is_some(),
"and `b.cpp` was not read"
);
}
#[test]
pub(crate) fn a_body_edit_does_not_cost_the_program_again() {
let handle = "#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let main = "#include \"api.h\"\nHWND h;\n";
let body_edit = "#include \"api.h\"\nHWND h;\nint padding;\n";
let directive_edit = "#include \"api.h\"\nHWND h;\nint padding;\n#define PAD 1\n";
let files = MemoryFiles::new()
.with_file("/p/handle.h", handle)
.with_file("/p/api.h", "#include \"handle.h\"\nDECLARE_HANDLE(HWND);\n")
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-body-edit-keeps-the-program", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let reading = session
.read_a_looked_at_unit()
.expect("the reader has a file open, so there is a program to read");
assert!(
reading.files >= 3,
"the program is three files: {reading:?}"
);
assert!(
session.the_wait_before_a_program_may_be_read().is_none(),
"one program per root, until something changes its shape"
);
session.did_change("/p/main.cpp", body_edit);
assert!(
session.the_wait_before_a_program_may_be_read().is_none(),
"a declaration added to a body does not make the program owed again"
);
session.advance(4);
assert!(
session
.index()
.summary(Path::new("/p/main.cpp"))
.is_some_and(|summary| summary
.declarations
.iter()
.any(|fact| fact.name == "padding")),
"the keystroke's own file is read again, by the per-file path"
);
session.did_change("/p/main.cpp", directive_edit);
assert!(
session.the_wait_before_a_program_may_be_read().is_some(),
"a directive that moved makes the program worth reading again"
);
}
#[test]
#[ignore = "a query still answers from the summary's conditional fact — the reading choice does not reach this question yet (design §4.1)"]
pub(crate) fn a_file_read_as_a_program_needs_no_guard_filter() {
let cfg = "#define ON 1\n";
let api = "#include \"cfg.h\"\n#if 0\nstruct Dead { int gone; };\n#endif\nstruct Live { int kept; };\n";
let main = "#include \"api.h\"\nLive live;\n";
let files = MemoryFiles::new()
.with_file("/p/cfg.h", cfg)
.with_file("/p/api.h", api)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("no-guard-filter-needed", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
session
.read_a_looked_at_unit()
.expect("the reader has a file open, so there is a program to read");
let ask = |session: &Session<_>| {
let mut answers: Vec<String> = Vec::new();
for name in ["Dead", "gone", "Live", "kept"] {
let answer = match session.index().definitions(name, Path::new("/p/main.cpp")) {
Known::Yes(found) => format!(
"{:?}",
found
.found
.iter()
.map(|one| (
one.file.to_string_lossy().to_string(),
one.fact.name.clone()
))
.collect::<Vec<_>>()
),
Known::Unknown(reason) => format!("unknown:{reason:?}"),
Known::No => String::from("no"),
};
answers.push(format!("{name} = {answer}"));
}
answers
};
let with = ask(&session);
session.index().forget_the_name_answers();
crate::probes::set_no_guard_filter(true);
let without = ask(&session);
crate::probes::set_no_guard_filter(false);
assert_eq!(
with, without,
"the filter changed an answer, so this file's reading is not the rendering: {with:?}"
);
assert!(
!with.iter().any(|answer| answer.starts_with("Dead = yes")),
"and the branch nobody takes is not an answer at all: {with:?}"
);
}
#[test]
pub(crate) fn a_files_rendering_is_its_reading_and_not_one_answer_beside_another() {
let api = "#ifdef FROM_THE_SOURCE\n\
struct Live { int a; };\n\
#else\n\
struct Live { long b; };\n\
#endif\n";
let main = "#define FROM_THE_SOURCE 1\n#include \"api.h\"\nLive live;\n";
let files = MemoryFiles::new()
.with_file("/p/api.h", api)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("the-rendering-is-the-reading", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let taken = api.find("struct Live { int a; }").expect("the fixture");
let not_taken = api.find("struct Live { long b; }").expect("the fixture");
let before = session
.index()
.definitions("Live", Path::new("/p/main.cpp"));
println!("before the program read: {before:?}");
session
.read_a_looked_at_unit()
.expect("the reader has a file open, so there is a program to read");
let found = session
.index()
.definitions("Live", Path::new("/p/main.cpp"));
let Known::Yes(found) = found else {
panic!("`Live` is declared in a header the source includes: {found:?}");
};
assert_eq!(
found.found.len(),
1,
"the file has been read as a program, so its reading is the reading: {:?}",
found
.found
.iter()
.map(|one| one.fact.range)
.collect::<Vec<_>>()
);
assert_eq!(
found.found[0].fact.range.start_offset, taken,
"the answer is the branch `FROM_THE_SOURCE` selects: {:?}",
found.found[0].fact.range
);
assert_ne!(
found.found[0].fact.range.start_offset, not_taken,
"and not the one the file has when it is read on its own"
);
}
#[test]
pub(crate) fn a_program_is_read_before_its_root_has_a_summary() {
let cfg = "#ifdef A\nstruct X { int from_a; };\n#else\nstruct X { long from_b; };\n#endif\n";
let a = "#define A 1\n#include \"cfg.h\"\n";
let b = "#include \"cfg.h\"\n";
let files = MemoryFiles::new()
.with_file("/p/cfg.h", cfg)
.with_file("/p/a.cpp", a)
.with_file("/p/b.cpp", b);
let fixture = Memory::new("a-program-without-its-summary", &files);
let mut session = fixture.session();
session.did_open("/p/a.cpp", a);
session.did_open("/p/b.cpp", b);
let mut read = Vec::new();
while let Some(reading) = session.read_a_looked_at_unit() {
read.push(reading.root.clone());
}
assert_eq!(
read.len(),
1,
"the focused document is read, and the background tab is not: {read:?}"
);
session.did_change("/p/a.cpp", a);
let reading = session
.read_a_looked_at_unit()
.expect("the newly focused document is read");
read.push(reading.root.clone());
assert_eq!(
read.len(),
2,
"both open files are read, summary or not: {read:?}"
);
}
#[test]
pub(crate) fn a_header_read_in_two_programs_answers_each_its_own_way() {
let cfg = "#ifdef A\nstruct X { int from_a; };\n#else\nstruct X { long from_b; };\n#endif\n";
let a = "#define A 1\n#include \"cfg.h\"\n";
let b = "#include \"cfg.h\"\n";
let files = MemoryFiles::new()
.with_file("/p/cfg.h", cfg)
.with_file("/p/a.cpp", a)
.with_file("/p/b.cpp", b);
let fixture = Memory::new("a-header-in-two-programs", &files);
let mut session = fixture.session();
session.did_open("/p/a.cpp", a);
session.did_open("/p/b.cpp", b);
session.index_everything();
let mut read = Vec::new();
for root in ["/p/a.cpp", "/p/b.cpp"] {
let reading = session
.read_the_unit(Path::new(root))
.unwrap_or_else(|| panic!("{root} reads"));
read.push(reading.root.clone());
}
assert_eq!(read.len(), 2, "both translation units are read: {read:?}");
let from_a = session.index().definitions("X", Path::new("/p/a.cpp"));
let from_b = session.index().definitions("X", Path::new("/p/b.cpp"));
let offset = |known: &Known<crate::ProjectDefinitions>| match known {
Known::Yes(found) => found.found.first().map(|one| one.fact.range.start_offset),
_ => None,
};
assert_eq!(
offset(&from_a),
Some(cfg.find("struct X { int from_a; }").expect("the fixture")),
"`a.cpp` defines `A`, so it sees the `int` branch: {from_a:?}"
);
assert_eq!(
offset(&from_b),
Some(cfg.find("struct X { long from_b; }").expect("the fixture")),
"`b.cpp` does not, so it sees the `long` one: {from_b:?}"
);
}
#[test]
pub(crate) fn a_branch_nobody_takes_reports_nothing_once_the_file_is_cooked() {
let main = "#include \"cfg.h\"\n#if OFF\nstruct Unclosed { int x;\n#endif\nint ok;\n";
let files = MemoryFiles::new()
.with_file("/p/cfg.h", "#define OFF 0\n")
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-branch-nobody-takes", &files);
let mut session = fixture.session();
session.load("/p/main.cpp");
assert!(
session.diagnostics("/p/main.cpp").is_none(),
"nothing has read this file as a compiler reads it, so there is nothing to say about it"
);
session.did_open("/p/main.cpp", main);
session.index_everything();
let cooked = session.diagnostics("/p/main.cpp").expect("the file reads");
assert_eq!(
cooked.reading,
crate::session::DiagnosticReading::Cooked,
"the index holds a reading of this file now"
);
assert!(
cooked.errors.is_empty(),
"and the branch it complained about is not in it: {cooked:?}"
);
assert_eq!(
cooked.unplaced, 0,
"nothing was hidden from the reader either"
);
}
#[test]
pub(crate) fn a_cooked_error_is_placed_in_the_file_not_in_the_rendering() {
let handle = "#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let main = "#include \"handle.h\"\nDECLARE_HANDLE(HWND);\nstruct Unclosed { int x;\n";
let files = MemoryFiles::new()
.with_file("/p/handle.h", handle)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("cooked-errors-are-file-offsets", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let declarations = session
.index()
.cooked_declarations(Path::new("/p/main.cpp"))
.expect("the file was cooked");
assert!(
declarations.iter().any(|fact| fact.name == "HWND__"),
"the rendering is not the file's text: the macro declared a struct this file never wrote: {:?}",
declarations
.iter()
.map(|fact| &fact.name)
.collect::<Vec<_>>()
);
let found = session.diagnostics("/p/main.cpp").expect("the file reads");
assert_eq!(found.reading, crate::session::DiagnosticReading::Cooked);
assert_eq!(found.errors.len(), 1, "{found:?}");
let error = &found.errors[0];
let unclosed = main.find("struct Unclosed").expect("the fixture writes it");
assert!(
error.start >= unclosed,
"the error is about the line that is still unclosed: {error:?}"
);
assert!(
error.end <= main.len(),
"and it is a position in the file — a rendering offset would be past its end: {error:?}"
);
assert_eq!(found.unplaced, 0, "nothing had to be hidden: {found:?}");
}
#[test]
pub(crate) fn a_file_nobody_looked_at_is_not_cooked_and_one_a_request_names_is() {
let main = "#include \"a.h\"\n#include \"b.h\"\n#include \"c.h\"\nA a; B b; C c;\n";
let files = MemoryFiles::new()
.with_file("/p/a.h", "struct A { int x; };\n")
.with_file("/p/b.h", "struct B { int x; };\n")
.with_file("/p/c.h", "struct C { int x; };\n")
.with_file("/p/main.cpp", main)
.with_file("/p/other.cpp", "#include \"a.h\"\nvoid g() { }\n");
let fixture = Memory::new("only-what-is-looked-at-is-cooked", &files);
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/other.cpp")]);
session.did_open("/p/main.cpp", main);
session.index_everything();
assert_eq!(session.pending_work(), 0, "both kinds of work are done");
for looked_at in ["/p/main.cpp", "/p/a.h", "/p/b.h", "/p/c.h"] {
assert!(
session
.index()
.cooked_declarations(Path::new(looked_at))
.is_some(),
"{looked_at} is what the reader is looking at"
);
}
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_none(),
"and a file nobody opened, nobody included and no request named has no reading"
);
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_some(),
"the file a request is about is read the way a compiler reads it"
);
}
#[test]
pub(crate) fn editing_a_header_invalidates_the_readings_that_depend_on_it() {
let ns = "#define BEGIN_NS namespace one {\n#define END_NS }\n";
let api = "#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n";
let other = "#include \"api.h\"\nWidget w;\n";
let files = MemoryFiles::new()
.with_file("/p/ns.h", ns)
.with_file("/p/api.h", api)
.with_file("/p/other.cpp", other);
let fixture = Memory::new("a-cooked-file-goes-stale", &files);
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/other.cpp")]);
session.did_open("/p/api.h", api);
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_some(),
"the fixture starts with the dependent cooked"
);
let changed = "#define BEGIN_NS namespace two {\n#define END_NS }\n";
session.did_open("/p/ns.h", changed);
for stale in ["/p/other.cpp", "/p/api.h"] {
assert!(
session
.index()
.cooked_declarations(Path::new(stale))
.is_none(),
"{stale} was read as the namespace the macro used to open"
);
}
session.index_everything();
session.want_cooked_reading(Path::new("/p/api.h"));
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
assert!(
matches!(
session
.index()
.definition("two::Widget", Path::new("/p/other.cpp")),
Known::Yes(_)
),
"and it is read again, as the new macro says: {:?}",
session
.index()
.definition("two::Widget", Path::new("/p/other.cpp"))
);
}
#[test]
pub(crate) fn editing_a_file_that_defines_no_macros_keeps_its_dependents_readings() {
let api = "struct Widget { int size; };\n";
let other = "#include \"api.h\"\nWidget w;\n";
let files = MemoryFiles::new()
.with_file("/p/api.h", api)
.with_file("/p/other.cpp", other);
let fixture = Memory::new("a-declaration-is-not-an-environment", &files);
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/other.cpp")]);
session.did_open("/p/api.h", api);
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
session.did_open("/p/api.h", "struct Widget { int size; int weight; };\n");
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_some(),
"nothing in this file is part of anyone's environment"
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/api.h"))
.is_none(),
"…while the file itself is re-read, which is not a question about macros"
);
}
#[test]
pub(crate) fn the_first_macro_a_header_defines_invalidates_the_readings_that_include_it() {
let api = "#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n";
let other = "#include \"api.h\"\nWidget w;\n";
let files = MemoryFiles::new()
.with_file("/p/ns.h", "// nothing to say yet\n")
.with_file("/p/api.h", api)
.with_file("/p/other.cpp", other);
let fixture = Memory::new("the-first-macro-of-a-header", &files);
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/other.cpp")]);
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_some(),
"the fixture starts with the dependent cooked"
);
session.did_open(
"/p/ns.h",
"#define BEGIN_NS namespace one {\n#define END_NS }\n",
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_none(),
"the reading was built when `BEGIN_NS` meant nothing"
);
}
#[test]
pub(crate) fn typing_in_a_body_does_not_cost_the_walk_a_translation_unit_is() {
let main = "#include \"b.h\"\nint main() { return A; }\n";
let files = MemoryFiles::new()
.with_file("/p/a.h", "#pragma once\n#define A 1\n")
.with_file(
"/p/b.h",
"#pragma once\n#include \"a.h\"\nstruct B { int b; };\n",
)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-body-edit-is-not-a-walk", &files);
let mut first = fixture.session();
first.add_project_files([PathBuf::from("/p/main.cpp")]);
first.did_open("/p/main.cpp", main);
first.index_everything();
assert!(
first
.index()
.cooked_declarations(Path::new("/p/main.cpp"))
.is_some(),
"the fixture cooked the file"
);
assert_eq!(
first.unit_stats().walked,
0,
"and no timeline was walked for it: {:?}",
first.unit_stats()
);
let over_a_fresh_session = |what: &str, text: &str| {
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/main.cpp")]);
session.did_open("/p/main.cpp", text);
session.index_everything();
let stats = session.unit_stats();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/main.cpp"))
.is_some(),
"{what}: the fixture cooked the file"
);
stats
};
let unedited = over_a_fresh_session("unedited", main);
assert_eq!(
unedited.walked, 0,
"the record the first session wrote answers for the text it was cooked from: {unedited:?}"
);
let typed = format!("{main}\nint more() {{ return A; }}\n");
let edited = over_a_fresh_session("a body edit", &typed);
assert_eq!(
edited.walked, 0,
"a text nobody has cooked composes too, so a keystroke costs no walk: {edited:?}"
);
let again = over_a_fresh_session("the same body edit again", &typed);
assert_eq!(
again.walked, 0,
"and the record the edit's cook wrote answers for it: {again:?}"
);
let with_an_include = "#include \"a.h\"\n#include \"b.h\"\nint main() { return A; }\n";
let moved = over_a_fresh_session("an include above the body", with_an_include);
assert_eq!(
moved.walked, 0,
"an include edit composes too, so it costs no walk: {moved:?}"
);
}
#[test]
pub(crate) fn a_file_read_back_through_its_own_unit_is_not_read_again_by_the_second_pass() {
let ns = "#define BEGIN_NS namespace one {\n#define END_NS }\n";
let api = "#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n";
let typed = format!("{api}void body() {{ int inside; }}\n");
let files = MemoryFiles::new()
.with_file("/p/ns.h", ns)
.with_file("/p/api.h", api);
let fixture = Memory::new("a-file-is-read-with-its-environment", &files);
let second_pass_has = |session: &Session<MemoryFiles>| {
session
.parsed_since_the_last_pass
.iter()
.any(|queued| queued.ends_with("api.h"))
};
let mut cold = fixture.session();
cold.did_open("/p/api.h", api);
cold.advance(1);
assert!(
second_pass_has(&cold),
"with no unit in memory the file is read the way it always was, and the pass still has it: {:?}",
cold.parsed_since_the_last_pass
);
let mut warm = fixture.session();
warm.did_open("/p/api.h", api);
warm.index_everything();
warm.read_the_unit(Path::new("/p/api.h"))
.expect("the program reads");
assert!(
holds_a_unit_for(&warm, "/p/api.h"),
"the program read holds a unit for the file"
);
warm.did_change("/p/api.h", &typed);
warm.advance(64);
assert!(
!second_pass_has(&warm),
"read with the environment of its own closure, so the pass has nothing to repair: {:?}",
warm.parsed_since_the_last_pass
);
}
#[test]
pub(crate) fn a_wave_with_nothing_to_parse_still_carries_the_drains_timelines() {
let count = crate::session::RE_READ_SLICE + 4;
let files = (0..count).fold(MemoryFiles::new(), |files, at| {
files.with_file(format!("/p/f{at}.cpp"), format!("int value_{at} = {at};\n"))
});
let fixture = Memory::new("a-wave-carries-its-drain-timelines", &files);
let mut session = fixture.session();
session.add_project_files((0..count).map(|at| PathBuf::from(format!("/p/f{at}.cpp"))));
let wave = session.prepare_a_wave(count);
session.commit_a_wave(wave, count);
assert_eq!(
session.pending(),
0,
"the fixture is the drain phase: nothing left to index"
);
assert!(
!session.parsed_since_the_last_pass.is_empty(),
"and the second pass still has files: {:?}",
session.parsed_since_the_last_pass
);
let next = session.prepare_a_wave(8);
assert!(
next.prepared.is_empty(),
"there is nothing to parse, which is the state this test is about"
);
assert!(
next.units.is_empty(),
"the pass builds no timelines: it composes"
);
assert!(
!session.parsed_since_the_last_pass.is_empty(),
"and the backlog the next slice drains is not empty"
);
}
#[test]
pub(crate) fn a_quoted_include_is_verified_in_the_directory_it_is_asked_from() {
let files = MemoryFiles::new()
.with_file("/p/a/x.h", "int from_a;\n")
.with_file("/p/b/x.h", "int from_b;\n")
.with_file("/p/a/one.cpp", "#include \"x.h\"\n")
.with_file("/p/b/two.cpp", "#include \"x.h\"\n");
let fixture = Memory::new("a-quoted-include-is-per-directory", &files);
let roots = [PathBuf::from("/p/a/one.cpp"), PathBuf::from("/p/b/two.cpp")];
let mut first = fixture.session();
first.add_project_files(roots.iter().cloned());
first.index_everything();
assert!(
first.stats().rebuilt >= 2,
"the fixture starts cold: {:?}",
first.stats()
);
let mut second = fixture.session();
second.add_project_files(roots.iter().cloned());
let steps = second.advance(16);
let outcomes: Vec<String> = steps
.iter()
.map(|step| {
format!(
"{}: {:?}",
step.path.file_name().unwrap_or_default().to_string_lossy(),
step.outcome
)
})
.collect();
assert!(
steps.iter().all(|step| step.outcome == StepOutcome::Reused),
"nothing may be rebuilt from a cache that holds every one of these files: {outcomes:?}"
);
}
#[test]
pub(crate) fn an_include_added_to_a_header_invalidates_the_readings_that_include_it() {
let files = MemoryFiles::new()
.with_file("/p/macros.h", "#define ANSWER 42\n")
.with_file("/p/api.h", "int declared;\n")
.with_file("/p/other.cpp", "#include \"api.h\"\nint x = ANSWER;\n");
let fixture = Memory::new("an-include-is-an-environment", &files);
let mut session = fixture.session();
session.add_project_files([PathBuf::from("/p/other.cpp")]);
session.want_cooked_reading(Path::new("/p/other.cpp"));
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_some()
);
session.did_open("/p/api.h", "#include \"macros.h\"\nint declared;\n");
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_none(),
"`ANSWER` is now in force where it was not"
);
}
#[test]
pub(crate) fn a_unit_survives_typing_in_a_body_and_not_a_change_to_a_directive() {
let ns = "#define BEGIN_NS namespace one {\n#define END_NS }\n";
let api = "#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n";
let files = MemoryFiles::new()
.with_file("/p/ns.h", ns)
.with_file("/p/api.h", api);
let fixture = Memory::new("a-unit-survives-a-body", &files);
let mut session = fixture.session();
session.did_open("/p/api.h", api);
session.index_everything();
session
.read_the_unit(Path::new("/p/api.h"))
.expect("the program reads");
assert!(
holds_a_unit_for(&session, "/p/api.h"),
"the program read holds a unit for the file"
);
let typed = format!("{api}void body() {{ int inside; }}\n");
session.did_change("/p/api.h", &typed);
assert!(
holds_a_unit_for(&session, "/p/api.h"),
"a body was typed below the last directive"
);
let typed_more = format!("{api}void body() {{ int inside; int more; }}\n");
session.did_change("/p/api.h", &typed_more);
assert!(holds_a_unit_for(&session, "/p/api.h"), "and again");
session.did_change(
"/p/api.h",
&format!("#include \"ns.h\"\n#define EXTRA 1\n{typed_more}"),
);
assert!(
!holds_a_unit_for(&session, "/p/api.h"),
"a directive appeared, so `api.h`'s timeline is out of date"
);
}
pub(crate) fn holds_a_unit_for(session: &Session<MemoryFiles>, path: &str) -> bool {
session
.units
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.held
.contains_key(&queue_key(Path::new(path)))
}
#[test]
pub(crate) fn a_cook_that_cannot_succeed_is_not_queued_again_by_a_request() {
let files = MemoryFiles::new().with_file("/p/a.cpp", "int a;\n");
let fixture = Memory::new("a-refused-cook-is-not-requeued", &files);
let mut session = fixture.session();
session.index_everything();
session.want_cooked_reading(Path::new("/p/absent.cpp"));
let claimed = session.claim_cooking(4);
assert_eq!(claimed.len(), 1, "the request queued it once");
let built = session.materials_for(claimed.clone());
assert!(
built.is_empty(),
"a file the index cannot describe has no materials to cook"
);
session.refuse_the_cooks_that_built_nothing(&claimed, &[]);
assert_eq!(
session.pending_cooking(),
0,
"the refused file left the queue"
);
session.want_cooked_reading(Path::new("/p/absent.cpp"));
assert_eq!(
session.pending_cooking(),
0,
"a request does not resurrect a refusal — that was the loop"
);
session.did_change(Path::new("/p/absent.cpp"), "int b;\n");
assert_eq!(
session.pending_cooking(),
1,
"an edit is what makes a refusal worth retrying"
);
}
#[test]
pub(crate) fn editing_a_header_stales_the_cooked_reading_of_a_file_that_includes_it() {
let files = MemoryFiles::new()
.with_file(
"/p/ns.h",
"#define BEGIN_NS namespace one {\n#define END_NS }\n",
)
.with_file(
"/p/api.h",
"#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n",
);
let fixture = Memory::new("a-header-edit-stales-readings", &files);
let mut session = fixture.session();
session.did_open(
"/p/api.h",
"#include \"ns.h\"\nBEGIN_NS struct Widget { int size; }; END_NS\n",
);
session.index_everything();
assert!(
matches!(
session
.index()
.definition("one::Widget", Path::new("/p/api.h")),
Known::Yes(_)
),
"the fixture reads as the header says: {:?}",
session
.index()
.definition("one::Widget", Path::new("/p/api.h"))
);
session.did_change(
"/p/ns.h",
"#define BEGIN_NS namespace two {\n#define END_NS }\n",
);
assert!(
session.pending_cooking() > 0,
"the open file that includes it is marked for cooking again"
);
session.index_everything();
assert!(
matches!(
session
.index()
.definition("two::Widget", Path::new("/p/api.h")),
Known::Yes(_)
),
"and the new reading is the one in the index: {:?}",
session
.index()
.definition("two::Widget", Path::new("/p/api.h"))
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/api.h"))
.is_some_and(|facts| facts
.iter()
.any(|fact| fact.qualified_name() == "two::Widget")),
"the stale reading was replaced, not kept beside it"
);
}
#[test]
pub(crate) fn a_buffer_is_the_text_the_analysis_reads() {
let files = MemoryFiles::new()
.with_file("/p/widget.h", "struct Widget { int old; };\n")
.with_file(
"/p/main.cpp",
"#include \"widget.h\"\nvoid f() { Widget w; }\n",
);
let fixture = Memory::new("buffer-is-the-text", &files);
let mut session = fixture.session();
session.did_open("/p/widget.h", "struct Widget { int fresh; };\n");
session.did_open(
"/p/main.cpp",
"#include \"widget.h\"\nvoid f() { Widget w; }\n",
);
session.index_everything();
let view = session.view("/p/main.cpp").expect("the file reads");
let at = view
.source
.find("Widget w")
.expect("the use is in the text");
match session.definition(&view, ReadingOffset::new(at)) {
Known::Yes(found) => {
assert_eq!(found.file, Path::new("/p/widget.h"));
assert_eq!(found.fact.name, "Widget");
}
other => panic!("the buffer's declaration should be found, got {other:?}"),
}
match session.members_of(&view, "Widget") {
Known::Yes(members) => {
let names: Vec<&str> = members
.own()
.map(|member| member.fact.name.as_str())
.collect();
assert_eq!(
names,
["fresh"],
"the disk says `old` and the disk is not the text"
);
}
other => panic!("the buffer's class should have members, got {other:?}"),
}
assert_eq!(
fixture.text("/p/widget.h").as_deref(),
Some("struct Widget { int fresh; };\n")
);
}
#[test]
pub(crate) fn a_closure_with_an_unindexed_member_has_no_record_key() {
let files = MemoryFiles::new()
.with_file("/p/ns.h", "#define NS one\n")
.with_file("/p/api.h", "#include \"ns.h\"\nNS\n");
let fixture = Memory::new("a-closure-with-an-unindexed-member", &files);
let mut session = fixture.session();
session.did_open("/p/api.h", "#include \"ns.h\"\nNS\n");
session.index_everything();
assert!(
session
.store
.the_closure_fingerprint(Path::new("/p/api.h"))
.is_some(),
"every member of the closure has a summary"
);
session.did_change("/p/ns.h", "#define NS two\n");
assert!(
session
.store
.the_closure_fingerprint(Path::new("/p/api.h"))
.is_none(),
"a member with no summary means the closure has no key, not a key that collides with any other text"
);
session.index_everything();
assert!(
session
.store
.the_closure_fingerprint(Path::new("/p/api.h"))
.is_some(),
"and the key is back once the index has caught up"
);
}
#[test]
pub(crate) fn a_header_that_includes_itself_is_read_once() {
let files = MemoryFiles::new()
.with_file("/p/self.h", "#include \"self.h\"\n#define X 1\n")
.with_file("/p/a.cpp", "#include \"self.h\"\nint a;\n");
let fixture = Memory::new("a-self-include", &files);
let mut session = fixture.session();
session.did_open("/p/a.cpp", "#include \"self.h\"\nint a;\n");
session.index_everything();
let unit = session
.read_the_unit(Path::new("/p/a.cpp"))
.expect("the program reads");
assert_eq!(
unit.files, 2,
"the header is filed once, not to the include-depth limit"
);
}