Skip to content

Commit

Permalink
test: add lsp fmt uint test
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Aug 23, 2023
1 parent e01b62d commit 5fac589
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 18 deletions.
25 changes: 25 additions & 0 deletions kclvm/tools/src/LSP/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use kclvm_tools::format::{format_source, FormatOptions};
use lsp_types::{Position, Range, TextEdit};

pub(crate) fn format_single_file(
file: String,
src: String,
) -> anyhow::Result<Option<Vec<TextEdit>>> {
let (source, is_formatted) = format_source(
&file,
&src,
&FormatOptions {
omit_errors: true,
..Default::default()
},
)
.map_err(|err| anyhow::anyhow!("Formmatting failed: {}", err))?;
if is_formatted {
Ok(Some(vec![TextEdit {
range: Range::new(Position::new(0, 0), Position::new(u32::MAX, u32::MAX)),
new_text: source,
}]))
} else {
Ok(None)
}
}
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod config;
mod db;
mod dispatcher;
mod find_ref;
mod formatting;
mod from_lsp;
mod notification;
mod state;
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod state;
mod to_lsp;
mod util;

mod formatting;
#[cfg(test)]
mod tests;

Expand Down
20 changes: 3 additions & 17 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crossbeam_channel::Sender;
use kclvm_tools::format::{format_source, FormatOptions};

use lsp_types::{CodeAction, CodeActionKind, CodeActionOrCommand, Position, Range, TextEdit};
use ra_ap_vfs::VfsPath;
use std::{collections::HashMap, time::Instant};
Expand All @@ -9,6 +9,7 @@ use crate::{
db::AnalysisDatabase,
dispatcher::RequestDispatcher,
document_symbol::document_symbol,
formatting::format_single_file,
from_lsp::{self, file_path_from_url, kcl_pos},
goto_def::goto_definition,
hover, quick_fix,
Expand Down Expand Up @@ -74,22 +75,7 @@ pub(crate) fn handle_formatting(
) -> anyhow::Result<Option<Vec<TextEdit>>> {
let file = file_path_from_url(&params.text_document.uri)?;
let src = std::fs::read_to_string(file.clone())?;
match format_source(&file, &src, &FormatOptions::default()) {
Ok((source, is_formatted)) => {
if is_formatted {
Ok(Some(vec![TextEdit {
range: Range::new(Position::new(0, 0), Position::new(u32::MAX, u32::MAX)),
new_text: source,
}]))
} else {
Ok(None)
}
}
Err(err) => {
log_message(format!("Format failed: {err}"), &sender)?;
Ok(None)
}
}
format_single_file(file, src)
}

/// Called when a `GotoDefinition` request was received.
Expand Down
74 changes: 73 additions & 1 deletion kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use std::process::Command;
Expand Down Expand Up @@ -33,6 +32,7 @@ use parking_lot::RwLock;

use crate::completion::KCLCompletionItem;
use crate::document_symbol::document_symbol;
use crate::formatting::format_single_file;
use crate::from_lsp::file_path_from_url;
use crate::hover::hover;
use crate::quick_fix::quick_fix;
Expand Down Expand Up @@ -1241,3 +1241,75 @@ fn goto_import_external_file_test() {
let res = goto_definition(&program, &pos, &prog_scope);
assert!(res.is_some());
}

#[test]
fn formmat_signle_file_test() {
const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".golden";
const TEST_CASES: &[&str; 17] = &[
"assert",
"check",
"blankline",
"breakline",
"codelayout",
"collection_if",
"comment",
"comp_for",
// "empty",
"import",
"indent",
"inline_comment",
"lambda",
"quant",
"schema",
"string",
"type_alias",
"unary",
];

let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let test_file = path;
let test_dir = test_file
.parent()
.unwrap()
.join("format")
.join("test_data")
.join("format_data");
for case in TEST_CASES {
let test_file = test_dir
.join(format!("{}{}", case, FILE_INPUT_SUFFIX))
.to_str()
.unwrap()
.to_string();
let test_src = std::fs::read_to_string(&test_file).unwrap();
let got = format_single_file(test_file, test_src).unwrap().unwrap();
let data_output = std::fs::read_to_string(
&test_dir
.join(format!("{}{}", case, FILE_OUTPUT_SUFFIX))
.to_str()
.unwrap()
.to_string(),
)
.unwrap();

#[cfg(target_os = "windows")]
let data_output = data_output.replace("\r\n", "\n");

let expect = vec![TextEdit {
range: Range::new(Position::new(0, 0), Position::new(u32::MAX, u32::MAX)),
new_text: data_output,
}];

assert_eq!(expect, got);
}

// empty test case, without change after fmt
let test_file = test_dir
.join(format!("{}{}", "empty", FILE_INPUT_SUFFIX))
.to_str()
.unwrap()
.to_string();
let test_src = std::fs::read_to_string(&test_file).unwrap();
let got = format_single_file(test_file, test_src).unwrap();
assert_eq!(got, None)
}

0 comments on commit 5fac589

Please sign in to comment.