1use std::str::FromStr;
2
3use lsp::{DiagnosticSeverity, DiagnosticTag};
4use project::lsp_command::*;
5use rpc::proto::{self};
6use serde_json::json;
7
8#[test]
9fn test_serialize_lsp_diagnostic() {
10 let lsp_diagnostic = lsp::Diagnostic {
11 range: lsp::Range {
12 start: lsp::Position::new(0, 1),
13 end: lsp::Position::new(2, 3),
14 },
15 severity: Some(DiagnosticSeverity::ERROR),
16 code: Some(lsp::NumberOrString::String("E001".to_string())),
17 source: Some("test-source".to_string()),
18 message: "Test error message".to_string(),
19 related_information: None,
20 tags: Some(vec![DiagnosticTag::DEPRECATED]),
21 code_description: None,
22 data: Some(json!({"detail": "test detail"})),
23 };
24
25 let proto_diagnostic = GetDocumentDiagnostics::serialize_lsp_diagnostic(lsp_diagnostic)
26 .expect("Failed to serialize diagnostic");
27
28 let start = proto_diagnostic.start.unwrap();
29 let end = proto_diagnostic.end.unwrap();
30 assert_eq!(start.row, 0);
31 assert_eq!(start.column, 1);
32 assert_eq!(end.row, 2);
33 assert_eq!(end.column, 3);
34 assert_eq!(
35 proto_diagnostic.severity,
36 proto::lsp_diagnostic::Severity::Error as i32
37 );
38 assert_eq!(proto_diagnostic.code, Some("E001".to_string()));
39 assert_eq!(proto_diagnostic.source, Some("test-source".to_string()));
40 assert_eq!(proto_diagnostic.message, "Test error message");
41}
42
43#[test]
44fn test_deserialize_lsp_diagnostic() {
45 let proto_diagnostic = proto::LspDiagnostic {
46 start: Some(proto::PointUtf16 { row: 0, column: 1 }),
47 end: Some(proto::PointUtf16 { row: 2, column: 3 }),
48 severity: proto::lsp_diagnostic::Severity::Warning as i32,
49 code: Some("ERR".to_string()),
50 source: Some("Prism".to_string()),
51 message: "assigned but unused variable - a".to_string(),
52 related_information: vec![],
53 tags: vec![],
54 code_description: None,
55 data: None,
56 };
57
58 let lsp_diagnostic = GetDocumentDiagnostics::deserialize_lsp_diagnostic(proto_diagnostic)
59 .expect("Failed to deserialize diagnostic");
60
61 assert_eq!(lsp_diagnostic.range.start.line, 0);
62 assert_eq!(lsp_diagnostic.range.start.character, 1);
63 assert_eq!(lsp_diagnostic.range.end.line, 2);
64 assert_eq!(lsp_diagnostic.range.end.character, 3);
65 assert_eq!(lsp_diagnostic.severity, Some(DiagnosticSeverity::WARNING));
66 assert_eq!(
67 lsp_diagnostic.code,
68 Some(lsp::NumberOrString::String("ERR".to_string()))
69 );
70 assert_eq!(lsp_diagnostic.source, Some("Prism".to_string()));
71 assert_eq!(lsp_diagnostic.message, "assigned but unused variable - a");
72}
73
74#[test]
75fn test_related_information() {
76 let related_info = lsp::DiagnosticRelatedInformation {
77 location: lsp::Location {
78 uri: lsp::Uri::from_str("file:///test.rs").unwrap(),
79 range: lsp::Range {
80 start: lsp::Position::new(1, 1),
81 end: lsp::Position::new(1, 5),
82 },
83 },
84 message: "Related info message".to_string(),
85 };
86
87 let lsp_diagnostic = lsp::Diagnostic {
88 range: lsp::Range {
89 start: lsp::Position::new(0, 0),
90 end: lsp::Position::new(0, 1),
91 },
92 severity: Some(DiagnosticSeverity::INFORMATION),
93 code: None,
94 source: Some("Prism".to_string()),
95 message: "assigned but unused variable - a".to_string(),
96 related_information: Some(vec![related_info]),
97 tags: None,
98 code_description: None,
99 data: None,
100 };
101
102 let proto_diagnostic = GetDocumentDiagnostics::serialize_lsp_diagnostic(lsp_diagnostic)
103 .expect("Failed to serialize diagnostic");
104
105 assert_eq!(proto_diagnostic.related_information.len(), 1);
106 let related = &proto_diagnostic.related_information[0];
107 assert_eq!(related.location_url, Some("file:///test.rs".to_string()));
108 assert_eq!(related.message, "Related info message");
109}
110
111#[test]
112fn test_invalid_ranges() {
113 let proto_diagnostic = proto::LspDiagnostic {
114 start: None,
115 end: Some(proto::PointUtf16 { row: 2, column: 3 }),
116 severity: proto::lsp_diagnostic::Severity::Error as i32,
117 code: None,
118 source: None,
119 message: "Test message".to_string(),
120 related_information: vec![],
121 tags: vec![],
122 code_description: None,
123 data: None,
124 };
125
126 let result = GetDocumentDiagnostics::deserialize_lsp_diagnostic(proto_diagnostic);
127 assert!(result.is_err());
128}