Files
lucisv/crates/lucisv_server/src/state/tests/large_file.rs
T

96 lines
3.7 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! The large-file fast path: the threshold is strict and `0` turns it off,
//! a fast-path buffer holds the analysis of an empty file and contributes
//! nothing to the index, and a buffer changes mode as it crosses the line.
use super::*;
/// A workspace whose fast path starts at `threshold` bytes.
fn thresholded(threshold: u64) -> Workspace {
let mut ws = Workspace::default();
ws.set_client_settings(ClientSettings {
large_file_threshold: threshold,
..ClientSettings::default()
});
ws
}
#[test]
fn the_threshold_is_strict_at_the_boundary() {
let sizes = [99, 100, 101];
let large: Vec<bool> = sizes
.iter()
.map(|&size| {
let mut ws = thresholded(100);
ws.set_document(uri("file:///rtl/big.v"), filler(size));
ws.document(&uri("file:///rtl/big.v")).expect("open").large
})
.collect();
assert_eq!(large, [false, false, true], "under, at, over");
}
#[test]
fn a_threshold_of_zero_turns_the_fast_path_off_entirely() {
let mut ws = thresholded(0);
ws.set_document(uri("file:///rtl/big.v"), filler(200_000));
let doc = ws.document(&uri("file:///rtl/big.v")).expect("open");
assert!(!doc.large);
assert!(!doc.tokens.is_empty(), "it really was lexed");
}
#[test]
fn a_fast_path_buffer_holds_the_analysis_of_an_empty_file() {
let mut ws = thresholded(100);
ws.set_document(uri("file:///rtl/big.v"), filler(500));
let doc = ws.document(&uri("file:///rtl/big.v")).expect("open");
assert!(doc.large);
assert!(doc.tokens.is_empty(), "nothing was lexed");
assert!(doc.diagnostics.is_empty(), "nothing was parsed");
assert!(doc.semantics.symbols().is_empty(), "nothing was analyzed");
assert!(doc.defines.entries().is_empty(), "no defines were collected");
// But the two things the fast path *does* build are real.
assert_eq!(doc.text.len(), 500);
assert!(doc.line_index.line_count() > 1);
}
#[test]
fn a_fast_path_buffer_keeps_the_on_disk_index_record() {
let tree = Tree::new("largefile");
tree.write("pkg.sv", "package p;\n localparam ON_DISK = 1;\nendpackage\n");
let path = tree.path.join("pkg.sv");
let mut ws = thresholded(100);
ws.open_whole_workspace(vec![tree.path.clone()]);
assert_eq!(ws.index().member("p", "ON_DISK").len(), 1, "premise: disk was indexed");
// The same file, opened as a buffer the fast path takes, keeps the on-disk
// record (subject to `maxFileSize`) without parsing the huge live text.
let mut text = String::from("package p;\n localparam IN_BUFFER = 1;\nendpackage\n");
text.push_str(&" ".repeat(200));
ws.set_document(crate::uri::path_to_uri(&path).expect("uri"), text);
ws.rebuild_index();
assert_eq!(ws.index().member("p", "IN_BUFFER").len(), 0, "the buffer was not parsed");
assert_eq!(
ws.index().member("p", "ON_DISK").len(),
1,
"disk index record stays present when an open buffer is over largeFile.threshold"
);
}
#[test]
fn a_buffer_edited_past_the_threshold_changes_mode_and_back() {
let mut ws = thresholded(200);
let file = uri("file:///rtl/grow.v");
ws.set_document(file.clone(), "module m;\nendmodule\n".into());
assert!(!ws.document(&file).expect("open").large);
ws.set_document(file.clone(), filler(1_000));
assert!(ws.document(&file).expect("open").large, "grew past the threshold");
ws.set_document(file.clone(), "module m;\nendmodule\n".into());
let doc = ws.document(&file).expect("open");
assert!(!doc.large, "and shrank back under it");
assert!(!doc.tokens.is_empty(), "with a full analysis again");
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com