Skip to content

Commit

Permalink
test: add open-close file mock test for vfs
Browse files Browse the repository at this point in the history
Signed-off-by: He1pa <[email protected]>
  • Loading branch information
He1pa committed Oct 17, 2023
1 parent 26d9b13 commit ef6a2a7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
5 changes: 2 additions & 3 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use lsp_types::{
};
use parking_lot::RwLock;
use ra_ap_vfs::{FileId, Vfs};
use ra_ap_vfs_notify::NotifyHandle;
use std::collections::HashMap;
use std::{sync::Arc, time::Instant};

Expand Down Expand Up @@ -103,7 +102,7 @@ impl LanguageServerState {
let loader = {
let (sender, _receiver) = unbounded::<ra_ap_vfs::loader::Message>();
let handle: ra_ap_vfs_notify::NotifyHandle =
ra_ap_vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
ra_ap_vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
let handle = Box::new(handle) as Box<dyn ra_ap_vfs::loader::Handle>;
Handle { handle, _receiver }
};
Expand Down Expand Up @@ -136,7 +135,7 @@ impl LanguageServerState {
analysis: Analysis::default(),
opened_files: IndexSet::new(),
word_index_map,
loader
loader,
}
}

Expand Down
45 changes: 45 additions & 0 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,51 @@ fn notification_test() {
}
}

#[test]
fn close_file_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut path = root.clone();

path.push("src/test_data/diagnostics.k");

let path = path.to_str().unwrap();
let src = std::fs::read_to_string(path.clone()).unwrap();
let server = Project {}.server(InitializeParams::default());

// Mock open file
server.notification::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::from_file_path(path).unwrap(),
language_id: "KCL".to_string(),
version: 0,
text: src.clone(),
},
},
);

// Mock close file
server.notification::<lsp_types::notification::DidCloseTextDocument>(
lsp_types::DidCloseTextDocumentParams {
text_document: TextDocumentIdentifier {
uri: Url::from_file_path(path).unwrap(),
},
},
);

// Mock reopen file
server.notification::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::from_file_path(path).unwrap(),
language_id: "KCL".to_string(),
version: 0,
text: src,
},
},
);
}

#[test]
fn goto_def_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down

0 comments on commit ef6a2a7

Please sign in to comment.