@@ -64,96 +64,57 @@ mod tests {
6464 use crate :: tests:: { initialize_language_server, open_unchecked, update} ;
6565 use assert_json_diff:: assert_json_eq;
6666 use nu_test_support:: fs:: fixtures;
67-
68- #[ test]
69- fn publish_diagnostics_variable_does_not_exists ( ) {
67+ use rstest:: rstest;
68+
69+ #[ rstest]
70+ #[ case:: file_with_no_issues( "pwd.nu" , None , None , serde_json:: json!( [ ] ) ) ]
71+ #[ case:: file_fixed_by_update( "var.nu" , Some ( "$env" ) , Some ( lsp_types:: Range {
72+ start: lsp_types:: Position { line: 0 , character: 6 } ,
73+ end: lsp_types:: Position { line: 0 , character: 30 } ,
74+ } ) , serde_json:: json!( [ ] ) ) ]
75+ #[ case:: variable_does_not_exist( "var.nu" , None , None , serde_json:: json!( [ {
76+ "range" : {
77+ "start" : { "line" : 0 , "character" : 6 } ,
78+ "end" : { "line" : 0 , "character" : 30 }
79+ } ,
80+ "message" : "Variable not found." ,
81+ "severity" : 1
82+ } ] ) ) ]
83+ fn publish_diagnostics (
84+ #[ case] filename : & str ,
85+ #[ case] update_text : Option < & str > ,
86+ #[ case] update_range : Option < lsp_types:: Range > ,
87+ #[ case] expected_diagnostics : serde_json:: Value ,
88+ ) {
7089 let ( client_connection, _recv) = initialize_language_server ( None , None ) ;
7190
7291 let mut script = fixtures ( ) ;
7392 script. push ( "lsp" ) ;
7493 script. push ( "diagnostics" ) ;
75- script. push ( "var.nu" ) ;
94+ script. push ( filename ) ;
7695 let script = path_to_uri ( & script) ;
7796
78- let notification = open_unchecked ( & client_connection, script. clone ( ) ) ;
79-
80- assert_json_eq ! (
81- notification,
82- serde_json:: json!( {
83- "method" : "textDocument/publishDiagnostics" ,
84- "params" : {
85- "uri" : script,
86- "diagnostics" : [ {
87- "range" : {
88- "start" : { "line" : 0 , "character" : 6 } ,
89- "end" : { "line" : 0 , "character" : 30 }
90- } ,
91- "message" : "Variable not found." ,
92- "severity" : 1
93- } ]
94- }
95- } )
96- ) ;
97- }
98-
99- #[ test]
100- fn publish_diagnostics_fixed_unknown_variable ( ) {
101- let ( client_connection, _recv) = initialize_language_server ( None , None ) ;
102-
103- let mut script = fixtures ( ) ;
104- script. push ( "lsp" ) ;
105- script. push ( "diagnostics" ) ;
106- script. push ( "var.nu" ) ;
107- let script = path_to_uri ( & script) ;
108-
109- open_unchecked ( & client_connection, script. clone ( ) ) ;
110- let notification = update (
111- & client_connection,
112- script. clone ( ) ,
113- String :: from ( "$env" ) ,
114- Some ( lsp_types:: Range {
115- start : lsp_types:: Position {
116- line : 0 ,
117- character : 6 ,
118- } ,
119- end : lsp_types:: Position {
120- line : 0 ,
121- character : 30 ,
122- } ,
123- } ) ,
124- ) ;
125-
126- assert_json_eq ! (
127- notification,
128- serde_json:: json!( {
129- "method" : "textDocument/publishDiagnostics" ,
130- "params" : {
131- "uri" : script,
132- "diagnostics" : [ ]
133- }
134- } )
135- ) ;
136- }
137-
138- #[ test]
139- fn publish_diagnostics_none ( ) {
140- let ( client_connection, _recv) = initialize_language_server ( None , None ) ;
141-
142- let mut script = fixtures ( ) ;
143- script. push ( "lsp" ) ;
144- script. push ( "diagnostics" ) ;
145- script. push ( "pwd.nu" ) ;
146- let script = path_to_uri ( & script) ;
147-
148- let notification = open_unchecked ( & client_connection, script. clone ( ) ) ;
97+ // For files that need fixing, open first then update
98+ let notification = if let ( Some ( text) , Some ( range) ) = ( update_text, update_range) {
99+ open_unchecked ( & client_connection, script. clone ( ) ) ;
100+ update (
101+ & client_connection,
102+ script. clone ( ) ,
103+ String :: from ( text) ,
104+ Some ( range) ,
105+ )
106+ } else {
107+ // For files with no issues, just open
108+ open_unchecked ( & client_connection, script. clone ( ) )
109+ } ;
149110
150111 assert_json_eq ! (
151112 notification,
152113 serde_json:: json!( {
153114 "method" : "textDocument/publishDiagnostics" ,
154115 "params" : {
155116 "uri" : script,
156- "diagnostics" : [ ]
117+ "diagnostics" : expected_diagnostics
157118 }
158119 } )
159120 ) ;
0 commit comments