Skip to content

Commit 860c336

Browse files
committed
refactor(test): matches_offsets -> assert_offsets
Reduce use of Google Test matchers to simplify the code a bit.
1 parent e848ab5 commit 860c336

7 files changed

+168
-159
lines changed

test/quick-lint-js/parse-support.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,19 @@ std::string summarize(std::optional<Expression*> expression) {
352352
}
353353
}
354354

355+
std::ostream& operator<<(std::ostream& out, Source_Code_Span_Offsets offsets) {
356+
out << offsets.begin << '-' << offsets.end;
357+
return out;
358+
}
359+
360+
bool operator==(Source_Code_Span_Offsets lhs, Source_Code_Span_Offsets rhs) {
361+
return lhs.begin == rhs.begin && lhs.end == rhs.end;
362+
}
363+
364+
bool operator!=(Source_Code_Span_Offsets lhs, Source_Code_Span_Offsets rhs) {
365+
return !(lhs == rhs);
366+
}
367+
355368
void Test_Parser::assert_diagnostics(Span<const Diagnostic_Assertion> diags,
356369
Source_Location caller) {
357370
quick_lint_js::assert_diagnostics(this->code, this->errors, diags, caller);

test/quick-lint-js/parse-support.h

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ void summarize(Expression*, std::string& out);
4343
std::string summarize(Expression*);
4444
std::string summarize(std::optional<Expression*>);
4545

46+
struct Source_Code_Span_Offsets {
47+
std::ptrdiff_t begin;
48+
std::ptrdiff_t end;
49+
50+
friend std::ostream& operator<<(std::ostream&, Source_Code_Span_Offsets);
51+
friend bool operator==(Source_Code_Span_Offsets, Source_Code_Span_Offsets);
52+
friend bool operator!=(Source_Code_Span_Offsets, Source_Code_Span_Offsets);
53+
};
54+
4655
inline constexpr Parser_Options javascript_options = [] {
4756
Parser_Options options;
4857
options.jsx = false;
@@ -145,16 +154,29 @@ class Test_Parser {
145154
return this->parser_.enter_function(attributes);
146155
}
147156

148-
// See offsets_matcher's constructor.
149-
Offsets_Matcher matches_offsets(CLI_Source_Position::Offset_Type begin_offset,
150-
String8_View text) {
151-
return Offsets_Matcher(&this->code_, begin_offset, text);
157+
Source_Code_Span_Offsets offsets(Source_Code_Span span) {
158+
return Source_Code_Span_Offsets{
159+
.begin = span.begin() - this->code_.data(),
160+
.end = span.end() - this->code_.data(),
161+
};
162+
}
163+
164+
void assert_offsets(Source_Code_Span span, std::ptrdiff_t expected_begin,
165+
std::ptrdiff_t expected_end,
166+
Source_Location caller = Source_Location::current()) {
167+
EXPECT_EQ_AT_CALLER(this->offsets(span), (Source_Code_Span_Offsets{
168+
.begin = expected_begin,
169+
.end = expected_end,
170+
}));
152171
}
153172

154-
// See offsets_matcher's constructor.
155-
Offsets_Matcher matches_offsets(CLI_Source_Position::Offset_Type begin_offset,
156-
CLI_Source_Position::Offset_Type end_offset) {
157-
return Offsets_Matcher(&this->code_, begin_offset, end_offset);
173+
void assert_offsets(Source_Code_Span span, std::ptrdiff_t expected_begin,
174+
String8_View expected_characters,
175+
Source_Location caller = Source_Location::current()) {
176+
this->assert_offsets(span, expected_begin,
177+
expected_begin + narrow_cast<std::ptrdiff_t>(
178+
expected_characters.size()),
179+
caller);
158180
}
159181

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

test/test-parse-conditional-expression.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ TEST_P(Test_Parse_Conditional_Expression, conditional_expression) {
3737
EXPECT_EQ(summarize(ast->child_0()), "var x");
3838
EXPECT_EQ(summarize(ast->child_1()), "var y");
3939
EXPECT_EQ(summarize(ast->child_2()), "var z");
40-
EXPECT_THAT(ast->span(), p.matches_offsets(0, 5));
40+
p.assert_offsets(ast->span(), 0, 5);
4141
}
4242

4343
{
@@ -80,7 +80,7 @@ TEST_P(Test_Parse_Conditional_Expression,
8080
{
8181
u8"^ Diag_Missing_Operand_For_Operator"_diag,
8282
});
83-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"? b : c"_sv));
83+
p.assert_offsets(ast->span(), 0, u8"? b : c"_sv);
8484
}
8585
}
8686

@@ -94,7 +94,7 @@ TEST_P(Test_Parse_Conditional_Expression,
9494
{
9595
u8" ^ Diag_Missing_Operand_For_Operator"_diag,
9696
});
97-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"a ? : c"_sv));
97+
p.assert_offsets(ast->span(), 0, u8"a ? : c"_sv);
9898
}
9999
}
100100

@@ -108,7 +108,7 @@ TEST_P(Test_Parse_Conditional_Expression,
108108
{
109109
u8" ^ Diag_Missing_Operand_For_Operator"_diag,
110110
});
111-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"a ? b : "_sv));
111+
p.assert_offsets(ast->span(), 0, u8"a ? b : "_sv);
112112
}
113113

114114
{
@@ -119,8 +119,7 @@ TEST_P(Test_Parse_Conditional_Expression,
119119
{
120120
u8" ^ Diag_Missing_Operand_For_Operator"_diag,
121121
});
122-
EXPECT_THAT(ast->child_0()->span(),
123-
p.matches_offsets(u8"("_sv.size(), u8"a ? b :)"_sv));
122+
p.assert_offsets(ast->child_0()->span(), u8"("_sv.size(), u8"a ? b :)"_sv);
124123
}
125124
}
126125

@@ -136,7 +135,7 @@ TEST_P(Test_Parse_Conditional_Expression,
136135
u8" ` Diag_Missing_Colon_In_Conditional_Expression.expected_colon\n"_diag
137136
u8" ^ .question"_diag,
138137
});
139-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"a ? b"_sv));
138+
p.assert_offsets(ast->span(), 0, u8"a ? b"_sv);
140139
}
141140

142141
{
@@ -149,7 +148,7 @@ TEST_P(Test_Parse_Conditional_Expression,
149148
u8" ` Diag_Missing_Colon_In_Conditional_Expression.expected_colon\n"_diag
150149
u8" ^ .question"_diag,
151150
});
152-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"a ? b"_sv));
151+
p.assert_offsets(ast->span(), 0, u8"a ? b"_sv);
153152
}
154153

155154
{
@@ -162,8 +161,7 @@ TEST_P(Test_Parse_Conditional_Expression,
162161
u8" ` Diag_Missing_Colon_In_Conditional_Expression.expected_colon\n"_diag
163162
u8" ^ .question"_diag,
164163
});
165-
EXPECT_THAT(ast->child_0()->span(),
166-
p.matches_offsets(u8"("_sv.size(), u8"a ? b"_sv));
164+
p.assert_offsets(ast->child_0()->span(), u8"("_sv.size(), u8"a ? b"_sv);
167165
}
168166
}
169167

test/test-parse-expression-jsx.cpp

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ TEST_F(Test_Parse_Expression_JSX, self_closing_tag) {
107107
Test_Parser p(u8"<div />"_sv, jsx_options);
108108
Expression* ast = p.parse_expression();
109109
EXPECT_EQ(ast->kind(), Expression_Kind::JSX_Element);
110-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"<div />"_sv));
110+
p.assert_offsets(ast->span(), 0, u8"<div />"_sv);
111111
}
112112

113113
{
114114
Test_Parser p(u8"<my-web-component/ >"_sv, jsx_options);
115115
Expression* ast = p.parse_expression();
116116
EXPECT_EQ(ast->kind(), Expression_Kind::JSX_Element);
117-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"<my-web-component/ >"_sv));
117+
p.assert_offsets(ast->span(), 0, u8"<my-web-component/ >"_sv);
118118
}
119119
}
120120

@@ -123,16 +123,15 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_no_children) {
123123
Test_Parser p(u8"<div></div>"_sv, jsx_options);
124124
Expression* ast = p.parse_expression();
125125
EXPECT_EQ(ast->kind(), Expression_Kind::JSX_Element);
126-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"<div></div>"_sv));
126+
p.assert_offsets(ast->span(), 0, u8"<div></div>"_sv);
127127
}
128128

129129
{
130130
Test_Parser p(u8"<my-web-component>< / my-web-component>"_sv, jsx_options);
131131
Expression* ast = p.parse_expression();
132132
EXPECT_EQ(ast->kind(), Expression_Kind::JSX_Element);
133-
EXPECT_THAT(
134-
ast->span(),
135-
p.matches_offsets(0, u8"<my-web-component>< / my-web-component>"_sv));
133+
p.assert_offsets(ast->span(), 0,
134+
u8"<my-web-component>< / my-web-component>"_sv);
136135
}
137136
}
138137

@@ -141,8 +140,7 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_text_children) {
141140
Test_Parser p(u8"<div>hello world</div>"_sv, jsx_options);
142141
Expression* ast = p.parse_expression();
143142
EXPECT_EQ(summarize(ast), "jsxelement(div)");
144-
EXPECT_THAT(ast->span(),
145-
p.matches_offsets(0, u8"<div>hello world</div>"_sv));
143+
p.assert_offsets(ast->span(), 0, u8"<div>hello world</div>"_sv);
146144
}
147145
}
148146

@@ -151,7 +149,7 @@ TEST_F(Test_Parse_Expression_JSX, fragment_with_no_children) {
151149
Test_Parser p(u8"<></>"_sv, jsx_options);
152150
Expression* ast = p.parse_expression();
153151
EXPECT_EQ(ast->kind(), Expression_Kind::JSX_Fragment);
154-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"<></>"_sv));
152+
p.assert_offsets(ast->span(), 0, u8"<></>"_sv);
155153
}
156154
}
157155

@@ -160,7 +158,7 @@ TEST_F(Test_Parse_Expression_JSX, fragment_with_text_children) {
160158
Test_Parser p(u8"<>hello world</>"_sv, jsx_options);
161159
Expression* ast = p.parse_expression();
162160
EXPECT_EQ(summarize(ast), "jsxfragment()");
163-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"<>hello world</>"_sv));
161+
p.assert_offsets(ast->span(), 0, u8"<>hello world</>"_sv);
164162
}
165163
}
166164

@@ -169,12 +167,10 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_element_children) {
169167
Test_Parser p(u8"<div>hello <span>world</span>!</div>"_sv, jsx_options);
170168
Expression* ast = p.parse_expression();
171169
ASSERT_EQ(summarize(ast), "jsxelement(div, jsxelement(span))");
172-
EXPECT_THAT(ast->span(),
173-
p.matches_offsets(u8""_sv.size(),
174-
u8"<div>hello <span>world</span>!</div>"_sv));
175-
EXPECT_THAT(ast->child_0()->span(),
176-
p.matches_offsets(u8"<div>hello "_sv.size(),
177-
u8"<span>world</span>"_sv));
170+
p.assert_offsets(ast->span(), u8""_sv.size(),
171+
u8"<div>hello <span>world</span>!</div>"_sv);
172+
p.assert_offsets(ast->child_0()->span(), u8"<div>hello "_sv.size(),
173+
u8"<span>world</span>"_sv);
178174
}
179175
}
180176

@@ -183,12 +179,10 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_fragment_children) {
183179
Test_Parser p(u8"<div>hello <>world</>!</div>"_sv, jsx_options);
184180
Expression* ast = p.parse_expression();
185181
ASSERT_EQ(summarize(ast), "jsxelement(div, jsxfragment())");
186-
EXPECT_THAT(
187-
ast->span(),
188-
p.matches_offsets(u8""_sv.size(), u8"<div>hello <>world</>!</div>"_sv));
189-
EXPECT_THAT(
190-
ast->child_0()->span(),
191-
p.matches_offsets(u8"<div>hello "_sv.size(), u8"<>world</>"_sv));
182+
p.assert_offsets(ast->span(), u8""_sv.size(),
183+
u8"<div>hello <>world</>!</div>"_sv);
184+
p.assert_offsets(ast->child_0()->span(), u8"<div>hello "_sv.size(),
185+
u8"<>world</>"_sv);
192186
}
193187
}
194188

@@ -197,12 +191,10 @@ TEST_F(Test_Parse_Expression_JSX, fragment_with_element_children) {
197191
Test_Parser p(u8"<>hello <span>world</span>!</>"_sv, jsx_options);
198192
Expression* ast = p.parse_expression();
199193
ASSERT_EQ(summarize(ast), "jsxfragment(jsxelement(span))");
200-
EXPECT_THAT(ast->span(),
201-
p.matches_offsets(u8""_sv.size(),
202-
u8"<>hello <span>world</span>!</>"_sv));
203-
EXPECT_THAT(
204-
ast->child_0()->span(),
205-
p.matches_offsets(u8"<>hello "_sv.size(), u8"<span>world</span>"_sv));
194+
p.assert_offsets(ast->span(), u8""_sv.size(),
195+
u8"<>hello <span>world</span>!</>"_sv);
196+
p.assert_offsets(ast->child_0()->span(), u8"<>hello "_sv.size(),
197+
u8"<span>world</span>"_sv);
206198
}
207199

208200
{
@@ -228,10 +220,10 @@ TEST_F(Test_Parse_Expression_JSX, fragment_with_fragment_children) {
228220
Test_Parser p(u8"<>hello <>world</>!</>"_sv, jsx_options);
229221
Expression* ast = p.parse_expression();
230222
ASSERT_EQ(summarize(ast), "jsxfragment(jsxfragment())");
231-
EXPECT_THAT(ast->span(), p.matches_offsets(u8""_sv.size(),
232-
u8"<>hello <>world</>!</>"_sv));
233-
EXPECT_THAT(ast->child_0()->span(),
234-
p.matches_offsets(u8"<>hello "_sv.size(), u8"<>world</>"_sv));
223+
p.assert_offsets(ast->span(), u8""_sv.size(),
224+
u8"<>hello <>world</>!</>"_sv);
225+
p.assert_offsets(ast->child_0()->span(), u8"<>hello "_sv.size(),
226+
u8"<>world</>"_sv);
235227
}
236228
}
237229

@@ -240,8 +232,8 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_expression_children) {
240232
Test_Parser p(u8"<div>hello {name}!</div>"_sv, jsx_options);
241233
Expression* ast = p.parse_expression();
242234
ASSERT_EQ(summarize(ast), "jsxelement(div, var name)");
243-
EXPECT_THAT(ast->child_0()->span(),
244-
p.matches_offsets(u8"<div>hello {"_sv.size(), u8"name"_sv));
235+
p.assert_offsets(ast->child_0()->span(), u8"<div>hello {"_sv.size(),
236+
u8"name"_sv);
245237
}
246238

247239
{
@@ -262,16 +254,15 @@ TEST_F(Test_Parse_Expression_JSX, tag_with_attributes) {
262254
Test_Parser p(u8"<div className='header' />"_sv, jsx_options);
263255
Expression* ast = p.parse_expression();
264256
ASSERT_EQ(summarize(ast), "jsxelement(div)");
265-
EXPECT_THAT(ast->span(),
266-
p.matches_offsets(0, u8"<div className='header' />"_sv));
257+
p.assert_offsets(ast->span(), 0, u8"<div className='header' />"_sv);
267258
}
268259

269260
{
270261
Test_Parser p(u8"<div className={expr} />"_sv, jsx_options);
271262
Expression* ast = p.parse_expression();
272263
ASSERT_EQ(summarize(ast), "jsxelement(div, var expr)");
273-
EXPECT_THAT(ast->child_0()->span(),
274-
p.matches_offsets(u8"<div className={"_sv.size(), u8"expr"_sv));
264+
p.assert_offsets(ast->child_0()->span(), u8"<div className={"_sv.size(),
265+
u8"expr"_sv);
275266
}
276267

277268
{

test/test-parse-expression-typescript.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ TEST_F(Test_Parse_Expression_TypeScript, type_annotation) {
3434
Expression* ast = p.parse_expression();
3535
ASSERT_EQ(ast->kind(), Expression_Kind::Type_Annotated);
3636
EXPECT_EQ(summarize(ast->child_0()), "var x");
37-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"x: Type"_sv));
37+
p.assert_offsets(ast->span(), 0, u8"x: Type"_sv);
3838

3939
Spy_Visitor v;
4040
expression_cast<Expression::Type_Annotated*>(ast)->visit_type_annotation(v);
@@ -291,7 +291,7 @@ TEST_F(Test_Parse_Expression_TypeScript, as_type_assertion) {
291291
Expression* ast = p.parse_expression();
292292
ASSERT_EQ(ast->kind(), Expression_Kind::As_Type_Assertion);
293293
EXPECT_EQ(summarize(ast->child_0()), "var x");
294-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"x as y"_sv));
294+
p.assert_offsets(ast->span(), 0, u8"x as y"_sv);
295295
EXPECT_THAT(p.visits, ElementsAreArray({
296296
"visit_enter_type_scope", // as
297297
"visit_variable_type_use",
@@ -371,7 +371,7 @@ TEST_F(Test_Parse_Expression_TypeScript,
371371
Expression* ast = p.parse_expression();
372372
ASSERT_EQ(ast->kind(), Expression_Kind::As_Type_Assertion);
373373
EXPECT_EQ(summarize(ast->child_0()), "object()");
374-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"{} as const"_sv));
374+
p.assert_offsets(ast->span(), 0, u8"{} as const"_sv);
375375
}
376376
}
377377

@@ -479,7 +479,7 @@ TEST_F(Test_Parse_Expression_TypeScript, satisfies) {
479479
Expression* ast = p.parse_expression();
480480
ASSERT_EQ(ast->kind(), Expression_Kind::Satisfies);
481481
EXPECT_EQ(summarize(ast->child_0()), "var x");
482-
EXPECT_THAT(ast->span(), p.matches_offsets(0, u8"x satisfies y"_sv));
482+
p.assert_offsets(ast->span(), 0, u8"x satisfies y"_sv);
483483
EXPECT_THAT(p.visits, ElementsAreArray({
484484
"visit_enter_type_scope", // satisfies
485485
"visit_variable_type_use",

0 commit comments

Comments
 (0)