Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarrus1 committed Oct 3, 2023
1 parent 99acaf1 commit 504f9cc
Show file tree
Hide file tree
Showing 12 changed files with 304 additions and 177 deletions.
4 changes: 2 additions & 2 deletions crates/sourcepawn_lsp/benches/bench_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let tmp_dir_path = tmp_dir.path().canonicalize().unwrap();
let current_dir = env::current_dir().unwrap();
let surtimer_path = current_dir.join("test_data/surftimer.zip");
fixture::unzip_file(&surtimer_path, tmp_dir_path.to_str().unwrap()).unwrap();
fixture::unzip_file(&surtimer_path, &tmp_dir_path).unwrap();

let mut fixture = create_fixture(tmp_dir_path.to_str().unwrap()).unwrap();
fixture.push_str("\n\n|\n^");

c.bench_function("surftimer_end2end", |b| {
b.iter(|| {
let _res = black_box(complete(&fixture));
let _res = black_box(complete(&fixture, None));
})
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sourcepawn_lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl LspClient {
log::trace!("Sending request {:?}", request);
self.raw.sender.send(request.into())?;

let response = rx.recv_timeout(Duration::from_secs(5))?;
let response = rx.recv_timeout(Duration::from_secs(15))?;
log::trace!("Received response {:?}", response);
let result = match response.error {
Some(error) => bail!(error.message),
Expand Down
42 changes: 27 additions & 15 deletions crates/sourcepawn_lsp/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use lsp_types::{
notification::{DidOpenTextDocument, Exit, Initialized},
request::ResolveCompletionItem,
request::{Completion, Initialize, Shutdown},
ClientCapabilities, CompletionItem, CompletionItemKind, CompletionParams, CompletionResponse,
DidOpenTextDocumentParams, InitializeParams, InitializedParams, Location, Position, Range,
TextDocumentIdentifier, TextDocumentItem, TextDocumentPositionParams, Url, WorkspaceFolder,
ClientCapabilities, CompletionContext, CompletionItem, CompletionItemKind, CompletionParams,
CompletionResponse, CompletionTriggerKind, DidOpenTextDocumentParams, InitializeParams,
InitializedParams, Location, Position, Range, TextDocumentIdentifier, TextDocumentItem,
TextDocumentPositionParams, Url, WorkspaceFolder,
};
use std::{
env,
Expand Down Expand Up @@ -212,11 +213,14 @@ impl TestBed {
if request.method == "workspace/configuration" {
let mut options = Options::default();
if add_sourcemod {
let current_dir = env::current_dir().unwrap();
let mut current_dir = env::current_dir().unwrap();
// The env depends on if we use the debugger or not.
if current_dir.ends_with("sourcepawn-vscode") {
current_dir = current_dir.join("crates/sourcepawn_lsp/");
}
let sourcemod_path =
current_dir.join("test_data/sourcemod.zip");
unzip_file(&sourcemod_path, destination.to_str().unwrap())
.unwrap();
unzip_file(&sourcemod_path, &destination).unwrap();
options
.includes_directories
.push(destination.clone().join("include/"));
Expand Down Expand Up @@ -310,7 +314,7 @@ impl TestBed {
}
}

pub fn complete(fixture: &str) -> Vec<CompletionItem> {
pub fn complete(fixture: &str, trigger_character: Option<String>) -> Vec<CompletionItem> {
let test_bed = TestBed::new(fixture, true).unwrap();
test_bed
.initialize(
Expand Down Expand Up @@ -341,7 +345,10 @@ pub fn complete(fixture: &str) -> Vec<CompletionItem> {
text_document_position,
partial_result_params: Default::default(),
work_done_progress_params: Default::default(),
context: None,
context: Some(CompletionContext {
trigger_kind: CompletionTriggerKind::TRIGGER_CHARACTER,
trigger_character,
}),
})
.unwrap()
{
Expand All @@ -368,31 +375,36 @@ pub fn complete(fixture: &str) -> Vec<CompletionItem> {
.collect();

// The results of include completions will always change because the paths of tempdir changes.
let tmp_dir_path_str = test_bed.temp_dir_path.to_str().unwrap();
let tmp_sm_dir_path_str = test_bed.temp_sm_dir_path.to_str().unwrap();
for item in &mut items {
if matches!(
item.kind,
Some(CompletionItemKind::FILE) | Some(CompletionItemKind::FOLDER)
) {
item.detail = item.detail.as_mut().map(|detail| {
detail
.replace(tmp_dir_path_str, "")
.replace(tmp_sm_dir_path_str, "")
let detail_path = PathBuf::from(&detail).canonicalize().unwrap();
match detail_path.strip_prefix(&test_bed.temp_dir_path) {
Ok(path) => path,
Err(_) => detail_path
.strip_prefix(&test_bed.temp_sm_dir_path)
.unwrap(),
}
.to_str()
.unwrap()
.to_string()
});
}
}

items
}

pub fn unzip_file(zip_file_path: &PathBuf, destination: &str) -> Result<(), io::Error> {
pub fn unzip_file(zip_file_path: &Path, destination: &Path) -> Result<(), io::Error> {
let file = File::open(zip_file_path)?;
let mut archive = ZipArchive::new(file)?;

for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let dest_path = format!("{}/{}", destination, file.name());
let dest_path = destination.join(file.name());

if file.is_dir() {
std::fs::create_dir_all(&dest_path)?;
Expand Down
10 changes: 9 additions & 1 deletion crates/sourcepawn_lsp/src/providers/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ pub(crate) fn provide_completions(
}
}

if let Some(trigger_char) = line.chars().last() {
let trigger_char = match &params.context {
Some(context) => context
.trigger_character
.clone()
.and_then(|char| char.chars().last()),
None => line.chars().last(),
};

if let Some(trigger_char) = trigger_char {
// The trigger character allows us to fine control which completion to trigger.
match trigger_char {
'<' | '"' | '\'' | '/' | '\\' => {
Expand Down
1 change: 1 addition & 0 deletions crates/sourcepawn_lsp/src/providers/completion/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use regex::Regex;
use std::fs;
use store::Store;

#[derive(Debug, Clone)]
pub(super) struct IncludeStatement {
/// Text inside of the include statement, excluding the traling quotation marks or chevrons.
text: String,
Expand Down
67 changes: 61 additions & 6 deletions crates/sourcepawn_lsp/tests/text_document/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ fn global_variable_1() {
int foo;
|
^"#
^"#,
None
));
}

Expand All @@ -24,7 +25,8 @@ int foo;
#include "bar.sp"
|
^"#
^"#,
None
));
}

Expand All @@ -40,7 +42,8 @@ int foo;
int bar;
|
^"#
^"#,
None
));
}

Expand All @@ -55,7 +58,8 @@ int baz;
%! foo.sp
#include ""
|
^"#
^"#,
Some('"'.to_string())
));
}

Expand All @@ -70,7 +74,8 @@ int baz;
%! foo.sp
#include <>
|
^"#
^"#,
Some('<'.to_string())
));
}

Expand All @@ -85,6 +90,56 @@ int bar;
|
^
%! foo.sp
int foo;"#
int foo;"#,
Some('<'.to_string())
));
}

#[test]
fn include_4() {
assert_json_snapshot!(complete(
r#"
%! bar.sp
int bar;
%! include/baz.inc
#include ""
|
^
%! foo.sp
int foo;"#,
Some('"'.to_string())
));
}

#[test]
fn include_5() {
assert_json_snapshot!(complete(
r#"
%! bar.sp
int bar;
%! include/baz.inc
#include ""
|
^
%! include/foo.inc
int foo;"#,
Some('"'.to_string())
));
}

#[test]
fn include_6() {
assert_json_snapshot!(complete(
r#"
%! main.sp
#include "sub_folder/foo.sp"
int main;
%! sub_folder/foo.sp
#include <sub_folder/>
|
^
%! sub_folder/bar.sp
int bar;"#,
Some("/".to_string())
));
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
---
source: crates/sourcepawn_lsp/tests/text_document/completion.rs
assertion_line: 49
expression: "complete(r#\"\n%! bar.sp\nint bar;\n%! include/baz.inc\nint baz;\n%! foo.sp\n#include \"\"\n |\n ^\"#)"
assertion_line: 52
expression: "complete(r#\"\n%! bar.sp\nint bar;\n%! include/baz.inc\nint baz;\n%! foo.sp\n#include \"\"\n |\n ^\"#,\n Some('\"'.to_string()))"
---
[
{
"label": "bar.sp",
"kind": 17,
"detail": "/bar.sp",
"detail": "bar.sp",
"insertText": "bar.sp"
},
{
"label": "foo.sp",
"kind": 17,
"detail": "/foo.sp",
"detail": "foo.sp",
"insertText": "foo.sp"
},
{
"label": "include",
"kind": 19,
"detail": "/include"
"detail": "include"
}
]
Loading

0 comments on commit 504f9cc

Please sign in to comment.