use crate::file::paths::MemoryFiles;
use crate::symbol::Known;
use std::path::{Path, PathBuf};
use super::*;
#[test]
pub(crate) fn a_rendering_is_not_left_in_the_cache_for_the_next_session() {
let api = "#ifdef FROM_THE_SOURCE\nstruct Live { int a; };\n#else\nstruct 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("a-rendering-is-not-cached", &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 program is read");
assert!(reading.files >= 2, "two files: {reading:?}");
assert!(
session
.index()
.cooked_declarations(Path::new("/p/api.h"))
.is_some_and(|facts| facts.iter().any(|fact| fact.name == "Live")),
"the program's reading of the header is in this session"
);
}
let mut session = fixture.session();
session.add_project_files([std::path::PathBuf::from("/p/api.h")]);
session.index_everything();
let summary = session
.index()
.summary(Path::new("/p/api.h"))
.expect("the header is read")
.clone();
let live: Vec<_> = summary
.declarations
.iter()
.filter(|fact| fact.name == "Live")
.collect();
assert_eq!(
live.len(),
2,
"a summary is the environment-free reading, so both branches are in it: {live:?}"
);
let from_the_source = api.find("struct Live { int a; }").expect("the fixture");
let alone = api.find("struct Live { long b; }").expect("the fixture");
assert!(
live.iter()
.any(|fact| fact.range.start_offset == from_the_source),
"including the branch `main.cpp`'s program took: {live:?}"
);
let found = session.index().definition("Live", Path::new("/p/api.h"));
match found {
Known::Yes(found) => assert_eq!(
found.fact.range.start_offset, alone,
"and a query answers the branch this file has when nothing defines `FROM_THE_SOURCE`"
),
Known::Unknown(crate::UnknownReason::Ambiguous(_)) => {}
other => {
panic!("`Live` is declared in the header, and not as `main.cpp` read it: {other:?}")
}
}
}
#[test]
pub(crate) fn a_refinement_is_served_to_the_next_session() {
let api = "struct Live { int a; };\n";
let main = "#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("a-refinement-is-served", &files);
let roots = [PathBuf::from("/p/main.cpp")];
{
let mut first = fixture.session();
first.add_project_files(roots.iter().cloned());
first.want_refinement(Path::new("/p/api.h"));
first.index_everything();
assert!(first.is_indexed("/p/api.h"), "the closure was followed");
}
let mut second = fixture.session();
second.add_project_files(roots.iter().cloned());
second.want_refinement(Path::new("/p/api.h"));
second.index_everything();
assert_eq!(
second.unit_stats().walked,
0,
"a reading nothing had to repair needs no timeline on the next start: {:?}",
second.unit_stats()
);
assert!(
second.stats().stored_refinements_served >= 1,
"the refinement came from the cache, not from a walk: {:?}",
second.stats()
);
}
#[test]
pub(crate) fn the_sharing_cache_survives_a_restart() {
let handle = "#define DECLARE_HANDLE(name) struct name##__ { int unused; };\n";
let source = "#include \"handle.h\"\nDECLARE_HANDLE(HWND);\n";
let files = MemoryFiles::new()
.with_file("/p/handle.h", handle)
.with_file("/p/api.h", source);
let fixture = Memory::new("the-sharing-cache-survives-a-restart", &files);
let written = {
let mut session = fixture.session();
session.set_sharing_contributions(true);
session.did_open("/p/api.h", source);
session.index_everything();
session
.read_the_unit(std::path::Path::new("/p/api.h"))
.expect("the program reads");
session
.save_the_sharing()
.expect("the cache directory is the fixture's")
};
assert!(
written > 0,
"a run that walked files must have something to write"
);
let empty = fixture.session();
assert_eq!(
empty.walk_contributions_held(),
0,
"a new store holds nothing"
);
let session = fixture.session();
let restored = session.load_the_sharing();
assert_eq!(restored, written, "what was written is what is read back");
assert_eq!(
session.walk_contributions_held(),
restored,
"and it is in the cache the walk consults, not merely counted"
);
if let Some(database) = session.store.database() {
let directory = database.directory().to_path_buf();
assert!(
!directory.join("sharing.bin").exists(),
"the sharing cache is not a file any more: {}",
directory.display()
);
}
}
#[test]
fn a_rendering_is_asked_for_once_per_text() {
let source = "#include \"b.h\"\nint main() { return 0; }\n";
let files = MemoryFiles::new()
.with_file("/p/b.h", "#define B 1\n")
.with_file("/p/a.cpp", source);
let fixture = Memory::new("asked-once-per-text", &files);
let mut session = fixture.session();
session.did_open("/p/a.cpp", source);
session.index_everything();
let asks = |session: &crate::session::Session<MemoryFiles>| asks_of(session);
session.forget_the_renderings();
assert!(
session.view(Path::new("/p/a.cpp")).is_some(),
"the store answers what the table forgot"
);
assert_eq!(asks(&session), 0, "and nothing has to be cooked again");
session.ask_for_a_rendering(Path::new("/p/a.cpp"), source);
assert_eq!(asks(&session), 1, "the miss asks once");
session.ask_for_a_rendering(Path::new("/p/a.cpp"), source);
assert_eq!(asks(&session), 1, "one ask per text, not per query");
}
fn asks_of(session: &crate::session::Session<MemoryFiles>) -> usize {
session
.renderings_asked
.lock()
.map(|asked| asked.len())
.unwrap_or(0)
}