mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-25 18:11:13 +00:00
Switch tests to use the rendered form of diagnostics (#8070)
Historically, we disabled the rendered form in tests outside of `toolchain/diagnostics`. The idea was to keep tests shorter and avoid very long chains of autoupdate before stabilizing due to all the line numbers and other artifacts in the rendered form. This PR adds `--diagnostic-line-numbers=[all|first]` and defaults to `first` in tests. This keeps the rendered form so that we can easily code review that form and the visual cues it does or doesn't provide. But it removes all but the first location's line numbers to provide significantly more stability in tests. By default, tests get the full unicode line-drawing form of diagnostics so we can ensure all of the artifacts there make sense with the specific case being diagnosed, but we do retain some focused ASCII-rendering tests to ensure that fallback remains usable. There are some issues here that will be fixed in subsequent PRs. Notably, we try to keep the frame together in the check lines, and so force some parts to use the first location. This largely works, but fails in some edge cases, causing the entire frame to be located incorrectly. There is a TODO about this that we need to fix, and it only occurs 6 times across the test suite, so it didn't seem worth front-loading a fix for here. Assisted-by: Claude Code
This commit is contained in:
1 parent
0b7059a360
commit
d0badecd2a
888 files changed
+39731
-12799
No files matched your search
@@ -91,15 +91,23 @@ static auto Narrow() -> Terminal::Capabilities {
|
||||
|
||||
// Renders `diagnostic` for `capabilities`.
|
||||
static auto Render(const Terminal::Capabilities& capabilities,
|
||||
const Diagnostic& diagnostic, bool snippets = true)
|
||||
-> std::string {
|
||||
const Diagnostic& diagnostic, bool snippets = true,
|
||||
LineNumbers line_numbers = LineNumbers::All) -> std::string {
|
||||
Renderer renderer(capabilities);
|
||||
renderer.set_snippets(snippets);
|
||||
renderer.set_line_numbers(line_numbers);
|
||||
llvm::SmallString<256> bytes;
|
||||
renderer.Render(bytes, diagnostic);
|
||||
return std::string(bytes);
|
||||
}
|
||||
|
||||
// Renders `diagnostic` framed, with `LineNumbers::First`.
|
||||
static auto RenderUnnumbered(const Terminal::Capabilities& capabilities,
|
||||
const Diagnostic& diagnostic) -> std::string {
|
||||
return Render(capabilities, diagnostic, /*snippets=*/true,
|
||||
LineNumbers::First);
|
||||
}
|
||||
|
||||
// Renders a diagnostic with a single message at `level`.
|
||||
static auto RenderOne(const Terminal::Capabilities& capabilities, Level level,
|
||||
Loc loc, llvm::StringRef text) -> std::string {
|
||||
@@ -1166,6 +1174,106 @@ TEST(RendererTest, FirstContextMessageTakesTheDiagnosticLevel) {
|
||||
"note: the problem\n"));
|
||||
}
|
||||
|
||||
TEST(RendererTest, UnnumberedGutterStillPointsToTheReportedLine) {
|
||||
Diagnostic diagnostic = {
|
||||
.level = Level::Error,
|
||||
.message = MakeMessage(At(3, 1, 2), "bad"),
|
||||
.labels = {MakeLabel(LabelCategory::Info, At(1, 1, 2), "here")}};
|
||||
// The same rows in the same columns as `MergedShowsASingleSkippedLine`,
|
||||
// with no numbers in the gutter. The `->` still points to the reported line.
|
||||
EXPECT_THAT(RenderUnnumbered(Plain(), diagnostic),
|
||||
Eq(" .-| foo.carbon:3:1\n"
|
||||
" | fn Run0() {}\n"
|
||||
" : -.\n"
|
||||
" : '--| here\n"
|
||||
" |\n"
|
||||
"-> | fn Main() {\n"
|
||||
" : ^^\n"
|
||||
" : '--| error: bad\n"
|
||||
" '----\n"
|
||||
""));
|
||||
}
|
||||
|
||||
TEST(RendererTest, UnnumberedFramesNameALaterFileAlone) {
|
||||
Loc other = {.filename = "bar.carbon",
|
||||
.line = "class C {",
|
||||
.line_number = 9,
|
||||
.column_number = 1,
|
||||
.length = 9};
|
||||
Diagnostic diagnostic = {
|
||||
.level = Level::Error,
|
||||
.message = MakeMessage(At(4, 3, 7), "bad"),
|
||||
.labels = {MakeLabel(LabelCategory::Info, other, "declared here")}};
|
||||
EXPECT_THAT(RenderUnnumbered(Plain(), diagnostic),
|
||||
Eq(" .-| foo.carbon:4:3\n"
|
||||
"-> | Run0(1);\n"
|
||||
" : ^^^^^^^\n"
|
||||
" : '--| error: bad\n"
|
||||
" :\n"
|
||||
" |-| bar.carbon\n"
|
||||
" | class C {\n"
|
||||
" : ----.----\n"
|
||||
" : '--| declared here\n"
|
||||
" '----\n"
|
||||
""));
|
||||
}
|
||||
|
||||
TEST(RendererTest, UnnumberedPathStepsKeepOnlyTheirFiles) {
|
||||
Loc import_loc = {.filename = "c.carbon",
|
||||
.line = "import library \"a\";",
|
||||
.line_number = 1,
|
||||
.column_number = 1,
|
||||
.length = 6};
|
||||
Loc reported = {.filename = "a.carbon",
|
||||
.line = "class Shape {}",
|
||||
.line_number = 3,
|
||||
.column_number = 1,
|
||||
.length = 14};
|
||||
Diagnostic diagnostic = {
|
||||
.level = Level::Error,
|
||||
.message = MakeMessage(reported, "duplicate name",
|
||||
{MakeOrigin(import_loc, "in import")})};
|
||||
// The step is drawn above the message's location row, but only the location
|
||||
// row keeps its full location.
|
||||
EXPECT_THAT(RenderUnnumbered(Plain(), diagnostic),
|
||||
Eq(" | .-- in import: c.carbon\n"
|
||||
" |-| a.carbon:3:1\n"
|
||||
"-> | class Shape {}\n"
|
||||
" : ^^^^^^^^^^^^^^\n"
|
||||
" : '--| error: duplicate name\n"
|
||||
" '----\n"
|
||||
""));
|
||||
}
|
||||
|
||||
TEST(RendererTest, UnnumberedFramesKeepTheFirstLocationRowsLocation) {
|
||||
Diagnostic diagnostic = {
|
||||
.level = Level::Error,
|
||||
.message = MakeMessage({}, "bad"),
|
||||
.labels = {MakeLabel(LabelCategory::Info, At(4, 3, 7), "here")}};
|
||||
// The message has no location, so its location row shows `<no location>` and
|
||||
// gets the `->`. The label's location row is the first with a location, so it
|
||||
// keeps that location in full; only the gutter's number is withheld.
|
||||
EXPECT_THAT(Render(Plain(), diagnostic), Eq("-> .-| <no location>\n"
|
||||
" : '--| error: bad\n"
|
||||
" :\n"
|
||||
" |-| foo.carbon:4:3\n"
|
||||
" 4 | Run0(1);\n"
|
||||
" : ---.---\n"
|
||||
" : '--| here\n"
|
||||
" '----\n"
|
||||
""));
|
||||
EXPECT_THAT(RenderUnnumbered(Plain(), diagnostic),
|
||||
Eq("-> .-| <no location>\n"
|
||||
" : '--| error: bad\n"
|
||||
" :\n"
|
||||
" |-| foo.carbon:4:3\n"
|
||||
" | Run0(1);\n"
|
||||
" : ---.---\n"
|
||||
" : '--| here\n"
|
||||
" '----\n"
|
||||
""));
|
||||
}
|
||||
|
||||
TEST(RendererTest, KindIsIncludedWhenAsked) {
|
||||
Renderer renderer(Plain());
|
||||
renderer.set_include_kind(true);
|
||||
@@ -2508,6 +2616,22 @@ TEST(RendererTest, AControlByteInAFilenameCannotBreakTheFrame) {
|
||||
EXPECT_THAT(rendered, Not(HasSubstr("\n\n"))) << rendered;
|
||||
}
|
||||
|
||||
TEST(RendererTest, AControlByteInAnUnnumberedFilenameCannotBreakTheFrame) {
|
||||
// With `LineNumbers::First`, a second file's location row shows only its
|
||||
// filename, and the newline in the filename is drawn as a space.
|
||||
Loc other = {.filename = "ab\ncd.carbon",
|
||||
.line = "class C {",
|
||||
.line_number = 9,
|
||||
.column_number = 1,
|
||||
.length = 9};
|
||||
Diagnostic diagnostic = {
|
||||
.level = Level::Error,
|
||||
.message = MakeMessage(At(4, 3, 7), "bad"),
|
||||
.labels = {MakeLabel(LabelCategory::Info, other, "declared here")}};
|
||||
std::string rendered = RenderUnnumbered(Plain(), diagnostic);
|
||||
EXPECT_THAT(rendered, HasSubstr("|-| ab cd.carbon\n")) << rendered;
|
||||
}
|
||||
|
||||
TEST(RendererTest, AMergedPairWithoutFileTextStillElides) {
|
||||
// A single skipped line is shown in place of an elision row, but only when
|
||||
// `Loc::file_text` offers a way to find it; without it the elision row says
|
||||
|
||||
Reference in new issue
Block a user