Skip to content

Commit

Permalink
refactor(test): delete used matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Jan 9, 2024
1 parent 119f4d9 commit 6e28ace
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 575 deletions.
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ quick_lint_js_add_executable(
test-debug-server.cpp
test-diag-code-list.cpp
test-diag-list.cpp
test-diag-matcher.cpp
test-diagnostic-assertion.cpp
test-diagnostic-formatter.cpp
test-diagnostic.cpp
Expand Down
148 changes: 0 additions & 148 deletions test/quick-lint-js/diag-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,6 @@
#include <vector>

namespace quick_lint_js {
class Offsets_Matcher::Span_Impl
: public testing::MatcherInterface<const Source_Code_Span &> {
public:
explicit Span_Impl(Padded_String_View code,
CLI_Source_Position::Offset_Type begin_offset,
CLI_Source_Position::Offset_Type end_offset)
: code_(code), begin_offset_(begin_offset), end_offset_(end_offset) {}

void DescribeTo(std::ostream *out) const override {
*out << "has begin-end offset " << this->begin_offset_ << '-'
<< this->end_offset_;
}

void DescribeNegationTo(std::ostream *out) const override {
*out << "doesn't have begin-end offset " << this->begin_offset_ << '-'
<< this->end_offset_;
}

bool MatchAndExplain(const Source_Code_Span &span,
testing::MatchResultListener *listener) const override {
auto span_begin_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
span.begin() - this->code_.data());
auto span_end_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
span.end() - this->code_.data());
bool result = span_begin_offset == this->begin_offset_ &&
span_end_offset == this->end_offset_;
*listener << "whose begin-end offset (" << span_begin_offset << '-'
<< span_end_offset << ") "
<< (result ? "equals" : "doesn't equal") << " "
<< this->begin_offset_ << '-' << this->end_offset_;
return result;
}

private:
Padded_String_View code_;
CLI_Source_Position::Offset_Type begin_offset_;
CLI_Source_Position::Offset_Type end_offset_;
};

Offsets_Matcher::Offsets_Matcher(Padded_String_View input,
CLI_Source_Position::Offset_Type begin_offset,
CLI_Source_Position::Offset_Type end_offset)
: code_(input), begin_offset_(begin_offset), end_offset_(end_offset) {}

Offsets_Matcher::Offsets_Matcher(Padded_String_View input,
CLI_Source_Position::Offset_Type begin_offset,
String8_View text)
: code_(input),
begin_offset_(begin_offset),
end_offset_(begin_offset + text.size()) {}

Offsets_Matcher::Offsets_Matcher(Offsets_Matcher &&) = default;

Offsets_Matcher &Offsets_Matcher::operator=(Offsets_Matcher &&) = default;

Offsets_Matcher::~Offsets_Matcher() = default;

/*implicit*/ Offsets_Matcher::operator testing::Matcher<
const Source_Code_Span &>() const {
return testing::Matcher<const Source_Code_Span &>(
new Span_Impl(this->code_, this->begin_offset_, this->end_offset_));
}

Source_Code_Span Diag_Matcher_Arg::get_span(const void *error_object) const {
const void *member_data =
reinterpret_cast<const char *>(error_object) + this->member_offset;
Expand Down Expand Up @@ -196,55 +133,6 @@ class Diag_Fields_Matcher_Impl_Base
State state_;
};

Diag_Matcher::Diag_Matcher(Diag_Type type) : state_{type, std::nullopt, {}} {}

Diag_Matcher::Diag_Matcher(Padded_String_View input, Diag_Type type,
Field field_0)
: state_{type, input, {field_0}} {}

Diag_Matcher::Diag_Matcher(Padded_String_View input, Diag_Type type,
Field field_0, Field field_1)
: state_{type, input, {field_0, field_1}} {}

Diag_Matcher::Diag_Matcher(Padded_String_View input, Diag_Type type,
Field field_0, Field field_1, Field field_2)
: state_{type, input, {field_0, field_1, field_2}} {}

class Diag_Matcher::Impl final
: public Diag_Fields_Matcher_Impl_Base<Diag_Matcher::State,
Diag_Matcher::Field> {
public:
using Base =
Diag_Fields_Matcher_Impl_Base<Diag_Matcher::State, Diag_Matcher::Field>;

using Base::Diag_Fields_Matcher_Impl_Base;

protected:
bool field_matches(const Any_Diag_Pointer &error, const Field &f,
testing::MatchResultListener *listener) const override {
QLJS_ASSERT(this->state_.input.has_value());
Source_Code_Span span = f.arg.get_span(error.data);
auto span_begin_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
span.begin() - this->state_.input->data());
auto span_end_offset = narrow_cast<CLI_Source_Position::Offset_Type>(
span.end() - this->state_.input->data());
auto expected_end_offset = f.begin_offset + f.text.size();

bool span_matches = span_begin_offset == f.begin_offset &&
span_end_offset == expected_end_offset;
*listener << "whose ." << f.arg.member_name << " (" << span_begin_offset
<< "-" << span_end_offset << ") "
<< (span_matches ? "equals" : "doesn't equal") << " "
<< f.begin_offset << "-" << expected_end_offset;
return span_matches;
}
};

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

Diag_Matcher_2::Diag_Matcher_2(Padded_String_View input, Diag_Type type,
std::vector<Field> fields)
: state_{type, input, std::move(fields)} {}
Expand Down Expand Up @@ -339,42 +227,6 @@ Diag_Matcher_2::operator testing::Matcher<const Diag_Collector::Diag &>()
Diag_Matcher_2::operator testing::Matcher<const Any_Diag_Pointer &>() const {
return testing::Matcher<const Any_Diag_Pointer &>(new Impl(this->state_));
}

Diag_Spans_Matcher::Diag_Spans_Matcher(Diag_Type type, Field field_0)
: state_{type, {field_0}} {}

Diag_Spans_Matcher::Diag_Spans_Matcher(Diag_Type type, Field field_0,
Field field_1)
: state_{type, {field_0, field_1}} {}

class Diag_Spans_Matcher::Impl
: public Diag_Fields_Matcher_Impl_Base<Diag_Spans_Matcher::State,
Diag_Spans_Matcher::Field> {
public:
using Base = Diag_Fields_Matcher_Impl_Base<Diag_Spans_Matcher::State,
Diag_Spans_Matcher::Field>;

using Base::Diag_Fields_Matcher_Impl_Base;

protected:
bool field_matches(const Any_Diag_Pointer &error, const Field &f,
testing::MatchResultListener *listener) const override {
Source_Code_Span span = f.arg.get_span(error.data);
bool span_matches = same_pointers(span, f.expected);
*listener << "whose ." << f.arg.member_name << " (`"
<< out_string8(span.string_view()) << "` @"
<< reinterpret_cast<const void *>(span.begin()) << ") "
<< (span_matches ? "equals" : "doesn't equal") << " `"
<< out_string8(f.expected.string_view()) << "` @"
<< reinterpret_cast<const void *>(f.expected.begin());
return span_matches;
}
};

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

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
192 changes: 0 additions & 192 deletions test/quick-lint-js/diag-matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,116 +15,7 @@
#include <quick-lint-js/port/char8.h>
#include <vector>

#define DIAG_TYPE_FIELD(diag_type, member, matcher) \
::testing::VariantWith<diag_type>( \
::testing::Field(#member, &diag_type::member, matcher))

// Equivalent to ::testing::VariantWith<type>(::testing::_), but compiles much
// more quickly.
#define DIAG_TYPE(type) \
::quick_lint_js::Diag_Matcher(::quick_lint_js::Diag_Type::type)

// Equivalent to the following, but compiles much more quickly:
//
// DIAG_TYPE_FIELD(type, member_0,
// Offsets_Matcher(code, start_0, end_or_text_0))
//
// but compiles much more quickly.
#define DIAG_TYPE_OFFSETS(code, type, member_0, start_0, end_or_text_0) \
::quick_lint_js::Diag_Matcher(code, ::quick_lint_js::Diag_Type::type, \
::quick_lint_js::Diag_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_0), \
start_0, \
end_or_text_0, \
})

// Equivalent to the following, but compiles much more quickly:
//
// DIAG_TYPE_FIELD(type,
// member_0, Offsets_Matcher(code, start_0, end_or_text_0),
// member_1, Offsets_Matcher(code, start_1, end_or_text_1))
#define DIAG_TYPE_2_OFFSETS(code, type, member_0, start_0, end_or_text_0, \
member_1, start_1, end_or_text_1) \
::quick_lint_js::Diag_Matcher(code, ::quick_lint_js::Diag_Type::type, \
::quick_lint_js::Diag_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_0), \
start_0, \
end_or_text_0, \
}, \
::quick_lint_js::Diag_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_1), \
start_1, \
end_or_text_1, \
})

// Equivalent to the following, but compiles much more quickly:
//
// DIAG_TYPE_FIELD(type, member_0, source_code_span_matcher(span_0))
//
// but compiles much more quickly.
#define DIAG_TYPE_SPAN(type, member_0, span_0) \
::quick_lint_js::Diag_Spans_Matcher( \
::quick_lint_js::Diag_Type::type, \
::quick_lint_js::Diag_Spans_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_0), \
span_0, \
})

// Equivalent to the following, but compiles much more quickly:
//
// DIAG_TYPE_2_FIELD(type,
// member_0, source_code_span_matcher(span_0),
// member_1, source_code_span_matcher(span_1))
//
// but compiles much more quickly.
#define DIAG_TYPE_2_SPANS(type, member_0, span_0, member_1, span_1) \
::quick_lint_js::Diag_Spans_Matcher( \
::quick_lint_js::Diag_Type::type, \
::quick_lint_js::Diag_Spans_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_0), \
span_0, \
}, \
::quick_lint_js::Diag_Spans_Matcher::Field{ \
DIAG_MATCHER_ARG(type, member_1), \
span_1, \
})

namespace quick_lint_js {
class Offsets_Matcher {
public:
// Create an Offsets_Matcher which asserts that the matched Source_Code_Span
// begins at begin_offset and ends at end_offset.
explicit Offsets_Matcher(Padded_String_View input,
CLI_Source_Position::Offset_Type begin_offset,
CLI_Source_Position::Offset_Type end_offset);

// Create an Offsets_Matcher which asserts that the matched Source_Code_Span
// begins at begin_offset and ends at begin_offset+strlen(text).
//
// TODO(strager): Also ensure the matched Source_Code_Span's content equals
// text.
explicit Offsets_Matcher(Padded_String_View input,
CLI_Source_Position::Offset_Type begin_offset,
String8_View text);

Offsets_Matcher(const Offsets_Matcher &) = delete;
Offsets_Matcher &operator=(const Offsets_Matcher &) = delete;

Offsets_Matcher(Offsets_Matcher &&);
Offsets_Matcher &operator=(Offsets_Matcher &&);

~Offsets_Matcher();

/*implicit*/ operator testing::Matcher<const Source_Code_Span &>() const;

private:
class Span_Impl;

Padded_String_View code_;
CLI_Source_Position::Offset_Type begin_offset_;
CLI_Source_Position::Offset_Type end_offset_;
};

// Metadata for a member of a diagnostic class.
struct Diag_Matcher_Arg {
std::string_view member_name;
Expand Down Expand Up @@ -160,52 +51,6 @@ struct Diag_Matcher_Arg {
type::member)>(), \
})

// A mix of ::testing::VariantWith, ::testing::Field, and Offsets_Matcher. These
// are combined into one matcher to significantly reduce compile times.
//
// See DIAG_TYPE and DIAG_TYPE_OFFSETS for example usage.
class Diag_Matcher {
public:
struct Field {
// Must be Source_Code_Span.
Diag_Matcher_Arg arg;

CLI_Source_Position::Offset_Type begin_offset;
String8_View text;
};

explicit Diag_Matcher(Diag_Type type);

// Create an Offsets_Matcher which asserts that an error's Source_Code_Span
// begins at field.begin_offset and ends at
// field.begin_offset+strlen(field.text).
//
// TODO(strager): Also ensure the error's Source_Code_Span's content equals
// text.
explicit Diag_Matcher(Padded_String_View input, Diag_Type type, Field);
explicit Diag_Matcher(Padded_String_View input, Diag_Type type, Field, Field);
explicit Diag_Matcher(Padded_String_View input, Diag_Type type, Field, Field,
Field);

Diag_Matcher(const Diag_Matcher &) = default;
Diag_Matcher(Diag_Matcher &&) = default;
Diag_Matcher &operator=(const Diag_Matcher &) = default;
Diag_Matcher &operator=(Diag_Matcher &&) = default;

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

private:
class Impl;

struct State {
Diag_Type type;
std::optional<Padded_String_View> input;
std::vector<Field> fields;
};

State state_;
};

struct Any_Diag_Pointer {
Diag_Type type;
const void *data;
Expand Down Expand Up @@ -260,43 +105,6 @@ class Diag_Matcher_2 {

State state_;
};

// A mix of ::testing::VariantWith, ::testing::Field, and
// source_code_span_matcher. These are combined into one matcher to
// significantly reduce compile times.
//
// See DIAG_TYPE_SPAN for example usage.
class Diag_Spans_Matcher {
public:
struct Field {
// Must be Source_Code_Span.
Diag_Matcher_Arg arg;

Source_Code_Span expected;
};

// Create a matcher which asserts that an error's Source_Code_Span's
// begin and end pointers equal the expected span.
explicit Diag_Spans_Matcher(Diag_Type type, Field);
explicit Diag_Spans_Matcher(Diag_Type type, Field, Field);

Diag_Spans_Matcher(const Diag_Spans_Matcher &) = default;
Diag_Spans_Matcher(Diag_Spans_Matcher &&) = default;
Diag_Spans_Matcher &operator=(const Diag_Spans_Matcher &) = default;
Diag_Spans_Matcher &operator=(Diag_Spans_Matcher &&) = default;

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

private:
class Impl;

struct State {
Diag_Type type;
std::vector<Field> fields;
};

State state_;
};
}

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
2 changes: 1 addition & 1 deletion test/quick-lint-js/parse-support.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Test_Parser {
expected_characters.size()),
caller);
}

Spy_Visitor& spy_visitor() { return this->errors_; }

void assert_diagnostics(Span<const Diagnostic_Assertion> diags,
Expand Down
Loading

0 comments on commit 6e28ace

Please sign in to comment.