1#![forbid(unsafe_code)]
11
12mod canonical_assertions;
13mod snapshot;
14
15use diagprint::{
16 Applicability, Diagnostic, DiagnosticReport, DiagnosticValue, LabelKind, ReportStatus,
17 Severity, SeverityCounts,
18};
19
20pub use canonical_assertions::{
21 DiagnosticCanonicalAssertions, DiagnosticDeltaAssertions, DiagnosticReportCanonicalAssertions,
22};
23
24pub use snapshot::{
25 SNAPSHOT_IGNORED_FIELDS, assert_diagnostic_matches_snapshot, assert_report_matches_snapshot,
26 diagnostic_snapshot, report_snapshot,
27};
28
29pub trait DiagnosticAssertions {
31 fn assert_severity(&self, expected: Severity) -> &Self;
33
34 fn assert_code(&self, expected: &str) -> &Self;
36
37 fn assert_no_code(&self) -> &Self;
39
40 fn assert_message_contains(&self, expected: &str) -> &Self;
42
43 fn assert_help_contains(&self, expected: &str) -> &Self;
45
46 fn assert_note_contains(&self, expected: &str) -> &Self;
48
49 fn assert_attribute<V>(&self, name: &str, expected: V) -> &Self
51 where
52 V: Into<DiagnosticValue>;
53
54 fn assert_label(&self, kind: LabelKind, file: &str, line: u32) -> &Self;
56
57 fn assert_primary_label(&self, file: &str, line: u32) -> &Self {
59 self.assert_label(LabelKind::Primary, file, line)
60 }
61
62 fn assert_secondary_label(&self, file: &str, line: u32) -> &Self {
64 self.assert_label(LabelKind::Secondary, file, line)
65 }
66
67 fn assert_suggestion(&self, title: &str) -> &Self;
69
70 fn assert_suggestion_applicability(&self, title: &str, expected: Applicability) -> &Self;
72}
73
74impl DiagnosticAssertions for Diagnostic {
75 fn assert_severity(&self, expected: Severity) -> &Self {
76 assert_eq!(
77 self.severity, expected,
78 "diagnostic severity mismatch: expected {expected}, got {}",
79 self.severity,
80 );
81
82 self
83 }
84
85 fn assert_code(&self, expected: &str) -> &Self {
86 assert_eq!(
87 self.code.as_deref(),
88 Some(expected),
89 "diagnostic code mismatch: expected {expected:?}, got {:?}",
90 self.code,
91 );
92
93 self
94 }
95
96 fn assert_no_code(&self) -> &Self {
97 assert!(
98 self.code.is_none(),
99 "expected diagnostic to have no code, got {:?}",
100 self.code,
101 );
102
103 self
104 }
105
106 fn assert_message_contains(&self, expected: &str) -> &Self {
107 assert!(
108 self.message.contains(expected),
109 "diagnostic message did not contain {expected:?}: {:?}",
110 self.message,
111 );
112
113 self
114 }
115
116 fn assert_help_contains(&self, expected: &str) -> &Self {
117 let help = self.help.as_deref().unwrap_or_else(|| {
118 panic!("expected diagnostic help containing {expected:?}, but no help was present")
119 });
120
121 assert!(
122 help.contains(expected),
123 "diagnostic help did not contain {expected:?}: {help:?}",
124 );
125
126 self
127 }
128
129 fn assert_note_contains(&self, expected: &str) -> &Self {
130 assert!(
131 self.notes.iter().any(|note| note.contains(expected)),
132 "no diagnostic note contained {expected:?}; notes: {:?}",
133 self.notes,
134 );
135
136 self
137 }
138
139 fn assert_attribute<V>(&self, name: &str, expected: V) -> &Self
140 where
141 V: Into<DiagnosticValue>,
142 {
143 let expected = expected.into();
144
145 let actual = self
146 .attributes
147 .iter()
148 .find(|attribute| attribute.name == name)
149 .unwrap_or_else(|| {
150 panic!(
151 "expected diagnostic attribute {name:?}; available attributes: {:?}",
152 self.attributes
153 .iter()
154 .map(|attribute| attribute.name.as_str())
155 .collect::<Vec<_>>(),
156 )
157 });
158
159 assert_eq!(
160 actual.value, expected,
161 "diagnostic attribute {name:?} mismatch",
162 );
163
164 self
165 }
166
167 fn assert_label(&self, kind: LabelKind, file: &str, line: u32) -> &Self {
168 assert!(
169 self.labels.iter().any(|label| {
170 label.kind == kind && label.location.file == file && label.location.line == line
171 }),
172 "expected {kind:?} label at {file}:{line}; labels: {:?}",
173 self.labels,
174 );
175
176 self
177 }
178
179 fn assert_suggestion(&self, title: &str) -> &Self {
180 assert!(
181 self.suggestions
182 .iter()
183 .any(|suggestion| suggestion.title == title),
184 "expected suggestion {title:?}; suggestions: {:?}",
185 self.suggestions
186 .iter()
187 .map(|suggestion| suggestion.title.as_str())
188 .collect::<Vec<_>>(),
189 );
190
191 self
192 }
193
194 fn assert_suggestion_applicability(&self, title: &str, expected: Applicability) -> &Self {
195 let suggestion = self
196 .suggestions
197 .iter()
198 .find(|suggestion| suggestion.title == title)
199 .unwrap_or_else(|| {
200 panic!(
201 "expected suggestion {title:?}; suggestions: {:?}",
202 self.suggestions
203 .iter()
204 .map(|suggestion| suggestion.title.as_str())
205 .collect::<Vec<_>>(),
206 )
207 });
208
209 assert_eq!(
210 suggestion.applicability, expected,
211 "suggestion {title:?} applicability mismatch",
212 );
213
214 self
215 }
216}
217
218pub trait DiagnosticReportAssertions {
220 fn assert_len(&self, expected: usize) -> &Self;
222
223 fn assert_empty(&self) -> &Self;
225
226 fn assert_not_empty(&self) -> &Self;
228
229 fn assert_status(&self, expected: ReportStatus) -> &Self;
231
232 fn assert_counts(&self, expected: SeverityCounts) -> &Self;
234
235 fn assert_contains_severity(&self, severity: Severity) -> &Self;
237
238 fn assert_contains_code(&self, expected: &str) -> &Self;
240
241 fn assert_count_at_or_above(&self, minimum: Severity, expected: usize) -> &Self;
243
244 fn assert_none_at_or_above(&self, minimum: Severity) -> &Self;
246}
247
248impl DiagnosticReportAssertions for DiagnosticReport {
249 fn assert_len(&self, expected: usize) -> &Self {
250 assert_eq!(self.len(), expected, "diagnostic report length mismatch",);
251
252 self
253 }
254
255 fn assert_empty(&self) -> &Self {
256 assert!(
257 self.is_empty(),
258 "expected an empty diagnostic report, got {} diagnostics",
259 self.len(),
260 );
261
262 self
263 }
264
265 fn assert_not_empty(&self) -> &Self {
266 assert!(!self.is_empty(), "expected a non-empty diagnostic report",);
267
268 self
269 }
270
271 fn assert_status(&self, expected: ReportStatus) -> &Self {
272 let actual = self.status();
273
274 assert_eq!(actual, expected, "diagnostic report status mismatch",);
275
276 self
277 }
278
279 fn assert_counts(&self, expected: SeverityCounts) -> &Self {
280 let actual = self.counts();
281
282 assert_eq!(
283 actual, expected,
284 "diagnostic report severity counts mismatch",
285 );
286
287 self
288 }
289
290 fn assert_contains_severity(&self, severity: Severity) -> &Self {
291 assert!(
292 self.iter()
293 .any(|diagnostic| diagnostic.severity == severity),
294 "diagnostic report did not contain severity {severity}",
295 );
296
297 self
298 }
299
300 fn assert_contains_code(&self, expected: &str) -> &Self {
301 assert!(
302 self.iter()
303 .any(|diagnostic| diagnostic.code.as_deref() == Some(expected)),
304 "diagnostic report did not contain code {expected:?}",
305 );
306
307 self
308 }
309
310 fn assert_count_at_or_above(&self, minimum: Severity, expected: usize) -> &Self {
311 let actual = self.count_at_or_above(minimum);
312
313 assert_eq!(
314 actual, expected,
315 "unexpected number of diagnostics at or above {minimum}",
316 );
317
318 self
319 }
320
321 fn assert_none_at_or_above(&self, minimum: Severity) -> &Self {
322 let count = self.count_at_or_above(minimum);
323
324 assert_eq!(count, 0, "expected no diagnostics at or above {minimum}",);
325
326 self
327 }
328}
329
330pub mod prelude {
332 pub use crate::{
333 DiagnosticAssertions, DiagnosticCanonicalAssertions, DiagnosticDeltaAssertions,
334 DiagnosticReportAssertions, DiagnosticReportCanonicalAssertions,
335 assert_diagnostic_file_snapshot, assert_diagnostic_snapshot, assert_report_file_snapshot,
336 assert_report_snapshot,
337 };
338}