Skip to content

Commit c859957

Browse files
authored
Merge pull request #691 from taminomara/gtest
Improve test reporting
2 parents e9b859e + 0f74c1e commit c859957

17 files changed

+1899
-1210
lines changed

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ clap = { version = "4.5.41", features = ["derive", "wrap_help"] }
5050
ansi_term = "0.12.1"
5151
num-traits = { version = "0.2", features = ["std"] }
5252
mimalloc = "0.1.47"
53+
googletest = "0.14.2"

crates/emmylua_ls/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ optional = true
4343
workspace = true
4444
optional = true
4545

46+
[dev-dependencies]
47+
googletest.workspace = true
48+
4649

4750
[[bin]]
4851
name = "emmylua_ls"

crates/emmylua_ls/src/handlers/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ mod request_handler;
2626
mod response_handler;
2727
mod semantic_token;
2828
mod signature_helper;
29-
mod test;
30-
mod test_lib;
3129
mod text_document;
3230
mod workspace;
3331
mod workspace_symbol;
3432

33+
#[cfg(test)]
34+
mod test;
35+
#[cfg(test)]
36+
mod test_lib;
37+
3538
pub use initialized::{ClientConfig, init_analysis, initialized_handler};
3639
use lsp_types::{ClientCapabilities, ServerCapabilities};
3740
pub use notification_handler::on_notification_handler;
Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#[cfg(test)]
22
mod tests {
3-
use crate::handlers::test_lib::ProviderVirtualWorkspace;
3+
use crate::handlers::test_lib::{ProviderVirtualWorkspace, VirtualCodeAction, check};
44
use emmylua_code_analysis::{DiagnosticCode, Emmyrc};
5-
use lsp_types::CodeActionOrCommand;
5+
use googletest::prelude::*;
66

7-
#[test]
8-
fn test_1() {
7+
#[gtest]
8+
fn test_1() -> Result<()> {
99
let mut ws = ProviderVirtualWorkspace::new();
1010
ws.def(
1111
r#"
@@ -14,43 +14,77 @@ mod tests {
1414
"#,
1515
);
1616

17-
let actions = ws
18-
.check_code_action(
19-
r#"
17+
check!(ws.check_code_action(
18+
r#"
2019
---@type Cast1
2120
local A
2221
2322
local _a = A:get(1):get(2):get(3)
2423
"#,
25-
)
26-
.unwrap();
27-
// 6 个禁用 + 2 个修复
28-
assert_eq!(actions.len(), 8);
24+
vec![
25+
VirtualCodeAction {
26+
title: "use cast to remove nil".to_string()
27+
},
28+
VirtualCodeAction {
29+
title: "Disable current line diagnostic (need-check-nil)".to_string()
30+
},
31+
VirtualCodeAction {
32+
title: "Disable all diagnostics in current file (need-check-nil)".to_string()
33+
},
34+
VirtualCodeAction {
35+
title:
36+
"Disable all diagnostics in current project (need-check-nil)".to_string()
37+
},
38+
VirtualCodeAction {
39+
title: "use cast to remove nil".to_string()
40+
},
41+
VirtualCodeAction {
42+
title: "Disable current line diagnostic (need-check-nil)".to_string()
43+
},
44+
VirtualCodeAction {
45+
title: "Disable all diagnostics in current file (need-check-nil)".to_string()
46+
},
47+
VirtualCodeAction {
48+
title:
49+
"Disable all diagnostics in current project (need-check-nil)".to_string()
50+
},
51+
]
52+
));
53+
54+
Ok(())
2955
}
3056

31-
#[test]
32-
fn test_add_doc_tag() {
57+
#[gtest]
58+
fn test_add_doc_tag() -> Result<()> {
3359
let mut ws = ProviderVirtualWorkspace::new();
3460
let mut emmyrc = Emmyrc::default();
3561
emmyrc
3662
.diagnostics
3763
.enables
3864
.push(DiagnosticCode::UnknownDocTag);
3965
ws.analysis.update_config(emmyrc.into());
40-
let actions = ws
41-
.check_code_action(
42-
r#"
66+
check!(ws.check_code_action(
67+
r#"
4368
---@class Cast1
4469
---@foo bar
4570
"#,
46-
)
47-
.unwrap();
48-
// 3 disable + 1 fix
49-
assert_eq!(actions.len(), 4);
71+
vec![
72+
VirtualCodeAction {
73+
title: "Add @foo to the list of known tags".to_string()
74+
},
75+
VirtualCodeAction {
76+
title: "Disable current line diagnostic (unknown-doc-tag)".to_string()
77+
},
78+
VirtualCodeAction {
79+
title: "Disable all diagnostics in current file (unknown-doc-tag)".to_string()
80+
},
81+
VirtualCodeAction {
82+
title:
83+
"Disable all diagnostics in current project (unknown-doc-tag)".to_string()
84+
},
85+
]
86+
));
5087

51-
let CodeActionOrCommand::CodeAction(action) = &actions[0] else {
52-
panic!()
53-
};
54-
assert_eq!(action.title, "Add @foo to the list of known tags")
88+
Ok(())
5589
}
5690
}
Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#[cfg(test)]
22
mod tests {
3+
use crate::handlers::test_lib::{
4+
ProviderVirtualWorkspace, VirtualCompletionResolveItem, check,
5+
};
6+
use googletest::prelude::*;
37

4-
use crate::handlers::test_lib::{ProviderVirtualWorkspace, VirtualCompletionResolveItem};
5-
6-
#[test]
7-
fn test_1() {
8+
#[gtest]
9+
fn test_1() -> Result<()> {
810
let mut ws = ProviderVirtualWorkspace::new();
911

10-
assert!(ws.check_completion_resolve(
12+
check!(ws.check_completion_resolve(
1113
r#"
1214
---@overload fun(event: "AAA", callback: fun(trg: string, data: number)): number
1315
---@overload fun(event: "BBB", callback: fun(trg: string, data: string)): string
@@ -20,68 +22,72 @@ mod tests {
2022
end
2123
2224
test<??>
23-
"#,
25+
"#,
2426
VirtualCompletionResolveItem {
2527
detail:
2628
"local function test(event: string, callback: fun(trg: string, data: number)) -> number (+2 overloads)"
2729
.to_string(),
2830
},
2931
));
32+
Ok(())
3033
}
31-
#[test]
32-
fn test_2() {
34+
#[gtest]
35+
fn test_2() -> Result<()> {
3336
let mut ws = ProviderVirtualWorkspace::new();
3437

35-
assert!(ws.check_completion_resolve(
38+
check!(ws.check_completion_resolve(
3639
r#"
37-
---@class Hover.Test2
38-
---@field event fun(event: "游戏-初始化")
39-
---@field event fun(event: "游戏-恢复", key: string)
40-
local Test2 = {}
40+
---@class Hover.Test2
41+
---@field event fun(event: "游戏-初始化")
42+
---@field event fun(event: "游戏-恢复", key: string)
43+
local Test2 = {}
4144
42-
Test2.<??>
43-
"#,
45+
Test2.<??>
46+
"#,
4447
VirtualCompletionResolveItem {
4548
detail: "(field) Test2.event(event: \"游戏-初始化\") (+1 overloads)".to_string(),
4649
},
4750
));
51+
Ok(())
4852
}
4953

50-
#[test]
51-
fn test_table_field_function_1() {
54+
#[gtest]
55+
fn test_table_field_function_1() -> Result<()> {
5256
let mut ws = ProviderVirtualWorkspace::new();
53-
assert!(ws.check_completion_resolve(
57+
check!(ws.check_completion_resolve(
5458
r#"
55-
---@class T
56-
---@field func fun(self:string) 注释注释
59+
---@class T
60+
---@field func fun(self:string) 注释注释
5761
58-
---@type T
59-
local t = {
60-
<??>
61-
}
62+
---@type T
63+
local t = {
64+
<??>
65+
}
6266
"#,
6367
VirtualCompletionResolveItem {
6468
detail: "(field) T.func(self: string)".to_string(),
6569
},
6670
));
71+
Ok(())
6772
}
6873

69-
#[test]
70-
fn test_table_field_function_2() {
74+
#[gtest]
75+
fn test_table_field_function_2() -> Result<()> {
7176
let mut ws = ProviderVirtualWorkspace::new();
72-
assert!(ws.check_completion_resolve(
77+
check!(ws.check_completion_resolve(
7378
r#"
74-
---@class T
75-
---@field func fun(self: T) 注释注释
79+
---@class T
80+
---@field func fun(self: T) 注释注释
7681
77-
---@type T
78-
local t = {
79-
<??>
80-
}
82+
---@type T
83+
local t = {
84+
<??>
85+
}
8186
"#,
8287
VirtualCompletionResolveItem {
8388
detail: "(method) T:func()".to_string(),
8489
},
8590
));
91+
Ok(())
8692
}
8793
}

0 commit comments

Comments
 (0)