37# include <gtest/gtest.h>
47TEST(Diagnostics, ErrorBuilderStoresPrimaryLabelAndCode)
53 const auto where = sm.
span(
id, 8, 9);
54 dx.
error(where,
"expected expression")
56 .
note(
"parser recovered at ';'")
57 .
help(
"insert an expression after '='")
60 ASSERT_EQ(dx.
size(), 1u);
64 const auto & diag = dx.
get(0);
65 EXPECT_EQ(diag.code,
"E001");
66 EXPECT_EQ(diag.message,
"expected expression");
67 ASSERT_EQ(diag.labels.size(), 1u);
68 EXPECT_EQ(diag.labels.access(0).kind, Diagnostic_Label_Kind::Primary);
69 EXPECT_EQ(diag.notes.size(), 1u);
70 EXPECT_EQ(diag.help.size(), 1u);
74TEST(Diagnostics, SecondaryLabelsAndCountersWork)
80 dx.
warning(sm.
span(
id, 8, 9),
"use before declaration")
85 ASSERT_EQ(dx.
size(), 1u);
89 const auto & diag = dx.
get(0);
90 ASSERT_EQ(diag.labels.size(), 2u);
91 EXPECT_EQ(diag.labels.access(1).kind, Diagnostic_Label_Kind::Secondary);
95TEST(Diagnostics, RenderPlainIncludesFileLocationAndCaret)
101 dx.
error(sm.
span(
id, 8, 9),
"expected expression")
103 .
note(
"parser recovered at ';'")
104 .
help(
"insert an expression after '='")
107 std::ostringstream
out;
109 const std::string text =
out.str();
111 EXPECT_NE(text.find(
"error[E001]: expected expression"), std::string::npos);
112 EXPECT_NE(text.find(
"main.aw:1:9"), std::string::npos);
113 EXPECT_NE(text.find(
"1 | let x = ;"), std::string::npos);
114 EXPECT_NE(text.find(
"^"), std::string::npos);
115 EXPECT_NE(text.find(
"note: parser recovered at ';'"), std::string::npos);
116 EXPECT_NE(text.find(
"help: insert an expression after '='"), std::string::npos);
120TEST(Diagnostics, RenderingIsDeterministic)
126 dx.
warning(sm.
span(
id, 0, 3),
"call target is unresolved")
129 dx.
error(sm.
span(
id, 3, 4),
"expected ')'")
133 std::ostringstream a;
134 std::ostringstream b;
138 EXPECT_EQ(a.str(), b.str());
142TEST(Diagnostics, ClearResetsState)
151 ASSERT_EQ(dx.
size(), 2u);
157 EXPECT_EQ(dx.
size(), 0u);
Plain-text diagnostic engine for compiler-style tooling.
size_t size_t int32_t * out
Diagnostic_Builder & code(const std::string &value)
Sets the stable diagnostic code.
size_t emit() const noexcept
Finalizes the builder and returns the diagnostic index.
Diagnostic_Builder & help(const std::string &msg)
Appends a help line.
Diagnostic_Builder & secondary(const Source_Span &span, const std::string &msg)
Adds a secondary label.
Diagnostic_Builder & note(const std::string &msg)
Appends a note line.
Diagnostic accumulator and renderer.
size_t warning_count() const noexcept
Returns the number of warnings.
void clear() noexcept
Removes all accumulated diagnostics and resets counters.
void render_plain(std::ostream &out) const
Renders all diagnostics as deterministic plain text.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
Diagnostic_Builder warning(const Source_Span &span, const std::string &msg)
Starts a warning diagnostic.
const Diagnostic & get(const size_t i) const
Returns diagnostic i.
size_t error_count() const noexcept
Returns the number of error and fatal diagnostics.
Diagnostic_Builder error(const Source_Span &span, const std::string &msg)
Starts an error diagnostic.
size_t size() const noexcept
Returns the number of stored diagnostics.
Stores source files and resolves offsets into human-readable data.
Source_Span span(const Source_File_Id id, const Source_Offset begin, const Source_Offset end) const
Creates a validated span in file id.
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Main namespace for Aleph-w library functions.