From ed0c64ca7c5d607f33a117ad51e65d24e11ba458 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Thu, 24 Nov 2022 10:12:38 -0500 Subject: [PATCH] Fix reporting tests --- crates/reporting/src/error/canonicalize.rs | 5 +- crates/reporting/src/report.rs | 36 ++++++++---- crates/reporting/tests/test_reporting.rs | 65 +++++++++++++--------- 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/crates/reporting/src/error/canonicalize.rs b/crates/reporting/src/error/canonicalize.rs index b5b26e4125..fd1dd0b854 100644 --- a/crates/reporting/src/error/canonicalize.rs +++ b/crates/reporting/src/error/canonicalize.rs @@ -1238,9 +1238,8 @@ fn to_bad_ident_expr_report<'b>( lines.convert_region(surroundings), lines.convert_region(region), ), - alloc.concat([alloc.reflow( - r"I recommend using camelCase, it is the standard in the Roc ecosystem.", - )]), + alloc.concat([alloc + .reflow(r"I recommend using camelCase. It's the standard style in Roc code!")]), ]) } diff --git a/crates/reporting/src/report.rs b/crates/reporting/src/report.rs index 80ddfc7c33..5d11bea9bf 100644 --- a/crates/reporting/src/report.rs +++ b/crates/reporting/src/report.rs @@ -549,8 +549,8 @@ impl<'a> RocDocAllocator<'a> { let this_line_number_length = line_number.len(); let line = self.src_lines[i as usize]; - - let rest_of_line = if !line.trim().is_empty() { + let is_line_empty = line.trim().is_empty(); + let rest_of_line = if !is_line_empty { self.text(line).indent(indent) } else { self.nil() @@ -572,11 +572,17 @@ impl<'a> RocDocAllocator<'a> { .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)) .append(rest_of_line) } else { - self.text(" ".repeat(max_line_number_length - this_line_number_length)) + let up_to_gutter = self + .text(" ".repeat(max_line_number_length - this_line_number_length)) .append(self.text(line_number).annotate(Annotation::LineNumber)) - .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)) - .append(self.text(" ")) - .append(rest_of_line) + .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)); + + if is_line_empty { + // Don't put an trailing space after the gutter + up_to_gutter + } else { + up_to_gutter.append(self.text(" ")).append(rest_of_line) + } }; result = result.append(source_line); @@ -667,8 +673,8 @@ impl<'a> RocDocAllocator<'a> { let this_line_number_length = line_number.len(); let line: &str = self.src_lines.get(i as usize).unwrap_or(&""); - - let rest_of_line = if !line.trim().is_empty() { + let is_line_empty = line.trim().is_empty(); + let rest_of_line = if !is_line_empty { self.text(line) .annotate(Annotation::CodeBlock) .indent(indent) @@ -691,11 +697,17 @@ impl<'a> RocDocAllocator<'a> { .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)) .append(rest_of_line) } else { - self.text(" ".repeat(max_line_number_length - this_line_number_length)) + let up_to_gutter = self + .text(" ".repeat(max_line_number_length - this_line_number_length)) .append(self.text(line_number).annotate(Annotation::LineNumber)) - .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)) - .append(self.text(" ")) - .append(rest_of_line) + .append(self.text(GUTTER_BAR).annotate(Annotation::GutterBar)); + + if is_line_empty { + // Don't put an trailing space after the gutter + up_to_gutter + } else { + up_to_gutter.append(self.text(" ")).append(rest_of_line) + } }; result = result.append(source_line); diff --git a/crates/reporting/tests/test_reporting.rs b/crates/reporting/tests/test_reporting.rs index f939c2e2e2..809131906f 100644 --- a/crates/reporting/tests/test_reporting.rs +++ b/crates/reporting/tests/test_reporting.rs @@ -4393,16 +4393,20 @@ mod test_reporting { test_report!( comment_with_tab, "# comment with a \t\n4", - @r###" - ── TAB CHARACTER ─────────────────────────────── tmp/comment_with_tab/Test.roc ─ + |golden| pretty_assertions::assert_eq!( + golden, + &format!( + r###"── TAB CHARACTER ─────────────────────────────── tmp/comment_with_tab/Test.roc ─ - I encountered a tab character +I encountered a tab character - 4│ # comment with a - ^ +4│ # comment with a {} + ^ - Tab characters are not allowed. - "### +Tab characters are not allowed."###, + "\t" + ) + ) ); // TODO bad error message @@ -5680,23 +5684,27 @@ All branches in an `if` must have the same type! main = 5 -> 3 "# ), - @r###" - ── UNKNOWN OPERATOR ───────────────────────────── tmp/wild_case_arrow/Test.roc ─ + |golden| pretty_assertions::assert_eq!( + golden, + &format!( + r###"── UNKNOWN OPERATOR ───────────────────────────── tmp/wild_case_arrow/Test.roc ─ - This looks like an operator, but it's not one I recognize! +This looks like an operator, but it's not one I recognize! - 1│ app "test" provides [main] to "./platform" - 2│ - 3│ main = - 4│ main = 5 -> 3 - ^^ +1│ app "test" provides [main] to "./platform" +2│ +3│ main = +4│ main = 5 -> 3 + ^^ - Looks like you are trying to define a function. +Looks like you are trying to define a function.{} - In roc, functions are always written as a lambda, like +In roc, functions are always written as a lambda, like{} - increment = \n -> n + 1 - "### + increment = \n -> n + 1"###, + ' ', ' ' + ) + ) ); #[test] @@ -9965,16 +9973,21 @@ All branches in an `if` must have the same type! f 1 _ 1 "# ), - @r###" - ── SYNTAX PROBLEM ──────────────────────────────────────── /code/proj/Main.roc ─ + |golden| pretty_assertions::assert_eq!( + golden, + &format!( + r###"── SYNTAX PROBLEM ──────────────────────────────────────── /code/proj/Main.roc ─ - Underscores are not allowed in identifier names: +Underscores are not allowed in identifier names: - 6│ f 1 _ 1 +6│ f 1 _ 1 +{} - - I recommend using camelCase, it is the standard in the Roc ecosystem. - "### +I recommend using camelCase. It's the standard style in Roc code! +"###, + " " // TODO make the reporter not insert extraneous spaces here in the first place! + ), + ) ); test_report!(