Skip to content

Commit

Permalink
refactor(test): delete Diag_Collector-based assert matcher code
Browse files Browse the repository at this point in the history
This is dead code. Delete it.

#1154
  • Loading branch information
strager committed Mar 6, 2024
1 parent 7a3de9f commit b2961f5
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 301 deletions.
18 changes: 1 addition & 17 deletions test/quick-lint-js/diag-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ Variable_Kind Diag_Matcher_Arg::get_variable_kind(

template <class State, class Field>
class Diag_Fields_Matcher_Impl_Base
: public testing::MatcherInterface<const Diag_Collector::Diag &>,
public testing::MatcherInterface<const Any_Diag_Pointer &> {
: public testing::MatcherInterface<const Any_Diag_Pointer &> {
public:
explicit Diag_Fields_Matcher_Impl_Base(State s) : state_(std::move(s)) {}

Expand All @@ -100,16 +99,6 @@ class Diag_Fields_Matcher_Impl_Base
this->DescribeTo(out);
}

bool MatchAndExplain(const Diag_Collector::Diag &error,
testing::MatchResultListener *listener) const final {
return this->MatchAndExplain(
Any_Diag_Pointer{
.type = error.type(),
.data = error.data(),
},
listener);
}

bool MatchAndExplain(const Any_Diag_Pointer &error,
testing::MatchResultListener *listener) const final {
bool type_matches = error.type == this->state_.type;
Expand Down Expand Up @@ -268,11 +257,6 @@ class Diag_Matcher_2::Impl final
}
};

Diag_Matcher_2::operator testing::Matcher<const Diag_Collector::Diag &>()
const {
return testing::Matcher<const Diag_Collector::Diag &>(new Impl(this->state_));
}

Diag_Matcher_2::operator testing::Matcher<const Any_Diag_Pointer &>() const {
return testing::Matcher<const Any_Diag_Pointer &>(new Impl(this->state_));
}
Expand Down
1 change: 0 additions & 1 deletion test/quick-lint-js/diag-matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ class Diag_Matcher_2 {
Diag_Matcher_2 &operator=(const Diag_Matcher_2 &) = default;
Diag_Matcher_2 &operator=(Diag_Matcher_2 &&) = default;

/*implicit*/ operator testing::Matcher<const Diag_Collector::Diag &>() const;
/*implicit*/ operator testing::Matcher<const Any_Diag_Pointer &>() const;

void DescribeTo(std::ostream *out) const {
Expand Down
80 changes: 0 additions & 80 deletions test/quick-lint-js/diagnostic-assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,21 +501,6 @@ Diagnostic_Assertion operator""_diag(
return Diagnostic_Assertion::parse_or_exit(specification);
}

void assert_diagnostics(Padded_String_View code,
const std::vector<Diag_Collector::Diag>& diagnostics,
Span<const Diagnostic_Assertion> assertions,
Source_Location caller) {
EXPECT_THAT_AT_CALLER(diagnostics, diagnostics_matcher(code, assertions));
}

void assert_diagnostics(Padded_String_View code,
const std::vector<Diag_Collector::Diag>& diagnostics,
std::initializer_list<Diagnostic_Assertion> assertions,
Source_Location caller) {
assert_diagnostics(code, diagnostics,
Span<const Diagnostic_Assertion>(assertions), caller);
}

void assert_diagnostics(Padded_String_View code, const Diag_List& diagnostics,
Span<const Diagnostic_Assertion> assertions,
Source_Location caller) {
Expand All @@ -529,71 +514,6 @@ void assert_diagnostics(Padded_String_View code, const Diag_List& diagnostics,
Span<const Diagnostic_Assertion>(assertions), caller);
}

// TODO(#1154): Delete in favor of diagnostics_matcher_2.
::testing::Matcher<const std::vector<Diag_Collector::Diag>&>
diagnostics_matcher(Padded_String_View code,
Span<const Diagnostic_Assertion> assertions) {
std::vector<Diag_Matcher_2> error_matchers;
for (const Diagnostic_Assertion& diag : assertions) {
Diagnostic_Assertion adjusted_diag =
diag.adjusted_for_escaped_characters(code.string_view());

std::vector<Diag_Matcher_2::Field> fields;
for (const Diagnostic_Assertion::Member& member : adjusted_diag.members) {
Diag_Matcher_2::Field field;
field.arg = Diag_Matcher_Arg{
.member_name = to_string_view(member.name),
.member_offset = member.offset,
.member_type = member.type,
};
switch (member.type) {
case Diagnostic_Arg_Type::source_code_span:
field.begin_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
member.span_begin_offset);
field.end_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
member.span_end_offset);
break;
case Diagnostic_Arg_Type::char8:
field.character = member.character;
break;
case Diagnostic_Arg_Type::enum_kind:
field.enum_kind = member.enum_kind;
break;
case Diagnostic_Arg_Type::string8_view:
field.string = member.string;
break;
case Diagnostic_Arg_Type::statement_kind:
field.statement_kind = member.statement_kind;
break;
case Diagnostic_Arg_Type::variable_kind:
field.variable_kind = member.variable_kind;
break;
default:
QLJS_ASSERT(false);
break;
}
fields.push_back(field);
}

error_matchers.push_back(
Diag_Matcher_2(code, adjusted_diag.type, std::move(fields)));
}
if (error_matchers.size() <= 1) {
// ElementsAreArray produces better diagnostics than
// UnorderedElementsAreArray.
return ::testing::ElementsAreArray(std::move(error_matchers));
} else {
return ::testing::UnorderedElementsAreArray(std::move(error_matchers));
}
}

::testing::Matcher<const std::vector<Diag_Collector::Diag>&>
diagnostics_matcher(Padded_String_View code,
std::initializer_list<Diagnostic_Assertion> assertions) {
return diagnostics_matcher(code,
Span<const Diagnostic_Assertion>(assertions));
}

namespace {
class Diag_List_Matcher_Impl
: public testing::MatcherInterface<const Diag_List&> {
Expand Down
16 changes: 0 additions & 16 deletions test/quick-lint-js/diagnostic-assertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,29 +223,13 @@ struct Diagnostic_Assertion {
Diagnostic_Assertion operator""_diag(const Char8* specification,
std::size_t specification_length);

void assert_diagnostics(Padded_String_View code,
const std::vector<Diag_Collector::Diag>& diagnostics,
Span<const Diagnostic_Assertion> assertions,
Source_Location caller);
void assert_diagnostics(Padded_String_View code,
const std::vector<Diag_Collector::Diag>& diagnostics,
std::initializer_list<Diagnostic_Assertion> assertions,
Source_Location caller = Source_Location::current());

void assert_diagnostics(Padded_String_View code, const Diag_List& diagnostics,
Span<const Diagnostic_Assertion> assertions,
Source_Location caller);
void assert_diagnostics(Padded_String_View code, const Diag_List& diagnostics,
std::initializer_list<Diagnostic_Assertion> assertions,
Source_Location caller = Source_Location::current());

::testing::Matcher<const std::vector<Diag_Collector::Diag>&>
diagnostics_matcher(Padded_String_View code,
Span<const Diagnostic_Assertion> assertions);
::testing::Matcher<const std::vector<Diag_Collector::Diag>&>
diagnostics_matcher(Padded_String_View code,
std::initializer_list<Diagnostic_Assertion> assertions);

::testing::Matcher<const Diag_List&> diagnostics_matcher_2(
Padded_String_View code, Span<const Diagnostic_Assertion> assertions);
::testing::Matcher<const Diag_List&> diagnostics_matcher_2(
Expand Down
187 changes: 0 additions & 187 deletions test/test-diagnostic-assertion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,193 +562,6 @@ TEST(Test_Diagnostic_Assertion,
}
}

TEST(Test_Diagnostic_Assertion, match_error_type_with_1_field) {
Padded_String code(u8"hello"_sv);

::testing::Matcher continue_matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Continue"_diag});
EXPECT_TRUE(continue_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[0], &code[5]),
}),
}));
EXPECT_FALSE(continue_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Break{
.break_statement = Source_Code_Span(&code[0], &code[5]),
}),
}));

::testing::Matcher break_matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Break"_diag});
EXPECT_FALSE(break_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[0], &code[5]),
}),
}));
EXPECT_TRUE(break_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Break{
.break_statement = Source_Code_Span(&code[0], &code[5]),
}),
}));
}

TEST(Test_Diagnostic_Assertion, match_error_type_with_1_field_message) {
Padded_String code(u8"hello"_sv);
::testing::Matcher matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Continue"_diag});
Diag_Collector::Diag value(Diag_Invalid_Break{
.break_statement = Source_Code_Span(&code[0], &code[5]),
});
EXPECT_EQ(get_matcher_message(matcher, {value}),
"whose element #0 doesn't match, whose type (Diag_Invalid_Break) "
"isn't Diag_Invalid_Continue");
}

TEST(Test_Diagnostic_Assertion, match_offsets_of_1_field_span) {
Padded_String code(u8"hello"_sv);

::testing::Matcher continue_matcher =
diagnostics_matcher(&code, {u8" ^^^^ Diag_Invalid_Continue"_diag});
EXPECT_TRUE(continue_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[1], &code[5]),
}),
}));
EXPECT_FALSE(continue_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[0], &code[5]),
}),
}));
EXPECT_FALSE(continue_matcher.Matches({
Diag_Collector::Diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[0], &code[4]),
}),
}));
}

TEST(Test_Diagnostic_Assertion, match_offsets_of_1_field_message) {
Padded_String code(u8"hello"_sv);

{
::testing::Matcher matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Continue"_diag});
Diag_Collector::Diag value(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[1], &code[4]),
});
EXPECT_EQ(get_matcher_message(matcher, {value}),
"whose element #0 doesn't match, whose .continue_statement (1-4) "
"doesn't equal 0-5");
}

{
::testing::Matcher matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Break"_diag});
Diag_Collector::Diag value(Diag_Invalid_Break{
.break_statement = Source_Code_Span(&code[1], &code[4]),
});
EXPECT_EQ(get_matcher_message(matcher, {value}),
"whose element #0 doesn't match, whose .break_statement (1-4) "
"doesn't equal 0-5");
}
}

TEST(Test_Diagnostic_Assertion, match_span_and_char8) {
Padded_String code(u8"(hello"_sv);

::testing::Matcher matcher = diagnostics_matcher(
&code,
{u8"^ Diag_Expected_Parenthesis_Around_Do_While_Condition.where{.token=)}"_diag});
EXPECT_TRUE(matcher.Matches({
Diag_Collector::Diag(Diag_Expected_Parenthesis_Around_Do_While_Condition{
.where = Source_Code_Span(&code[0], &code[1]),
.token = u8')',
}),
}));
EXPECT_FALSE(matcher.Matches({
Diag_Collector::Diag(Diag_Expected_Parenthesis_Around_Do_While_Condition{
.where = Source_Code_Span(&code[0], &code[1]),
.token = u8'(',
}),
}));
}

TEST(Test_Diagnostic_Assertion, char8_message) {
Padded_String code(u8"hello"_sv);

::testing::Matcher matcher = diagnostics_matcher(
&code,
{u8"^ Diag_Expected_Parenthesis_Around_Do_While_Condition.where{.token=)}"_diag});

Diag_Collector::Diag value(
Diag_Expected_Parenthesis_Around_Do_While_Condition{
.where = Source_Code_Span(&code[0], &code[1]),
.token = u8'(',
});
EXPECT_EQ(get_matcher_message(matcher, {value}),
"whose element #0 doesn't match, whose .where (0-1) equals 0-1 and "
"whose .token ('(') doesn't equal ')'");
}

TEST(Test_Diagnostic_Assertion, match_span_and_string8_view) {
Padded_String code(u8"hi"_sv);

::testing::Matcher matcher = diagnostics_matcher(
&code,
{u8"^ Diag_Integer_Literal_Will_Lose_Precision.characters{.rounded_val=hello}"_diag});
EXPECT_TRUE(matcher.Matches({
Diag_Collector::Diag(Diag_Integer_Literal_Will_Lose_Precision{
.characters = Source_Code_Span(&code[0], &code[1]),
.rounded_val = u8"hello"_sv,
}),
}));
EXPECT_FALSE(matcher.Matches({
Diag_Collector::Diag(Diag_Integer_Literal_Will_Lose_Precision{
.characters = Source_Code_Span(&code[0], &code[1]),
.rounded_val = u8"HELLO"_sv,
}),
}));
}

TEST(Test_Diagnostic_Assertion, string8_view_message) {
Padded_String code(u8"hi"_sv);

::testing::Matcher matcher = diagnostics_matcher(
&code,
{u8"^ Diag_Integer_Literal_Will_Lose_Precision.characters{.rounded_val=hello}"_diag});

Diag_Collector::Diag value(Diag_Integer_Literal_Will_Lose_Precision{
.characters = Source_Code_Span(&code[0], &code[1]),
.rounded_val = u8"HELLO"_sv,
});
EXPECT_EQ(
get_matcher_message(matcher, {value}),
"whose element #0 doesn't match, whose .characters (0-1) equals 0-1 and "
"whose .rounded_val (\"HELLO\") doesn't equal \"hello\"");
}

TEST(Test_Diagnostic_Assertion, multiple_diagnostics_are_matched_in_any_order) {
Padded_String code(u8"hello"_sv);

::testing::Matcher continue_break_matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Continue"_diag,
u8"^^^^^ Diag_Invalid_Break"_diag});
::testing::Matcher break_continue_matcher =
diagnostics_matcher(&code, {u8"^^^^^ Diag_Invalid_Break"_diag,
u8"^^^^^ Diag_Invalid_Continue"_diag});

Diag_Collector::Diag continue_diag(Diag_Invalid_Continue{
.continue_statement = Source_Code_Span(&code[0], &code[5]),
});
Diag_Collector::Diag break_diag(Diag_Invalid_Break{
.break_statement = Source_Code_Span(&code[0], &code[5]),
});

EXPECT_TRUE(break_continue_matcher.Matches({break_diag, continue_diag}));
EXPECT_TRUE(break_continue_matcher.Matches({continue_diag, break_diag}));
EXPECT_TRUE(continue_break_matcher.Matches({break_diag, continue_diag}));
EXPECT_TRUE(continue_break_matcher.Matches({continue_diag, break_diag}));
}

class Test_Diagnostic_Assertion_2 : public ::testing::Test {
protected:
Monotonic_Allocator memory_{"Test_Diagnostic_Assertion_2"};
Expand Down

0 comments on commit b2961f5

Please sign in to comment.