Skip to content

Commit

Permalink
tests: populate symbols and fields in mock initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 committed Dec 2, 2024
1 parent e468cc5 commit 136fc9a
Showing 1 changed file with 120 additions and 51 deletions.
171 changes: 120 additions & 51 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ mod tests {
use serde_json::to_value;

use lazy_static::lazy_static;
use std::{collections::BTreeMap, sync::Arc};
use std::{
collections::{BTreeMap, HashSet},
sync::Arc,
};
use tree_sitter::Parser;

use tower::{Service, ServiceExt};
Expand All @@ -30,7 +33,7 @@ mod tests {
LspService,
};

use crate::{Backend, Options, QUERY_LANGUAGE, SERVER_CAPABILITIES};
use crate::{Backend, Options, SymbolInfo, QUERY_LANGUAGE, SERVER_CAPABILITIES};

lazy_static! {
static ref TEST_URI: Url = Url::parse("file:///tmp/test.scm").unwrap();
Expand Down Expand Up @@ -147,8 +150,10 @@ mod tests {
}
}

/// Initialize a test server, populating it with fake documents denoted by (uri, text) pairs.
async fn initialize_server(documents: &[(Url, &str)]) -> (LspService<Backend>, Response) {
/// Initialize a test server, populating it with fake documents denoted by (uri, text, symbols, fields) tuples.
async fn initialize_server(
documents: &[(Url, &str, Vec<SymbolInfo>, Vec<&str>)],
) -> (LspService<Backend>, Response) {
let mut parser = Parser::new();
parser
.set_language(&QUERY_LANGUAGE)
Expand All @@ -158,25 +163,40 @@ mod tests {
parser_aliases: None,
language_retrieval_patterns: None,
}));
let (mut service, _socket) = LspService::build(|client| Backend {
client,
document_map: DashMap::from_iter(
documents
.iter()
.map(|(uri, source)| (uri.clone(), Rope::from(*source))),
),
cst_map: DashMap::from_iter(
documents
.iter()
.map(|(uri, source)| (uri.clone(), parser.parse(*source, None).unwrap())),
),
symbols_set_map: DashMap::new(),
symbols_vec_map: DashMap::new(),
fields_set_map: DashMap::new(),
fields_vec_map: DashMap::new(),
options,
})
.finish();
let (mut service, _socket) =
LspService::build(|client| Backend {
client,
document_map: DashMap::from_iter(
documents
.iter()
.map(|(uri, source, _, _)| (uri.clone(), Rope::from(*source))),
),
cst_map: DashMap::from_iter(documents.iter().map(|(uri, source, _, _)| {
(uri.clone(), parser.parse(*source, None).unwrap())
})),
symbols_set_map: DashMap::from_iter(documents.iter().map(
|(uri, _, symbols, _)| (uri.clone(), HashSet::from_iter(symbols.clone())),
)),
symbols_vec_map: DashMap::from_iter(
documents
.iter()
.map(|(uri, _, symbols, _)| (uri.clone(), symbols.clone())),
),
fields_set_map: DashMap::from_iter(documents.iter().map(|(uri, _, _, fields)| {
(
uri.clone(),
HashSet::from_iter(fields.iter().map(ToString::to_string)),
)
})),
fields_vec_map: DashMap::from_iter(documents.iter().map(|(uri, _, _, fields)| {
(
uri.clone(),
fields.clone().iter().map(ToString::to_string).collect(),
)
})),
options,
})
.finish();

let init_result = service
.ready()
Expand All @@ -200,13 +220,23 @@ mod tests {
#[case(&[])]
#[case(&[(
TEST_URI.clone(),
SIMPLE_FILE.clone()
SIMPLE_FILE.clone(),
vec![],
vec![],
), (
TEST_URI_2.clone(),
COMPLEX_FILE.clone()
COMPLEX_FILE.clone(),
vec![
SymbolInfo { named: true, label: String::from("identifier") },
SymbolInfo { named: false, label: String::from(";") }
],
vec![
"operator",
"content",
],
)])]
#[tokio::test(flavor = "current_thread")]
async fn test_server_initialize(#[case] documents: &[(Url, &str)]) {
async fn test_server_initialize(#[case] documents: &[(Url, &str, Vec<SymbolInfo>, Vec<&str>)]) {
// Act
let (service, response) = initialize_server(documents).await;

Expand All @@ -219,27 +249,58 @@ mod tests {
..Default::default()
})
);
if documents.is_empty() {
assert!(backend.document_map.is_empty());
assert!(backend.cst_map.is_empty());
} else {
assert_eq!(backend.document_map.len(), documents.len());
assert_eq!(backend.cst_map.len(), documents.len());
for (uri, doc) in documents {
assert_eq!(
backend.document_map.get(uri).unwrap().to_string(),
(*doc).to_string()
);
assert_eq!(
backend
.cst_map
.get(uri)
.unwrap()
.root_node()
.utf8_text((*doc).to_string().as_bytes())
.unwrap(),
(*doc).to_string()
);
assert_eq!(backend.document_map.len(), documents.len());
assert_eq!(backend.cst_map.len(), documents.len());
assert_eq!(backend.symbols_vec_map.len(), documents.len());
assert_eq!(backend.symbols_set_map.len(), documents.len());
assert_eq!(backend.fields_vec_map.len(), documents.len());
assert_eq!(backend.fields_set_map.len(), documents.len());
for (uri, doc, symbols, fields) in documents {
assert_eq!(
backend.document_map.get(uri).unwrap().to_string(),
(*doc).to_string()
);
assert_eq!(
backend
.cst_map
.get(uri)
.unwrap()
.root_node()
.utf8_text((*doc).to_string().as_bytes())
.unwrap(),
(*doc).to_string()
);
assert!(backend
.symbols_vec_map
.get(uri)
.is_some_and(|v| v.len() == symbols.len()));
assert!(backend
.symbols_set_map
.get(uri)
.is_some_and(|v| v.len() == symbols.len()));
for symbol in symbols {
assert!(backend.symbols_vec_map.get(uri).unwrap().contains(symbol));
assert!(backend.symbols_set_map.get(uri).unwrap().contains(symbol));
}
assert!(backend
.fields_vec_map
.get(uri)
.is_some_and(|v| v.len() == fields.len()));
assert!(backend
.fields_set_map
.get(uri)
.is_some_and(|v| v.len() == fields.len()));
for field in fields {
assert!(backend
.fields_vec_map
.get(uri)
.unwrap()
.contains(&field.to_string()));
assert!(backend
.fields_set_map
.get(uri)
.unwrap()
.contains(&field.to_string()));
}
}
}
Expand Down Expand Up @@ -363,7 +424,9 @@ mod tests {
#[case] edits: &[TestEdit],
) {
// Arrange
let mut service = initialize_server(&[(TEST_URI.clone(), original)]).await.0;
let mut service = initialize_server(&[(TEST_URI.clone(), original, vec![], vec![])])
.await
.0;

// Act
service
Expand Down Expand Up @@ -436,7 +499,9 @@ function: (identifier) @function)",
#[case] ranges: &[Coordinate],
) {
// Arrange
let mut service = initialize_server(&[(TEST_URI.clone(), input)]).await.0;
let mut service = initialize_server(&[(TEST_URI.clone(), input, vec![], vec![])])
.await
.0;

// Act
let refs = service
Expand Down Expand Up @@ -529,7 +594,9 @@ function: (identifier) @function)",
#[case] new_name: &str,
) {
// Arrange
let mut service = initialize_server(&[(TEST_URI.clone(), original)]).await.0;
let mut service = initialize_server(&[(TEST_URI.clone(), original, vec![], vec![])])
.await
.0;

// Act
let rename_edits = service
Expand Down Expand Up @@ -586,6 +653,8 @@ function: (identifier) @function)",
r"( node
) @cap
;;;; comment ",
vec![],
vec![],
)])
.await
.0;
Expand Down Expand Up @@ -721,7 +790,7 @@ function: (identifier) @function)",
cursor_position.expect("Expected one <CURSOR> marker in test input, found none");
let cleaned_input = source.replace("<CURSOR>", "");

let mut service = initialize_server(&[(TEST_URI.clone(), &cleaned_input)])
let mut service = initialize_server(&[(TEST_URI.clone(), &cleaned_input, vec![], vec![])])
.await
.0;
let data = service
Expand Down

0 comments on commit 136fc9a

Please sign in to comment.