Skip to content

Commit 32c4548

Browse files
committed
tries to fix tests
1 parent 2795f6c commit 32c4548

File tree

1 file changed

+63
-42
lines changed

1 file changed

+63
-42
lines changed

pyrefly/lib/test/lsp/lsp_interaction/diagnostic.rs

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use lsp_types::Url;
1313

1414
use crate::test::lsp::lsp_interaction::object_model::InitializeSettings;
1515
use crate::test::lsp::lsp_interaction::object_model::LspInteraction;
16+
use crate::test::lsp::lsp_interaction::object_model::ValidationResult;
1617
use crate::test::lsp::lsp_interaction::util::get_test_files_root;
1718

1819
#[test]
@@ -236,65 +237,85 @@ fn test_version_support_publish_diagnostics() {
236237
..Default::default()
237238
});
238239

240+
let gen_validator = |expected_version: i64| {
241+
let actual_uri = uri.as_str();
242+
move |msg: &Message| {
243+
let Message::Notification(Notification { method, params }) = msg else {
244+
return ValidationResult::Skip;
245+
};
246+
let Some(uri_val) = params.get("uri") else {
247+
return ValidationResult::Skip;
248+
};
249+
let Some(expected_uri) = uri_val.as_str() else {
250+
return ValidationResult::Skip;
251+
};
252+
if expected_uri == actual_uri && method == "textDocument/publishDiagnostics" {
253+
if let Some(actual_version) = params.get("version") {
254+
if let Some(actual_version) = actual_version.as_i64() {
255+
assert!(
256+
actual_version <= expected_version,
257+
"expected version: {}, actual version: {}",
258+
expected_version,
259+
actual_version
260+
);
261+
return match actual_version.cmp(&expected_version) {
262+
std::cmp::Ordering::Less => ValidationResult::Skip,
263+
std::cmp::Ordering::Equal => ValidationResult::Pass,
264+
std::cmp::Ordering::Greater => ValidationResult::Fail,
265+
};
266+
}
267+
}
268+
}
269+
ValidationResult::Skip
270+
}
271+
};
272+
239273
interaction.server.did_open("text_document.py");
240-
interaction.server.diagnostic("text_document.py");
241274

242-
interaction
243-
.client
244-
.expect_message(lsp_server::Message::Notification(
245-
lsp_server::Notification {
246-
method: "textDocument/publishDiagnostics".to_owned(),
247-
params: serde_json::json! {{
248-
"uri": uri,
249-
"diagnostics": [],
250-
"version": 1
251-
}},
252-
},
253-
));
275+
let version = 1;
276+
interaction.client.expect_message_helper(
277+
gen_validator(version),
278+
&format!(
279+
"publishDiagnostics notification with version {} for file: {}",
280+
version,
281+
uri.as_str()
282+
),
283+
);
254284

255-
interaction.server.did_change("text_document.py", "# test");
256-
interaction.server.diagnostic("text_document.py");
285+
interaction.server.did_change("text_document.py", "a = b");
257286

258-
// I don't understand why this version is still 1
259-
interaction
260-
.client
261-
.expect_message(lsp_server::Message::Notification(
262-
lsp_server::Notification {
263-
method: "textDocument/publishDiagnostics".to_owned(),
264-
params: serde_json::json! {{
265-
"uri": uri,
266-
"diagnostics": [],
267-
"version": 2
268-
}},
269-
},
270-
));
287+
let version = 2;
288+
interaction.client.expect_message_helper(
289+
gen_validator(version),
290+
&format!(
291+
"publishDiagnostics notification with version {} for file: {}",
292+
version,
293+
uri.as_str()
294+
),
295+
);
271296

272297
interaction
273298
.server
274299
.send_message(Message::Notification(Notification {
275300
method: "textDocument/didClose".to_owned(),
276301
params: serde_json::json!({
277302
"textDocument": {
278-
"uri": uri.to_string(),
303+
"uri": uri.as_str(),
279304
"languageId": "python",
280305
"version": 3
281306
},
282307
}),
283308
}));
284-
interaction.server.diagnostic("text_document.py");
285309

286-
interaction
287-
.client
288-
.expect_message(lsp_server::Message::Notification(
289-
lsp_server::Notification {
290-
method: "textDocument/publishDiagnostics".to_owned(),
291-
params: serde_json::json! {{
292-
"uri": uri,
293-
"diagnostics": [],
294-
"version": 3
295-
}},
296-
},
297-
));
310+
let version = 3;
311+
interaction.client.expect_message_helper(
312+
gen_validator(version),
313+
&format!(
314+
"publishDiagnostics notification with version {} for file: {}",
315+
version,
316+
uri.as_str()
317+
),
318+
);
298319

299320
interaction.shutdown();
300321
}

0 commit comments

Comments
 (0)