11#[ cfg( test) ]
22mod tests {
3+ use lsp_types:: WorkspaceEdit ;
4+
35 use crate :: handlers:: test_lib:: ProviderVirtualWorkspace ;
46
7+ fn check_len ( result : & Option < WorkspaceEdit > , len : usize ) -> bool {
8+ let Some ( result) = result else {
9+ return false ;
10+ } ;
11+ if let Some ( changes) = & result. changes {
12+ let mut count = 0 ;
13+ for ( _, edits) in changes {
14+ count += edits. len ( ) ;
15+ }
16+ if count != len {
17+ return false ;
18+ }
19+ }
20+ true
21+ }
22+
23+ /// 检查指定名称的文件的`new_text`是否为预期值
24+ fn check_new_text ( result : & Option < WorkspaceEdit > , file_name : & str , new_text : & str ) -> bool {
25+ let Some ( result) = result else {
26+ return false ;
27+ } ;
28+ if let Some ( changes) = & result. changes {
29+ for ( uri, edits) in changes {
30+ let path = uri. path ( ) . as_str ( ) ;
31+ if path. ends_with ( format ! ( "/{}" , file_name) . as_str ( ) ) {
32+ if let Some ( edit) = edits. first ( ) {
33+ if edit. new_text == new_text {
34+ return true ;
35+ }
36+ }
37+ }
38+ }
39+ }
40+ false
41+ }
42+
543 #[ test]
644 fn test_int_key ( ) {
745 let mut ws = ProviderVirtualWorkspace :: new ( ) ;
8- assert ! ( ws. check_rename(
9- r#"
46+ assert ! ( check_len(
47+ & ws. check_rename(
48+ r#"
1049 local export = {
1150 [<??>1] = 1,
1251 }
1352
1453 export[1] = 2
1554 "# ,
16- "2" . to_string( ) ,
17- 2 ,
55+ "2" . to_string( )
56+ ) ,
57+ 2
1858 ) ) ;
1959
20- assert ! ( ws. check_rename(
21- r#"
60+ assert ! ( check_len(
61+ & ws. check_rename(
62+ r#"
2263 local export = {
2364 [1] = 1,
2465 }
2566
2667 export[<??>1] = 2
2768 "# ,
28- "2" . to_string( ) ,
29- 2 ,
69+ "2" . to_string( )
70+ ) ,
71+ 2
3072 ) ) ;
3173 }
3274
@@ -42,9 +84,8 @@ mod tests {
4284 Test[1] = 2
4385 "# ,
4486 "2" . to_string ( ) ,
45- 2 ,
4687 ) ;
47- assert ! ( result) ;
88+ assert ! ( check_len ( & result, 2 ) ) ;
4889 }
4990
5091 #[ test]
@@ -64,9 +105,8 @@ mod tests {
64105 end
65106 "# ,
66107 "_next" . to_string ( ) ,
67- 2 ,
68108 ) ;
69- assert ! ( result) ;
109+ assert ! ( check_len ( & result, 2 ) ) ;
70110 }
71111
72112 #[ test]
@@ -79,9 +119,8 @@ mod tests {
79119 ---@type Para<??>ms<number>
80120 "# ,
81121 "Params1" . to_string ( ) ,
82- 2 ,
83122 ) ;
84- assert ! ( result) ;
123+ assert ! ( check_len ( & result, 2 ) ) ;
85124 }
86125
87126 #[ test]
@@ -99,9 +138,8 @@ mod tests {
99138 ABC.test()
100139 "# ,
101140 "test1" . to_string ( ) ,
102- 2 ,
103141 ) ;
104- assert ! ( result) ;
142+ assert ! ( check_len ( & result, 2 ) ) ;
105143 }
106144
107145 #[ test]
@@ -116,9 +154,8 @@ mod tests {
116154 end
117155 "# ,
118156 "aaa1" . to_string ( ) ,
119- 3 ,
120157 ) ;
121- assert ! ( result) ;
158+ assert ! ( check_len ( & result, 3 ) ) ;
122159 }
123160 {
124161 let result = ws. check_rename (
@@ -129,9 +166,8 @@ mod tests {
129166 end
130167 "# ,
131168 "aaa1" . to_string ( ) ,
132- 3 ,
133169 ) ;
134- assert ! ( result) ;
170+ assert ! ( check_len ( & result, 3 ) ) ;
135171 }
136172 }
137173
@@ -147,14 +183,33 @@ mod tests {
147183
148184 "# ,
149185 ) ;
150- ws. check_rename (
186+ let result = ws. check_rename (
151187 r#"
152188 ---@namespace Luakit
153189 ---@class Test.Abc<??>
154190 local Test = {}
155191 "# ,
156192 "Abc" . to_string ( ) ,
157- 2 ,
158193 ) ;
194+ assert ! ( check_len( & result, 2 ) ) ;
195+ assert ! ( check_new_text( & result, "a.lua" , "Luakit.Abc" ) ) ;
196+ }
197+
198+ #[ test]
199+ fn test_namespace_class_1 ( ) {
200+ let mut ws = ProviderVirtualWorkspace :: new ( ) ;
201+ let result = ws. check_rename (
202+ r#"
203+ ---@namespace Luakit
204+ ---@class Abc
205+ local Test = {}
206+
207+ ---@type Abc<??>
208+ local a = Test
209+ "# ,
210+ "AAA" . to_string ( ) ,
211+ ) ;
212+ assert ! ( check_len( & result, 2 ) ) ;
213+ assert ! ( check_new_text( & result, "virtual_0.lua" , "AAA" ) ) ;
159214 }
160215}
0 commit comments