Skip to content

Commit

Permalink
refactor(test): avoid Diag_Collector in most uses of Test_Parser
Browse files Browse the repository at this point in the history
I want to kill Diag_Collector. Move some users over to Diag_List.

#1154
  • Loading branch information
strager committed Jan 8, 2024
1 parent 82cbbc3 commit ab00e85
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 78 deletions.
9 changes: 7 additions & 2 deletions test/quick-lint-js/parse-support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,13 @@ std::string summarize(std::optional<Expression*> expression) {

void Test_Parser::assert_diagnostics(Span<const Diagnostic_Assertion> diags,
Source_Location caller) {
quick_lint_js::assert_diagnostics(this->code, this->errors_.errors, diags,
caller);
quick_lint_js::assert_diagnostics(this->code, this->errors, diags, caller);
}

std::vector<Diag_Collector::Diag> Test_Parser::legacy_errors() {
Diag_Collector d;
d.report(this->diag_reporter_.diags());
return std::move(d.errors);
}

Spy_Visitor test_parse_and_visit_statement(String8_View input, No_Diags_Tag,
Expand Down
10 changes: 8 additions & 2 deletions test/quick-lint-js/parse-support.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class Test_Parser {

explicit Test_Parser(String8_View input, const Parser_Options& options,
Capture_Diags_Tag)
: code_(input), parser_(&this->code_, &this->errors_, options) {}
: code_(input), parser_(&this->code_, &this->diag_reporter_, options) {}

// Fails the test if there are any diagnostics during parsing.
explicit Test_Parser(String8_View input)
Expand Down Expand Up @@ -168,6 +168,9 @@ class Test_Parser {
Failing_Diag_Reporter failing_reporter_;
quick_lint_js::Parser parser_;

Monotonic_Allocator diag_allocator_{"Test_Parser::diag_allocator_"};
Diag_List_Diag_Reporter diag_reporter_{&this->diag_allocator_};

public:
// Aliases for convenience.
std::vector<std::string_view>& visits = this->errors_.visits;
Expand All @@ -180,8 +183,11 @@ class Test_Parser {
std::vector<Visited_Variable_Declaration>& variable_declarations =
this->errors_.variable_declarations;
std::vector<String8>& variable_uses = this->errors_.variable_uses;
std::vector<Diag_Collector::Diag>& errors = this->errors_.errors;
const Diag_List& errors = this->diag_reporter_.diags();
Padded_String_View code = Padded_String_View(&this->code_);

// TODO(#1154): Delete this.
std::vector<Diag_Collector::Diag> legacy_errors();
};

struct No_Diags_Tag {};
Expand Down
4 changes: 2 additions & 2 deletions test/test-parse-expression-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ TEST_F(Test_Parse_Expression_Statement,
SCOPED_TRACE(p.code);
p.parse_and_visit_statement();
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Operand_For_Operator, //
where, 0, op),
Expand Down Expand Up @@ -1066,7 +1066,7 @@ TEST_F(Test_Parse_Expression_Statement,
Test_Parser p(concat(u8"*\n"_sv, statement), capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_module();
EXPECT_THAT(p.errors, Not(IsEmpty()));
EXPECT_THAT(p.legacy_errors(), Not(IsEmpty()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/test-parse-expression-typescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ TEST_F(Test_Parse_Expression_TypeScript,
SCOPED_TRACE(code);
Test_Parser p(code.string_view(), typescript_options, capture_diags);
p.parse_and_visit_expression();
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
Expand Down
32 changes: 16 additions & 16 deletions test/test-parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ TEST_F(Test_Parse_Expression, parse_broken_math_expression) {
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "upassign(missing, literal)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Operand_For_Operator, //
where, 0, op),
Expand Down Expand Up @@ -1068,14 +1068,14 @@ TEST_F(Test_Parse_Expression,
} else if (test.expected_normal_function) {
// 'await' should look like an identifier.
EXPECT_EQ(summarize(ast), test.expected_normal_function);
EXPECT_THAT(p.errors, IsEmpty());
EXPECT_THAT(p.legacy_errors(), IsEmpty());
} else {
// 'await' doesn't look like an identifier. We should report an error
// and recover as if 'await' was an operator.
EXPECT_EQ(summarize(ast), test.expected_async_function);
if (test.code == u8"await await x"_sv) {
EXPECT_THAT(
p.errors,
p.legacy_errors(),
::testing::IsSupersetOf(
{DIAG_TYPE_OFFSETS(p.code, Diag_Await_Operator_Outside_Async,
await_operator, 0, u8"await"_sv),
Expand All @@ -1084,7 +1084,7 @@ TEST_F(Test_Parse_Expression,
u8"await"_sv)}));
} else {
std::size_t await_offset = test.code.find(u8"await"_sv);
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Await_Operator_Outside_Async, //
Expand All @@ -1106,7 +1106,7 @@ TEST_F(Test_Parse_Expression,
u8"^^^^^ Diag_Redundant_Await"_diag,
});
} else {
EXPECT_THAT(p.errors, IsEmpty());
EXPECT_THAT(p.legacy_errors(), IsEmpty());
}
}

Expand All @@ -1123,7 +1123,7 @@ TEST_F(Test_Parse_Expression,
u8"^^^^^ Diag_Redundant_Await"_diag,
});
} else {
EXPECT_THAT(p.errors, IsEmpty());
EXPECT_THAT(p.legacy_errors(), IsEmpty());
}
}
}
Expand Down Expand Up @@ -2213,7 +2213,7 @@ TEST_F(Test_Parse_Expression,
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "object(literal: missing)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code,
Diag_Missing_Value_For_Object_Literal_Entry, //
Expand All @@ -2226,7 +2226,7 @@ TEST_F(Test_Parse_Expression,
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "object(literal: missing, literal: var other)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code,
Diag_Missing_Value_For_Object_Literal_Entry, //
Expand Down Expand Up @@ -2471,7 +2471,7 @@ TEST_F(Test_Parse_Expression, malformed_object_literal) {
"object(literal: dot(var one, two))",
"object(literal: upassign(var one, var two))"));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Key_For_Object_Entry, //
expression, u8"{"_sv.size(),
Expand All @@ -2495,7 +2495,7 @@ TEST_F(Test_Parse_Expression, malformed_object_literal) {
"object(literal: dot(literal, two))",
"object(literal: literal = var two)"));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Key_For_Object_Entry, //
expression, u8"{"_sv.size(),
Expand All @@ -2513,7 +2513,7 @@ TEST_F(Test_Parse_Expression, malformed_object_literal) {
"object(literal: dot(literal, two))",
"object(literal: literal = var two)"));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Key_For_Object_Entry, //
expression, u8"{"_sv.size(),
Expand All @@ -2529,7 +2529,7 @@ TEST_F(Test_Parse_Expression, malformed_object_literal) {
EXPECT_EQ(summarize(ast),
"object(literal: rwunarysuffix(var one), literal: var two)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
UnorderedElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_Key_For_Object_Entry, //
expression, u8"{"_sv.size(),
Expand Down Expand Up @@ -2570,7 +2570,7 @@ TEST_F(Test_Parse_Expression, malformed_object_literal) {
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "object(literal: function)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Expand Down Expand Up @@ -3533,7 +3533,7 @@ TEST_F(Test_Parse_Expression, generator_misplaced_star) {
Test_Parser p(u8"(*function f(){})"_sv, capture_diags);
Expression* ast = p.parse_expression();
EXPECT_THAT(ast->child_0()->span(), p.matches_offsets(1, 16));
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({DIAG_TYPE(
Diag_Generator_Function_Star_Belongs_Before_Name)}));
}
Expand All @@ -3545,7 +3545,7 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "binary(unary(var a), var b)");
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
Expand All @@ -3564,7 +3564,7 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
EXPECT_EQ(summarize(ast), "typeof(binary(var a, var b))");
}
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
Expand Down
18 changes: 9 additions & 9 deletions test/test-parse-module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ TEST_F(Test_Parse_Module, export_default_of_variable_is_illegal) {
"visit_variable_declaration", // x
}));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Cannot_Export_Default_Variable, //
declaring_token, u8"export default "_sv.size(),
Expand Down Expand Up @@ -428,7 +428,7 @@ TEST_F(Test_Parse_Module,
p.parse_and_visit_statement();
EXPECT_THAT(p.visits, IsEmpty());
EXPECT_THAT(p.variable_uses, IsEmpty());
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Export_Variable_Named_Keyword, //
Expand All @@ -443,7 +443,7 @@ TEST_F(Test_Parse_Module,
p.parse_and_visit_statement();
EXPECT_THAT(p.visits, IsEmpty());
EXPECT_THAT(p.variable_uses, IsEmpty());
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Export_Variable_Named_Keyword, //
Expand Down Expand Up @@ -907,7 +907,7 @@ TEST_F(Test_Parse_Module, imported_modules_must_be_quoted) {
capture_diags);
p.parse_and_visit_statement();
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code, Diag_Cannot_Import_From_Unquoted_Module,
import_name, u8"import { test } from "_sv.size(),
Expand All @@ -927,7 +927,7 @@ TEST_F(Test_Parse_Module,
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_declaration", // (name)
}));
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Import_Variable_Named_Keyword, //
Expand All @@ -945,7 +945,7 @@ TEST_F(Test_Parse_Module,
"visit_variable_declaration", // (name)
}));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Import_Variable_Named_Keyword, //
Expand All @@ -962,7 +962,7 @@ TEST_F(Test_Parse_Module,
EXPECT_THAT(p.variable_declarations,
ElementsAreArray({import_decl(name)}));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Import_Variable_Named_Keyword, //
Expand All @@ -978,7 +978,7 @@ TEST_F(Test_Parse_Module,
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_declaration", // (name)
}));
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Import_Variable_Named_Keyword, //
Expand All @@ -994,7 +994,7 @@ TEST_F(Test_Parse_Module,
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_declaration", // (name)
}));
EXPECT_THAT(p.errors,
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Cannot_Import_Variable_Named_Keyword, //
Expand Down
2 changes: 1 addition & 1 deletion test/test-parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ TEST_F(Test_Parse_Statement, if_else_with_malformed_condition) {
"visit_variable_use", // e
"visit_exit_block_scope"}));
EXPECT_THAT(
p.errors,
p.legacy_errors(),
::testing::Not(::testing::Contains(DIAG_TYPE(Diag_Else_Has_No_If))));
}
}
Expand Down
Loading

0 comments on commit ab00e85

Please sign in to comment.