Skip to content

Commit 7427c29

Browse files
author
Zibi Braniecki
committed
Add a coupe more tests
1 parent b59784d commit 7427c29

File tree

3 files changed

+135
-27
lines changed

3 files changed

+135
-27
lines changed

src/display_list/from_snippet.rs

+21-26
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ fn format_label(label: Option<&str>, style: Option<DisplayTextStyle>) -> Vec<Dis
55
let mut result = vec![];
66
if let Some(label) = label {
77
let elements: Vec<&str> = label.split("__").collect();
8-
let mut idx = 0;
9-
for element in elements {
8+
for (idx, element) in elements.iter().enumerate() {
109
let element_style = match style {
1110
Some(s) => s,
1211
None => if idx % 2 == 0 {
@@ -19,10 +18,9 @@ fn format_label(label: Option<&str>, style: Option<DisplayTextStyle>) -> Vec<Dis
1918
content: element.to_string(),
2019
style: element_style,
2120
});
22-
idx += 1;
2321
}
2422
}
25-
return result;
23+
result
2624
}
2725

2826
fn format_title(annotation: &snippet::Annotation) -> DisplayLine {
@@ -84,11 +82,11 @@ fn format_header(
8482
let mut col = 1;
8583
let mut row = slice.line_start;
8684

87-
for idx in 0..body.len() {
85+
for item in body.iter() {
8886
if let DisplayLine::Source {
8987
line: DisplaySourceLine::Content { range, .. },
9088
..
91-
} = body[idx]
89+
} = item
9290
{
9391
if annotation.range.0 >= range.0 && annotation.range.0 <= range.1 {
9492
col = annotation.range.0 - range.0;
@@ -104,14 +102,13 @@ fn format_header(
104102
header_type: display_header,
105103
}));
106104
}
107-
} else {
108-
if let Some(ref path) = slice.origin {
109-
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
110-
path: path.to_string(),
111-
pos: None,
112-
header_type: display_header,
113-
}));
114-
}
105+
}
106+
if let Some(ref path) = slice.origin {
107+
return Some(DisplayLine::Raw(DisplayRawLine::Origin {
108+
path: path.to_string(),
109+
pos: None,
110+
header_type: display_header,
111+
}));
115112
}
116113
None
117114
}
@@ -142,19 +139,19 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
142139
} else {
143140
1
144141
};
145-
for i in fold_start..fold_start + pre_len {
146-
new_body.push(body[i].clone());
147-
}
142+
for item in body.iter().take(fold_start + pre_len).skip(fold_start) {
143+
new_body.push(item.clone());
144+
};
148145
new_body.push(DisplayLine::Fold {
149146
inline_marks: inline_marks.clone(),
150147
});
151-
for i in fold_end - post_len..fold_end {
152-
new_body.push(body[i].clone());
148+
for item in body.iter().take(fold_end).skip(fold_end - post_len) {
149+
new_body.push(item.clone());
153150
}
154151
} else {
155152
let start = idx - no_annotation_lines_counter;
156-
for i in start..idx {
157-
new_body.push(body[i].clone());
153+
for item in body.iter().take(idx).skip(start) {
154+
new_body.push(item.clone());
158155
}
159156
}
160157
no_annotation_lines_counter = 0;
@@ -172,7 +169,7 @@ fn fold_body(body: &[DisplayLine]) -> Vec<DisplayLine> {
172169
idx += 1;
173170
}
174171

175-
return new_body;
172+
new_body
176173
}
177174

178175
fn format_body(slice: &snippet::Slice, has_footer: bool) -> Vec<DisplayLine> {
@@ -372,14 +369,12 @@ impl From<snippet::Snippet> for DisplayList {
372369
body.push(format_title(&annotation));
373370
}
374371

375-
let mut slice_idx = 0;
376-
for slice in snippet.slices {
372+
for (idx, slice) in snippet.slices.iter().enumerate() {
377373
body.append(&mut format_slice(
378374
&slice,
379-
slice_idx == 0,
375+
idx == 0,
380376
!snippet.footer.is_empty(),
381377
));
382-
slice_idx += 1;
383378
}
384379

385380
for annotation in snippet.footer {

src/formatter/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl DisplayListFormatter {
203203
if annotation.is_empty() {
204204
return Some(format!("{}{}", indent, marks));
205205
}
206-
return Some(format!("{}{} {}", indent, marks, color.paint(&annotation)));
206+
Some(format!("{}{} {}", indent, marks, color.paint(&annotation)))
207207
}
208208
}
209209
}

tests/dl_from_snippet.rs

+113
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,81 @@ fn test_format_slice() {
8383
assert_eq!(dl::DisplayList::from(input), output);
8484
}
8585

86+
#[test]
87+
fn test_format_slices_continuation() {
88+
let input = snippet::Snippet {
89+
title: None,
90+
footer: vec![],
91+
slices: vec![
92+
snippet::Slice {
93+
source: "This is slice 1".to_string(),
94+
line_start: 5402,
95+
origin: Some("file1.rs".to_string()),
96+
annotations: vec![],
97+
fold: false,
98+
},
99+
snippet::Slice {
100+
source: "This is slice 2".to_string(),
101+
line_start: 2,
102+
origin: Some("file2.rs".to_string()),
103+
annotations: vec![],
104+
fold: false,
105+
},
106+
],
107+
};
108+
let output = dl::DisplayList {
109+
body: vec![
110+
dl::DisplayLine::Raw(dl::DisplayRawLine::Origin {
111+
path: "file1.rs".to_string(),
112+
pos: None,
113+
header_type: dl::DisplayHeaderType::Initial,
114+
}),
115+
dl::DisplayLine::Source {
116+
lineno: None,
117+
inline_marks: vec![],
118+
line: dl::DisplaySourceLine::Empty,
119+
},
120+
dl::DisplayLine::Source {
121+
lineno: Some(5402),
122+
inline_marks: vec![],
123+
line: dl::DisplaySourceLine::Content {
124+
text: "This is slice 1".to_string(),
125+
range: (0, 16),
126+
},
127+
},
128+
dl::DisplayLine::Source {
129+
lineno: None,
130+
inline_marks: vec![],
131+
line: dl::DisplaySourceLine::Empty,
132+
},
133+
dl::DisplayLine::Raw(dl::DisplayRawLine::Origin {
134+
path: "file2.rs".to_string(),
135+
pos: None,
136+
header_type: dl::DisplayHeaderType::Continuation,
137+
}),
138+
dl::DisplayLine::Source {
139+
lineno: None,
140+
inline_marks: vec![],
141+
line: dl::DisplaySourceLine::Empty,
142+
},
143+
dl::DisplayLine::Source {
144+
lineno: Some(2),
145+
inline_marks: vec![],
146+
line: dl::DisplaySourceLine::Content {
147+
text: "This is slice 2".to_string(),
148+
range: (0, 16),
149+
},
150+
},
151+
dl::DisplayLine::Source {
152+
lineno: None,
153+
inline_marks: vec![],
154+
line: dl::DisplaySourceLine::Empty,
155+
},
156+
],
157+
};
158+
assert_eq!(dl::DisplayList::from(input), output);
159+
}
160+
86161
#[test]
87162
fn test_format_slice_annotation_standalone() {
88163
let input = snippet::Snippet {
@@ -155,3 +230,41 @@ fn test_format_slice_annotation_standalone() {
155230
};
156231
assert_eq!(dl::DisplayList::from(input), output);
157232
}
233+
234+
#[test]
235+
fn test_format_label() {
236+
let input = snippet::Snippet {
237+
title: None,
238+
footer: vec![snippet::Annotation {
239+
id: None,
240+
label: Some("This __is__ a title".to_string()),
241+
annotation_type: snippet::AnnotationType::Error,
242+
}],
243+
slices: vec![],
244+
};
245+
let output = dl::DisplayList {
246+
body: vec![dl::DisplayLine::Raw(dl::DisplayRawLine::Annotation {
247+
annotation: dl::Annotation {
248+
annotation_type: dl::DisplayAnnotationType::Error,
249+
id: None,
250+
label: vec![
251+
dl::DisplayTextFragment {
252+
content: "This ".to_string(),
253+
style: dl::DisplayTextStyle::Regular,
254+
},
255+
dl::DisplayTextFragment {
256+
content: "is".to_string(),
257+
style: dl::DisplayTextStyle::Emphasis,
258+
},
259+
dl::DisplayTextFragment {
260+
content: " a title".to_string(),
261+
style: dl::DisplayTextStyle::Regular,
262+
},
263+
],
264+
},
265+
source_aligned: true,
266+
continuation: false,
267+
})],
268+
};
269+
assert_eq!(dl::DisplayList::from(input), output);
270+
}

0 commit comments

Comments
 (0)