Skip to content

Commit 262ad6c

Browse files
committed
refactor(test): replace DIAG_TYPE_2_OFFSETS -> DIAGNOSTIC_ASSERTION_2_SPANS
#1154
1 parent ece8f2f commit 262ad6c

6 files changed

+90
-60
lines changed

test/quick-lint-js/diagnostic-assertion.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ struct Diagnostic_Assertion {
169169
// Create a Diagnostic_Assertion which matches 'type_'. It asserts that
170170
// 'type_::member_' is a Source_Code_Span beginning at 'begin_offset_' and
171171
// ending at 'begin_offset_ + span_string_.size()'.
172+
//
173+
// If you need to match two fields of the diagnostic type, see
174+
// DIAGNOSTIC_ASSERTION_2_SPANS.
172175
#define DIAGNOSTIC_ASSERTION_SPAN(type_, member_, begin_offset_, span_string_) \
173176
(::quick_lint_js::Diagnostic_Assertion::make_raw( \
174177
Diag_Type::type_, \
@@ -183,6 +186,37 @@ struct Diagnostic_Assertion {
183186
(span_string_).size())), \
184187
}))
185188

189+
// Create a Diagnostic_Assertion which matches 'type_'.
190+
//
191+
// It asserts that 'type_::member_0_' is a Source_Code_Span beginning at
192+
// 'begin_offset_0_' and ending at 'begin_offset_0_ + span_string_0_.size()'.
193+
//
194+
// It asserts that 'type_::member_1_' is a Source_Code_Span beginning at
195+
// 'begin_offset_1_' and ending at 'begin_offset_1_ + span_string_1_.size()'.
196+
#define DIAGNOSTIC_ASSERTION_2_SPANS(type_, member_0_, begin_offset_0_, \
197+
span_string_0_, member_1_, \
198+
begin_offset_1_, span_string_1_) \
199+
(::quick_lint_js::Diagnostic_Assertion::make_raw( \
200+
Diag_Type::type_, \
201+
{ \
202+
::quick_lint_js::Diagnostic_Assertion::Member::make_span( \
203+
QLJS_CPP_QUOTE_U8_SV(member_0_), offsetof(type_, member_0_), \
204+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
205+
(begin_offset_0_)), \
206+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
207+
(begin_offset_0_)) + \
208+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
209+
(span_string_0_).size())), \
210+
::quick_lint_js::Diagnostic_Assertion::Member::make_span( \
211+
QLJS_CPP_QUOTE_U8_SV(member_1_), offsetof(type_, member_1_), \
212+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
213+
(begin_offset_1_)), \
214+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
215+
(begin_offset_1_)) + \
216+
::quick_lint_js::narrow_cast<Padded_String_Size>( \
217+
(span_string_1_).size())), \
218+
}))
219+
186220
// See [_diag-syntax].
187221
//
188222
// Exits the program at run-time if the specification is malformed.

test/test-parse-expression-typescript.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,14 @@ TEST_F(Test_Parse_Expression_TypeScript,
417417
SCOPED_TRACE(code);
418418
Test_Parser p(code.string_view(), typescript_options, capture_diags);
419419
p.parse_and_visit_expression();
420-
EXPECT_THAT(p.legacy_errors(),
421-
ElementsAreArray({
422-
DIAG_TYPE_2_OFFSETS(
423-
p.code,
424-
Diag_TypeScript_As_Const_With_Non_Literal_Typeable, //
425-
expression, 0, expression, //
426-
as_const, expression.size() + 1, u8"as const"_sv),
427-
}));
420+
assert_diagnostics(
421+
p.code, p.errors,
422+
{
423+
DIAGNOSTIC_ASSERTION_2_SPANS(
424+
Diag_TypeScript_As_Const_With_Non_Literal_Typeable, //
425+
expression, 0, expression, //
426+
as_const, expression.size() + 1, u8"as const"_sv),
427+
});
428428
}
429429

430430
test_parse_and_visit_expression(

test/test-parse-expression.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,15 +3548,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
35483548
SCOPED_TRACE(p.code);
35493549
Expression* ast = p.parse_expression();
35503550
EXPECT_EQ(summarize(ast), "binary(unary(var a), var b)");
3551-
EXPECT_THAT(
3552-
p.legacy_errors(),
3553-
ElementsAreArray({
3554-
DIAG_TYPE_2_OFFSETS(
3555-
p.code,
3551+
assert_diagnostics(
3552+
p.code, p.errors,
3553+
{
3554+
DIAGNOSTIC_ASSERTION_2_SPANS(
35563555
Diag_Missing_Parentheses_Around_Unary_Lhs_Of_Exponent, //
35573556
unary_expression, 0, op + u8"a"s, //
35583557
exponent_operator, (op + u8"a "s).size(), u8"**"_sv),
3559-
}));
3558+
});
35603559
}
35613560

35623561
for (String8_View op : {u8"delete"s, u8"typeof"s, u8"void"s}) {
@@ -3567,15 +3566,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
35673566
// TODO(strager): Rewrite the AST into something like the following:
35683567
EXPECT_EQ(summarize(ast), "typeof(binary(var a, var b))");
35693568
}
3570-
EXPECT_THAT(
3571-
p.legacy_errors(),
3572-
ElementsAreArray({
3573-
DIAG_TYPE_2_OFFSETS(
3574-
p.code,
3569+
assert_diagnostics(
3570+
p.code, p.errors,
3571+
{
3572+
DIAGNOSTIC_ASSERTION_2_SPANS(
35753573
Diag_Missing_Parentheses_Around_Exponent_With_Unary_Lhs, //
35763574
exponent_expression, concat(op, u8" "s).size(), u8"a ** b"_sv,
35773575
unary_operator, 0, op),
3578-
}));
3576+
});
35793577
}
35803578
}
35813579

test/test-parse-typescript-class.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -984,16 +984,16 @@ TEST_F(Test_Parse_TypeScript_Class,
984984
Test_Parser p(code.string_view(), typescript_options, capture_diags);
985985
p.parse_and_visit_statement();
986986

987-
EXPECT_THAT(
988-
p.legacy_errors(),
989-
ElementsAreArray({
990-
DIAG_TYPE_2_OFFSETS(
991-
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
987+
assert_diagnostics(
988+
p.code, p.errors,
989+
{
990+
DIAGNOSTIC_ASSERTION_2_SPANS(
991+
Diag_Access_Specifier_Must_Precede_Other_Modifiers,
992992
second_modifier,
993993
concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(),
994994
access_specifier, //
995995
first_modifier, u8"class C { "_sv.size(), other_modifier),
996-
}));
996+
});
997997
}
998998
}
999999

@@ -1005,16 +1005,16 @@ TEST_F(Test_Parse_TypeScript_Class,
10051005
Test_Parser p(code.string_view(), typescript_options, capture_diags);
10061006
p.parse_and_visit_statement();
10071007

1008-
EXPECT_THAT(
1009-
p.legacy_errors(),
1010-
ElementsAreArray({
1011-
DIAG_TYPE_2_OFFSETS(
1012-
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
1008+
assert_diagnostics(
1009+
p.code, p.errors,
1010+
{
1011+
DIAGNOSTIC_ASSERTION_2_SPANS(
1012+
Diag_Access_Specifier_Must_Precede_Other_Modifiers, //
10131013
second_modifier,
10141014
concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(),
10151015
access_specifier, //
10161016
first_modifier, u8"class C { "_sv.size(), other_modifier),
1017-
}));
1017+
});
10181018
}
10191019
}
10201020
}
@@ -1860,17 +1860,17 @@ TEST_F(Test_Parse_TypeScript_Class, parameter_property_in_constructor) {
18601860
SCOPED_TRACE(p.code);
18611861
p.parse_and_visit_module();
18621862

1863-
EXPECT_THAT(
1864-
p.legacy_errors(),
1865-
ElementsAreArray({
1866-
DIAG_TYPE_2_OFFSETS(
1867-
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
1863+
assert_diagnostics(
1864+
p.code, p.errors,
1865+
{
1866+
DIAGNOSTIC_ASSERTION_2_SPANS(
1867+
Diag_Access_Specifier_Must_Precede_Other_Modifiers, //
18681868
second_modifier,
18691869
u8"class C {\n constructor(readonly "_sv.size(),
18701870
access_specifier, //
18711871
first_modifier, u8"class C {\n constructor("_sv.size(),
1872-
u8"readonly"),
1873-
}));
1872+
u8"readonly"_sv),
1873+
});
18741874
}
18751875
}
18761876

test/test-parse-typescript-declare-class.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -549,19 +549,17 @@ TEST_F(Test_Parse_TypeScript_Declare_Class,
549549
EXPECT_THAT(p.variable_declarations,
550550
ElementsAreArray(
551551
{func_param_decl(u8"field"_sv), class_decl(u8"C"_sv)}));
552-
EXPECT_THAT(
553-
p.legacy_errors(),
554-
ElementsAreArray({
555-
DIAG_TYPE_2_OFFSETS(
556-
p.code,
552+
// only keyword should report a diagnostic; 'readonly' should not have its
553+
// own diagnostic.
554+
assert_diagnostics(
555+
p.code, p.errors,
556+
{
557+
DIAGNOSTIC_ASSERTION_2_SPANS(
557558
Diag_TypeScript_Parameter_Property_Not_Allowed_In_Declare_Class, //
558559
property_keyword,
559560
u8"declare class C {\n constructor("_sv.size(), keyword, //
560561
declare_keyword, 0, u8"declare"_sv),
561-
}))
562-
<< "only '" << out_string8(keyword)
563-
<< "' should report a diagnostic; 'readonly' should not have its own "
564-
"diagnostic";
562+
});
565563
}
566564
}
567565
}

test/test-parse-var.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) {
518518
EXPECT_THAT(p.variable_declarations,
519519
ElementsAreArray(
520520
{let_init_decl(u8"x"_sv), let_noinit_decl(u8"z"_sv)}));
521-
EXPECT_THAT(
522-
p.legacy_errors(),
523-
ElementsAreArray({
524-
DIAG_TYPE_2_OFFSETS(
525-
p.code, Diag_Cannot_Update_Variable_During_Declaration, //
521+
assert_diagnostics(
522+
p.code, p.errors,
523+
{
524+
DIAGNOSTIC_ASSERTION_2_SPANS(
525+
Diag_Cannot_Update_Variable_During_Declaration, //
526526
updating_operator, u8"let x "_sv.size(),
527527
compound_assignment_operator, //
528528
declaring_token, 0, u8"let"_sv),
529-
}));
529+
});
530530
}
531531

532532
{
@@ -544,15 +544,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) {
544544
EXPECT_THAT(p.variable_declarations,
545545
ElementsAreArray(
546546
{const_init_decl(u8"x"_sv), const_init_decl(u8"y"_sv)}));
547-
EXPECT_THAT(
548-
p.legacy_errors(),
549-
ElementsAreArray({
550-
DIAG_TYPE_2_OFFSETS(
551-
p.code, Diag_Cannot_Update_Variable_During_Declaration, //
547+
assert_diagnostics(
548+
p.code, p.errors,
549+
{
550+
DIAGNOSTIC_ASSERTION_2_SPANS(
551+
Diag_Cannot_Update_Variable_During_Declaration, //
552552
updating_operator, u8"const [x, y] "_sv.size(),
553553
compound_assignment_operator, //
554554
declaring_token, 0, u8"const"_sv),
555-
}));
555+
});
556556
}
557557
}
558558

0 commit comments

Comments
 (0)