Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
not-my-profile committed Aug 30, 2023
1 parent e9e1c43 commit 68dc621
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,101 @@ mod tests {
"###)
}

#[test]
fn span_numbers_are_char_offsets() {
let source = "🍎 == apple";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_label(Label::new(2..4).with_message("comparison operator"))
.finish()
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error:
,-[<unknown>:1:1]
|
1 | 🍎 == apple
| ^|
| `-- comparison operator
---'
"###);
}

#[test]
fn empty_span() {
let source = "apple";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_label(Label::new(0..0).with_message("first character"))
.finish()
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error:
,-[<unknown>:1:1]
|
1 | apple
| |
| `- first character
---'
"###);
}

#[test]
fn one_char_span() {
let source = "apple";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_label(Label::new(0..1).with_message("first character"))
.finish()
.write_to_string(Source::from(source));
// TODO: it would be nice if this rendered just like the empty_label test
assert_snapshot!(msg, @r###"
Error:
,-[<unknown>:1:1]
|
1 | apple
| |
| `-- first character
---'
"###);
}

#[test]
fn empty_span_directly_after_end() {
let source = "universe";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_label(Label::new(source.len()..source.len()).with_message("outside"))
.finish()
.write_to_string(Source::from(source));
assert_snapshot!(msg, @r###"
Error:
,-[<unknown>:1:1]
|
1 | universe
| |
| `- outside
---'
"###);
}

#[test]
fn span_out_of_bounds() {
let source = "universe";
let msg = Report::<Range<usize>>::build(ReportKind::Error, (), 0)
.with_config(no_color_and_ascii())
.with_label(Label::new(source.len() + 1..source.len() + 1).with_message("outside"))
.finish()
.write_to_string(Source::from(source));
// TODO: Report::write should probably panic on out-of-bound spans
assert_snapshot!(msg, @r###"
Error:
,-[<unknown>:1:1]
|
|
---'
"###);
}

#[test]
fn two_labels_without_messages() {
let source = "apple == orange;";
Expand Down

0 comments on commit 68dc621

Please sign in to comment.