Skip to content

Commit

Permalink
refactor: extract did_open
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Dec 24, 2023
1 parent ed3fd35 commit 55fd6fa
Showing 1 changed file with 54 additions and 91 deletions.
145 changes: 54 additions & 91 deletions crates/typos-lsp/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use tower_lsp::lsp_types::Url;
mod common;
use common::TestServer;

fn initialize_default() -> String {
initialize(None, None)
fn initialize() -> String {
initialize_with(None, None)
}

fn initialize(workspace_folder_uri: Option<&Url>, custom_config: Option<&PathBuf>) -> String {
fn initialize_with(workspace_folder_uri: Option<&Url>, custom_config: Option<&PathBuf>) -> String {
let workspace_folders = workspace_folder_uri.map_or(String::default(), |v| {
format!(
r#",
Expand Down Expand Up @@ -47,12 +47,35 @@ fn initialize(workspace_folder_uri: Option<&Url>, custom_config: Option<&PathBuf
)
}

fn did_open(text: &str) -> String {
did_open_with(text, None)
}

fn did_open_with(text: &str, uri: Option<&Url>) -> String {
format!(
r#"{{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {{
"textDocument": {{
"uri": "{}",
"languageId": "plaintext",
"version": 1,
"text": "{}"
}}
}}
}}"#,
uri.unwrap_or(&Url::parse("file:///diagnostics.txt").unwrap()),
text.replace("\n", "\\n")
)
}

#[test_log::test(tokio::test)]
async fn test_initialize_e2e() {
let mut server = TestServer::new();

similar_asserts::assert_eq!(
server.request(&initialize_default()).await,
server.request(&initialize()).await,
format!(
r#"{{"jsonrpc":"2.0","result":{{"capabilities":{{"codeActionProvider":{{"codeActionKinds":["quickfix"],"workDoneProgress":false}},"textDocumentSync":1,"workspace":{{"workspaceFolders":{{"changeNotifications":true,"supported":true}}}}}},"serverInfo":{{"name":"typos","version":"{}"}}}},"id":1}}"#,
env!("CARGO_PKG_VERSION")
Expand All @@ -62,19 +85,7 @@ async fn test_initialize_e2e() {

#[test_log::test(tokio::test)]
async fn test_code_action() {
let did_open = r#"{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file:///diagnostics.txt",
"languageId": "plaintext",
"version": 1,
"text": "this is an apropriate test\nfo typos\n"
}
}
}
"#;
let did_open = &did_open("this is an apropriate test\nfo typos\n");

let code_action = r#"
{
Expand Down Expand Up @@ -171,7 +182,7 @@ async fn test_code_action() {
"#;

let mut server = TestServer::new();
let _ = server.request(&initialize_default()).await;
let _ = server.request(&initialize()).await;

similar_asserts::assert_eq!(
server.request(did_open).await,
Expand All @@ -193,94 +204,58 @@ async fn test_code_action() {
async fn test_config_file() {
let workspace_folder_uri =
Url::from_file_path(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests")).unwrap();
let diag_txt = workspace_folder_uri.join("tests/diagnostics.txt").unwrap();
let changelog_md = workspace_folder_uri.join("tests/CHANGELOG.md").unwrap();

let did_open_diag_txt = format!(
r#"{{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {{
"textDocument": {{
"uri": "{}/diagnostics.txt",
"languageId": "plaintext",
"version": 1,
"text": "this is an apropriate test\nfo typos\n"
}}
}}
}}
"#,
workspace_folder_uri
);
let did_open_diag_txt =
&did_open_with("this is an apropriate test\nfo typos\n", Some(&diag_txt));

let did_open_changelog = format!(
r#"{{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {{
"textDocument": {{
"uri": "{}/CHANGELOG.md",
"languageId": "plaintext",
"version": 1,
"text": "this is an apropriate test\nfo typos\n"
}}
}}
}}
"#,
workspace_folder_uri
let did_open_changelog_md = &did_open_with(
"this is an apropriate test\nfo typos\n",
Some(&changelog_md),
);

let mut server = TestServer::new();
let _ = server
.request(&initialize(Some(&workspace_folder_uri), None))
.request(&initialize_with(Some(&workspace_folder_uri), None))
.await;

// check "fo" is corrected to "of" because of default.extend-words
similar_asserts::assert_eq!(
server.request(&did_open_diag_txt).await,
format!(
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[{{"data":{{"corrections":["appropriate"]}},"message":"`apropriate` should be `appropriate`","range":{{"end":{{"character":21,"line":0}},"start":{{"character":11,"line":0}}}},"severity":2,"source":"typos"}},{{"data":{{"corrections":["of"]}},"message":"`fo` should be `of`","range":{{"end":{{"character":2,"line":1}},"start":{{"character":0,"line":1}}}},"severity":2,"source":"typos"}}],"uri":"{}/diagnostics.txt","version":1}}}}"#,
workspace_folder_uri
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[{{"data":{{"corrections":["appropriate"]}},"message":"`apropriate` should be `appropriate`","range":{{"end":{{"character":21,"line":0}},"start":{{"character":11,"line":0}}}},"severity":2,"source":"typos"}},{{"data":{{"corrections":["of"]}},"message":"`fo` should be `of`","range":{{"end":{{"character":2,"line":1}},"start":{{"character":0,"line":1}}}},"severity":2,"source":"typos"}}],"uri":"{}","version":1}}}}"#,
diag_txt
),
);

// check changelog is excluded because of files.extend-exclude
similar_asserts::assert_eq!(
server.request(&did_open_changelog).await,
server.request(&did_open_changelog_md).await,
format!(
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[],"uri":"{}/CHANGELOG.md","version":1}}}}"#,
workspace_folder_uri
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[],"uri":"{}","version":1}}}}"#,
changelog_md
),
);
}

#[test_log::test(tokio::test)]
async fn test_custom_config_file() {
let workspace_folder_uri =
Url::from_file_path(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests")).unwrap();

let custom_config = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("custom_typos.toml");

let did_open_diag_txt = format!(
r#"{{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {{
"textDocument": {{
"uri": "{}/diagnostics.txt",
"languageId": "plaintext",
"version": 1,
"text": "this is an apropriate test\nfo typos\n"
}}
}}
}}
"#,
workspace_folder_uri
);
let workspace_folder_uri =
Url::from_file_path(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests")).unwrap();

let diag_txt = workspace_folder_uri.join("tests/diagnostics.txt").unwrap();

let did_open_diag_txt =
&did_open_with("this is an apropriate test\nfo typos\n", Some(&diag_txt));

let mut server = TestServer::new();
let _ = server
.request(&initialize(
.request(&initialize_with(
Some(&workspace_folder_uri),
Some(&custom_config),
))
Expand All @@ -291,30 +266,18 @@ async fn test_custom_config_file() {
similar_asserts::assert_eq!(
server.request(&did_open_diag_txt).await,
format!(
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[{{"data":{{"corrections":["appropriate"]}},"message":"`apropriate` should be `appropriate`","range":{{"end":{{"character":21,"line":0}},"start":{{"character":11,"line":0}}}},"severity":2,"source":"typos"}},{{"data":{{"corrections":["go"]}},"message":"`fo` should be `go`","range":{{"end":{{"character":2,"line":1}},"start":{{"character":0,"line":1}}}},"severity":2,"source":"typos"}}],"uri":"{}/diagnostics.txt","version":1}}}}"#,
workspace_folder_uri
r#"{{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{{"diagnostics":[{{"data":{{"corrections":["appropriate"]}},"message":"`apropriate` should be `appropriate`","range":{{"end":{{"character":21,"line":0}},"start":{{"character":11,"line":0}}}},"severity":2,"source":"typos"}},{{"data":{{"corrections":["go"]}},"message":"`fo` should be `go`","range":{{"end":{{"character":2,"line":1}},"start":{{"character":0,"line":1}}}},"severity":2,"source":"typos"}}],"uri":"{}","version":1}}}}"#,
diag_txt
),
);
}

#[test_log::test(tokio::test)]
async fn test_unicode_diagnostics() {
let did_open = r#"{
"jsonrpc": "2.0",
"method": "textDocument/didOpen",
"params": {
"textDocument": {
"uri": "file:///diagnostics.txt",
"languageId": "plaintext",
"version": 1,
"text": "¿Qué hace él?"
}
}
}
"#;
let did_open = &did_open("¿Qué hace él?");

let mut server = TestServer::new();
let _ = server.request(&initialize_default()).await;
let _ = server.request(&initialize()).await;

// start position should count graphemes with multiple code points as one visible character
similar_asserts::assert_eq!(
Expand Down

0 comments on commit 55fd6fa

Please sign in to comment.