Skip to content

Commit d8b388d

Browse files
committed
refactor(test): simplify some users of Diag_List_Diag_Reporter
Use Diag_List instead of Diag_List_Diag_Reporter.
1 parent 40a07f9 commit d8b388d

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

test/test-lex.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ using ::testing::VariantWith;
3737

3838
namespace quick_lint_js {
3939
namespace {
40+
// Contains pointers to objects inside a Lexer.
41+
struct Tokens_And_Diags {
42+
std::vector<Token> tokens;
43+
Diag_List* diags;
44+
};
45+
4046
class Test_Lex : public ::testing::Test {
4147
protected:
4248
// NOTE(strager): These functions take callbacks as function pointers to
@@ -78,7 +84,7 @@ class Test_Lex : public ::testing::Test {
7884
void check_tokens_with_errors(
7985
String8_View input, Span<const Diagnostic_Assertion>,
8086
std::initializer_list<Token_Type> expected_token_types, Source_Location);
81-
std::vector<Token> lex_to_eof(Padded_String_View, Diag_Reporter&);
87+
Tokens_And_Diags lex_to_eof_returning_diags(Padded_String_View);
8288
std::vector<Token> lex_to_eof(Padded_String_View,
8389
Source_Location = Source_Location::current());
8490
std::vector<Token> lex_to_eof(String8_View,
@@ -2931,25 +2937,24 @@ void Test_Lex::check_single_token_with_errors(
29312937
void Test_Lex::check_single_token_with_errors(
29322938
String8_View input, Span<const Diagnostic_Assertion> diagnostic_assertions,
29332939
String8_View expected_identifier_name, Source_Location caller) {
2934-
Diag_List_Diag_Reporter errors(&this->memory_);
29352940
Padded_String code(input);
2936-
std::vector<Token> lexed_tokens = this->lex_to_eof(&code, errors);
2941+
Tokens_And_Diags lexed = this->lex_to_eof_returning_diags(&code);
29372942

29382943
EXPECT_THAT_AT_CALLER(
2939-
lexed_tokens,
2944+
lexed.tokens,
29402945
ElementsAreArray({
29412946
::testing::Field("type", &Token::type,
29422947
::testing::AnyOf(Token_Type::identifier,
29432948
Token_Type::private_identifier)),
29442949
}));
2945-
if (lexed_tokens.size() == 1 &&
2946-
(lexed_tokens[0].type == Token_Type::identifier ||
2947-
lexed_tokens[0].type == Token_Type::private_identifier)) {
2948-
EXPECT_EQ_AT_CALLER(lexed_tokens[0].identifier_name().normalized_name(),
2950+
if (lexed.tokens.size() == 1 &&
2951+
(lexed.tokens[0].type == Token_Type::identifier ||
2952+
lexed.tokens[0].type == Token_Type::private_identifier)) {
2953+
EXPECT_EQ_AT_CALLER(lexed.tokens[0].identifier_name().normalized_name(),
29492954
expected_identifier_name);
29502955
}
29512956

2952-
assert_diagnostics(&code, errors.diags(), diagnostic_assertions, caller);
2957+
assert_diagnostics(&code, *lexed.diags, diagnostic_assertions, caller);
29532958
}
29542959

29552960
void Test_Lex::check_tokens(
@@ -3003,31 +3008,29 @@ void Test_Lex::check_tokens_with_errors(
30033008
String8_View input, Span<const Diagnostic_Assertion> diag_assertions,
30043009
std::initializer_list<Token_Type> expected_token_types,
30053010
Source_Location caller) {
3006-
Diag_List_Diag_Reporter errors(&this->memory_);
30073011
Padded_String code(input);
3008-
std::vector<Token> lexed_tokens = this->lex_to_eof(&code, errors);
3012+
Tokens_And_Diags lexed = this->lex_to_eof_returning_diags(&code);
30093013

30103014
std::vector<Token_Type> lexed_token_types;
3011-
for (const Token& t : lexed_tokens) {
3015+
for (const Token& t : lexed.tokens) {
30123016
lexed_token_types.push_back(t.type);
30133017
}
30143018

30153019
EXPECT_THAT_AT_CALLER(lexed_token_types,
30163020
::testing::ElementsAreArray(expected_token_types));
30173021

3018-
assert_diagnostics(&code, errors.diags(), diag_assertions, caller);
3022+
assert_diagnostics(&code, *lexed.diags, diag_assertions, caller);
30193023
}
30203024

30213025
std::vector<Token> Test_Lex::lex_to_eof(Padded_String_View input,
30223026
Source_Location caller) {
3023-
Diag_List_Diag_Reporter diags(&this->memory_);
3024-
std::vector<Token> tokens = this->lex_to_eof(input, diags);
3025-
assert_diagnostics(input, diags.diags(), {}, caller);
3026-
return tokens;
3027+
Tokens_And_Diags lexed = this->lex_to_eof_returning_diags(input);
3028+
assert_diagnostics(input, *lexed.diags, {}, caller);
3029+
return lexed.tokens;
30273030
}
30283031

3029-
std::vector<Token> Test_Lex::lex_to_eof(Padded_String_View input,
3030-
Diag_Reporter& errors) {
3032+
Tokens_And_Diags Test_Lex::lex_to_eof_returning_diags(
3033+
Padded_String_View input) {
30313034
Lexer& l = this->make_lexer(input);
30323035
std::vector<Token> tokens;
30333036
while (l.peek().type != Token_Type::end_of_file) {
@@ -3038,11 +3041,10 @@ std::vector<Token> Test_Lex::lex_to_eof(Padded_String_View input,
30383041
l.skip();
30393042
}
30403043
}
3041-
// TODO(#1154): Remove or deduplicate with quick_lint_js::parse_and_lint.
3042-
l.diags().for_each([&](Diag_Type diag_type, void* diag_data) {
3043-
errors.report_impl(diag_type, diag_data);
3044-
});
3045-
return tokens;
3044+
return Tokens_And_Diags{
3045+
.tokens = std::move(tokens),
3046+
.diags = &l.diags(),
3047+
};
30463048
}
30473049

30483050
std::vector<Token> Test_Lex::lex_to_eof(String8_View input,

0 commit comments

Comments
 (0)