Skip to content

Commit

Permalink
Treat empty input as one line still
Browse files Browse the repository at this point in the history
Otherwise no labels can be printed over an empty input,
not even empty ones!
(`get_source_groups` never returns anything if there are
no source lines.)
  • Loading branch information
ISSOtm authored and zesterer committed Jun 27, 2024
1 parent 603afb2 commit 7c988a6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ impl<I: AsRef<str>> From<I> for Source<I> {
///
/// Note that this function can be expensive for long strings. Use an implementor of [`Cache`] where possible.
fn from(input: I) -> Self {
// `input.split_inclusive()` will not iterate at all,
// but an empty input still ought to count as a single empty line.
if input.as_ref().is_empty() {
return Self {
text: input,
lines: vec![Line {
offset: 0,
char_len: 0,
byte_offset: 0,
byte_len: 0,
}],
len: 0,
byte_len: 0,
};
}

let mut char_offset = 0;
let mut byte_offset = 0;
let mut lines = Vec::new();
Expand Down Expand Up @@ -383,7 +399,7 @@ mod tests {

#[test]
fn source_from_empty() {
test_with_lines(vec![]); // Empty string
test_with_lines(vec![""]); // Empty string
}

#[test]
Expand Down
96 changes: 96 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,102 @@ mod tests {
"###);
}

#[test]
fn empty_input() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
.finish()
.write_to_string(Source::from(source));

assert_snapshot!(msg, @r###"
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
| `- No more fruit!
---'
"###);
}

#[test]
fn empty_input_help() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
.with_help("have you tried going to the farmer's market?")
.finish()
.write_to_string(Source::from(source));

assert_snapshot!(msg, @r###"
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
| `- No more fruit!
|
| Help: have you tried going to the farmer's market?
---'
"###);
}

#[test]
fn empty_input_note() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
.with_note("eat your greens!")
.finish()
.write_to_string(Source::from(source));

assert_snapshot!(msg, @r###"
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
| `- No more fruit!
|
| Note: eat your greens!
---'
"###);
}

#[test]
fn empty_input_help_note() {
let source = "";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_message("unexpected end of file")
.with_label(Label::new(0..0).with_message("No more fruit!"))
.with_note("eat your greens!")
.with_help("have you tried going to the farmer's market?")
.finish()
.write_to_string(Source::from(source));

assert_snapshot!(msg, @r###"
Error: unexpected end of file
,-[<unknown>:1:1]
|
1 |
| |
| `- No more fruit!
|
| Help: have you tried going to the farmer's market?
|
| Note: eat your greens!
---'
"###);
}

#[test]
fn byte_spans_never_crash() {
let source = "apple\np\n\nempty\n";
Expand Down

0 comments on commit 7c988a6

Please sign in to comment.