use crate::file::paths::MemoryFiles;
use crate::symbol::Known;
use std::path::Path;
use super::*;
#[test]
pub(crate) fn a_declaration_only_a_macro_makes_is_found_once_the_session_has_cooked_the_file() {
let handle = "#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\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("cooked-reading-is-indexed", &files);
let mut session = fixture.session();
session.did_open("/p/api.h", source);
session.advance(1);
let before = session.index().definition("HWND__", Path::new("/p/api.h"));
assert!(
matches!(before, Known::Unknown(_)),
"the file's own text does not declare what the macro declares: {before:?}"
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/api.h"))
.is_none(),
"and nothing has cooked it yet"
);
session.index_everything();
let after = session.index().definition("HWND__", Path::new("/p/api.h"));
let Known::Yes(found) = after else {
panic!("the cooked reading declares it: {after:?}");
};
assert_eq!(found.file, Path::new("/p/api.h"));
let at = found.fact.range;
let written = &source[at.start_offset..at.end_offset()];
assert!(
written.contains("DECLARE_HANDLE"),
"the declaration is reported at the invocation: {written:?}"
);
let cooked = session.cook_the_open_files();
assert_eq!(cooked.len(), 1, "api.h is the open file");
let (path, reading) = &cooked[0];
assert_eq!(path, Path::new("/p/api.h"));
assert!(
reading.declarations >= 2,
"the struct, its member and the typedef are declarations here: {reading:?}"
);
assert!(
reading.only_after_expansion >= 2,
"`HWND__` and `HWND` exist only after expansion: {reading:?}"
);
assert_eq!(reading.mapped.dropped, 0, "every range landed in the file");
}
#[test]
pub(crate) fn a_header_the_user_never_opened_is_cooked_when_a_request_names_it() {
let handle = "#include \"deep.h\"\n#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let api = "#include \"handle.h\"\nDECLARE_HANDLE(HWND);\n";
let main = "#include \"api.h\"\nHWND h;\n";
let files = MemoryFiles::new()
.with_file("/p/deep.h", "#define DEEP 1\n")
.with_file("/p/handle.h", handle)
.with_file("/p/api.h", api)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-named-file-is-cooked", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let after = session
.index()
.definition("HWND__", Path::new("/p/main.cpp"));
let Known::Yes(found) = after else {
panic!("`api.h`'s cooked reading declares it: {after:?}");
};
assert_eq!(
found.file,
Path::new("/p/api.h"),
"the file that invoked the macro, which nobody opened"
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/handle.h"))
.is_some(),
"the level under a direct include is where the library keeps what a reader asks for"
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/deep.h"))
.is_some(),
"the third level is where a header's own class is declared, and it is cooked"
);
session.want_cooked_reading(Path::new("/p/handle.h"));
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/handle.h"))
.is_some(),
"and then it has one"
);
}
#[test]
pub(crate) fn a_file_that_does_not_balance_its_braces_is_named_and_still_read() {
let files = MemoryFiles::new()
.with_file(
"/p/broken.h",
"namespace vc_attributes {\nstruct NotAClosure { int x; };\n",
)
.with_file(
"/p/good.h",
"namespace good {\nstruct Inside { int y; };\n}\n",
)
.with_file(
"/p/main.cpp",
"#include \"broken.h\"\n#include \"good.h\"\nInside i;\n",
);
let fixture = Memory::new("a-unit-that-does-not-balance", &files);
let mut session = fixture.session();
session.did_open(
"/p/main.cpp",
"#include \"broken.h\"\n#include \"good.h\"\nInside i;\n",
);
session.index_everything();
let reading = session
.read_the_unit(Path::new("/p/main.cpp"))
.expect("the unit reads");
assert_eq!(
reading.unbalanced.len(),
1,
"one file does not balance: {reading:?}"
);
assert!(
reading.unbalanced[0].ends_with("broken.h"),
"and it is the one that opens a brace it never closes: {reading:?}"
);
let inside = session
.index()
.cooked_declarations(Path::new("/p/good.h"))
.expect("good.h is part of the program");
let inside = inside
.iter()
.find(|fact| fact.name == "Inside")
.expect("the class is declared there");
assert_eq!(
inside.scope.as_deref(),
Some("good"),
"a scope does not cross a file boundary, so `good.h` is scoped by its own namespace"
);
let broken = session
.index()
.cooked_declarations(Path::new("/p/broken.h"))
.expect("the unbalanced file's own reading is filed too");
let struct_fact = broken
.iter()
.find(|fact| fact.name == "NotAClosure")
.expect("the struct is declared in the file that does not balance");
assert_eq!(
struct_fact.scope.as_deref(),
Some("vc_attributes"),
"and at the scope its own text gives it: {broken:?}"
);
}
#[test]
pub(crate) fn a_single_bracket_attribute_no_longer_props_the_program_open() {
let leaky = "REPEATABLE\n[source_annotation_attribute( 1 )]\nstruct Pre\n{\n int Deref;\n};\n";
let main = "#include \"broken.h\"\n#include \"good.h\"\nInside i;\n";
let files = MemoryFiles::new()
.with_file("/p/broken.h", leaky)
.with_file(
"/p/good.h",
"namespace good {\nstruct Inside { int y; };\n}\n",
)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-scope-that-used-to-leak", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let reading = session
.read_the_unit(Path::new("/p/main.cpp"))
.expect("the unit reads");
assert!(
reading.unbalanced.is_empty(),
"the text balances, and now the parse does too: {reading:?}"
);
assert!(
reading.unbalanced.is_empty(),
"nothing leaked and nothing is out of balance: {reading:?}"
);
let pre = session
.index()
.cooked_declarations(Path::new("/p/broken.h"))
.expect("the reading was filed")
.iter()
.find(|fact| fact.name == "Pre")
.expect("the struct the attribute precedes is declared there");
assert_eq!(
pre.scope, None,
"and it is at file scope, not inside anything"
);
let inside = session
.index()
.cooked_declarations(Path::new("/p/good.h"))
.expect("the reading was filed")
.iter()
.find(|fact| fact.name == "Inside")
.expect("the class is declared there")
.scope
.clone();
assert_eq!(
inside.as_deref(),
Some("good"),
"the file after it is read as itself"
);
}
#[test]
pub(crate) fn a_file_that_does_not_balance_costs_nothing_but_a_name() {
let good = "namespace good {\nstruct Good { int x; };\n";
let other = "struct Other { int y; };\n";
let main = "#include \"good.h\"\n#include \"other.h\"\nOther o;\n";
let files = MemoryFiles::new()
.with_file("/p/good.h", good)
.with_file("/p/other.h", other)
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-scope-that-really-leaks", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let reading = session
.read_the_unit(Path::new("/p/main.cpp"))
.expect("the unit reads");
assert_eq!(
reading.unbalanced.len(),
1,
"one file does not balance, and it is reported: {reading:?}"
);
let other_fact = session
.index()
.cooked_declarations(Path::new("/p/other.h"))
.expect("other.h is part of the program")
.iter()
.find(|fact| fact.name == "Other")
.map(|fact| fact.scope.clone())
.expect("the struct is declared there");
assert_eq!(
other_fact, None,
"the file after the unbalanced one is at file scope, not inside `good::`: {reading:?}"
);
let good_facts = session
.index()
.cooked_declarations(Path::new("/p/good.h"))
.expect("the leaking file's own reading is filed too");
assert!(
good_facts.iter().any(|fact| fact.name == "Good"),
"the struct is declared in the file that does not balance: {good_facts:?}"
);
}
#[test]
pub(crate) fn a_program_whose_files_keep_their_scopes_quarantines_nothing() {
let main = "#include \"a.h\"\n#include \"b.h\"\nint main_variable;\n";
let files = MemoryFiles::new()
.with_file("/p/a.h", "namespace a {\nstruct A { int x; };\n}\n")
.with_file("/p/b.h", "namespace b {\n#include \"c.h\"\n}\n")
.with_file("/p/c.h", "struct C { int z; };\n")
.with_file("/p/main.cpp", main);
let fixture = Memory::new("a-program-that-does-not-leak", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let reading = session
.read_the_unit(Path::new("/p/main.cpp"))
.expect("the unit reads");
assert!(
reading.unbalanced.is_empty(),
"a scope that closes around an `#include` is in balance: {reading:?}"
);
let c = session
.index()
.cooked_declarations(Path::new("/p/c.h"))
.expect("filed")
.iter()
.find(|fact| fact.name == "C")
.expect("declared")
.scope
.clone();
assert_eq!(
c.as_deref(),
Some("b"),
"the namespace `b.h` opens around its include still holds `C`"
);
}
#[test]
pub(crate) fn a_unit_read_reads_the_whole_program_once() {
let handle = "#include \"deep.h\"\n#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let api = "#include \"handle.h\"\nDECLARE_HANDLE(HWND);\n";
let main = "#include \"api.h\"\nHWND h;\n";
let files = MemoryFiles::new()
.with_file("/p/deep.h", "#define DEEP 1\n")
.with_file("/p/handle.h", handle)
.with_file("/p/api.h", api)
.with_file("/p/main.cpp", main)
.with_file("/p/other.cpp", "#include \"other.h\"\nvoid g() { }\n")
.with_file("/p/other.h", "struct Unrelated { int y; };\n");
let fixture = Memory::new("a-unit-read", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
assert!(
session
.index()
.cooked_declarations(Path::new("/p/handle.h"))
.is_some(),
"the level under a direct include is cooked"
);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/deep.h"))
.is_some(),
"and the level under that is cooked too — the bound moved when `std::string` needed it"
);
let reading = session
.read_the_unit(Path::new("/p/main.cpp"))
.expect("the unit reads");
assert_eq!(reading.root, Path::new("/p/main.cpp"));
assert!(
reading.files >= 3,
"the program is three files: {reading:?}"
);
assert_eq!(
reading.files_with_tokens, 2,
"two of them write tokens; a file of directives writes none: {reading:?}"
);
assert_eq!(reading.missing, 0, "every file had text: {reading:?}");
for (file, why) in [
(
"/p/handle.h",
"a file the program is made of is read with it",
),
("/p/deep.h", "including the one the per-file bound left out"),
] {
assert!(
session
.index()
.cooked_declarations(Path::new(file))
.is_some(),
"{why}"
);
}
let after = session
.index()
.definition("HWND__", Path::new("/p/main.cpp"));
let Known::Yes(found) = after else {
panic!("`api.h`'s cooked reading declares it: {after:?}");
};
assert_eq!(found.file, Path::new("/p/api.h"));
assert!(
session
.index()
.cooked_declarations(Path::new("/p/other.cpp"))
.is_none(),
"a file no program reads and nobody opens has no reading"
);
}
#[test]
pub(crate) fn a_program_is_read_when_the_reader_stops_editing() {
let handle = "#include \"mid.h\"\n#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let main = "#include \"api.h\"\nHWND h;\n";
let files = MemoryFiles::new()
.with_file("/p/deep.h", "#define DEEP 1\n")
.with_file("/p/mid.h", "#include \"deep.h\"\nstruct Mid { int x; };\n")
.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-program-when-the-typing-stops", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
let steps = session.advance(4);
assert!(
!session
.index()
.cooked_declarations(Path::new("/p/deep.h"))
.is_some(),
"nothing read the program while the reader was typing: {:?}",
paths(&steps)
);
std::thread::sleep(
crate::session::UNTIL_A_UNIT_MAY_BE_READ + std::time::Duration::from_millis(100),
);
session.advance(1);
assert!(
session
.index()
.cooked_declarations(Path::new("/p/deep.h"))
.is_some(),
"the program's own files are read with it, two includes below the file the reader opened"
);
let found = session
.index()
.definition("HWND__", Path::new("/p/main.cpp"));
let Known::Yes(found) = found else {
panic!("the macro's declaration is in the index after the program was read: {found:?}");
};
assert_eq!(
found.file,
Path::new("/p/api.h"),
"filed under the file that invoked the macro"
);
}
#[test]
pub(crate) fn a_program_of_any_size_is_read() {
let line = "// padding padding padding padding\n";
let big = line.repeat((9 * 1024 * 1024) / line.len() + 1);
assert!(
big.len() > 8 * 1024 * 1024,
"larger than the ceiling that was: {} bytes",
big.len()
);
let handle = "#include \"big.h\"\n#include \"mid.h\"\n#define DECLARE_HANDLE(name) struct name##__ { int unused; }; \
typedef struct name##__ *name\n";
let main = "#include \"api.h\"\nHWND h;\n";
let files = MemoryFiles::new()
.with_file("/p/big.h", big)
.with_file("/p/deep.h", "#define DEEP 1\n")
.with_file("/p/mid.h", "#include \"deep.h\"\nstruct Mid { int x; };\n")
.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-program-of-any-size", &files);
let mut session = fixture.session();
session.did_open("/p/main.cpp", main);
session.index_everything();
session.pretend_a_pause(
crate::session::UNTIL_A_UNIT_MAY_BE_READ + std::time::Duration::from_millis(1),
);
let root = crate::session::queue_key(Path::new("/p/main.cpp"));
match session.the_program_to_read() {
Some(next) => assert_eq!(next.as_path(), Path::new("/p/main.cpp")),
None => assert!(
session.units_read.contains(&root),
"the program is neither read nor owed: the file kept its per-file reading, which is what the old \
size ceiling did"
),
}
}
#[test]
pub(crate) fn a_cook_writes_its_rendering_to_the_disk() {
let source = "#include \"b.h\"\nint main() { return B; }\n";
let files = MemoryFiles::new()
.with_file("/p/b.h", "#define B 1\n")
.with_file("/p/a.cpp", source);
let fixture = Memory::new("a-cook-writes-its-rendering", &files);
let mut session = fixture.session();
session.did_open("/p/a.cpp", source);
session.index_everything();
session.cook("/p/a.cpp").expect("the file cooks");
let (cooked, rendered) = session
.store
.the_stored_cooked_reading_with_rendering(Path::new("/p/a.cpp"))
.expect("the record has its rendering");
assert!(!cooked.declarations.is_empty(), "and its facts");
assert!(
!rendered.spans.is_empty(),
"one span per token of the rendering: {} span(s) for {:?}",
rendered.spans.len(),
rendered.text
);
session
.store
.put_the_cooked_reading(Path::new("/p/a.cpp"), &cooked);
assert!(
session
.store
.the_stored_cooked_reading_with_rendering(Path::new("/p/a.cpp"))
.is_some(),
"the rendering the cook wrote survives a facts-only write"
);
}