Skip to content

Commit

Permalink
refactor(test): replace DIAG_TYPE_2_OFFSETS -> DIAGNOSTIC_ASSERTION_2…
Browse files Browse the repository at this point in the history
…_SPANS

#1154
  • Loading branch information
strager committed Jan 8, 2024
1 parent 8fb50e8 commit daf3863
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 60 deletions.
44 changes: 44 additions & 0 deletions test/quick-lint-js/diagnostic-assertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ struct Diagnostic_Assertion {
// Create a Diagnostic_Assertion which matches 'type_'. It asserts that
// 'type_::member_' is a Source_Code_Span beginning at 'begin_offset_' and
// ending at 'begin_offset_ + span_string_.size()'.
//
// If you need to match two fields of the diagnostic type, see
// DIAGNOSTIC_ASSERTION_2_SPANS.
#define DIAGNOSTIC_ASSERTION_SPAN(type_, member_, begin_offset_, span_string_) \
(::quick_lint_js::Diagnostic_Assertion::make_raw( \
Diag_Type::type_, \
Expand All @@ -176,6 +179,47 @@ struct Diagnostic_Assertion {
}, \
}))

// Create a Diagnostic_Assertion which matches 'type_'.
//
// It asserts that 'type_::member_0_' is a Source_Code_Span beginning at
// 'begin_offset_0_' and ending at 'begin_offset_0_ + span_string_0_.size()'.
//
// It asserts that 'type_::member_1_' is a Source_Code_Span beginning at
// 'begin_offset_1_' and ending at 'begin_offset_1_ + span_string_1_.size()'.
#define DIAGNOSTIC_ASSERTION_2_SPANS(type_, member_0_, begin_offset_0_, \
span_string_0_, member_1_, \
begin_offset_1_, span_string_1_) \
(::quick_lint_js::Diagnostic_Assertion::make_raw( \
Diag_Type::type_, \
{ \
::quick_lint_js::Diagnostic_Assertion::Member{ \
.name = QLJS_CPP_QUOTE_U8_SV(member_0_), \
.offset = offsetof(type_, member_0_), \
.type = Diagnostic_Arg_Type::source_code_span, \
.span_begin_offset = \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(begin_offset_0_)), \
.span_end_offset = \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(begin_offset_0_)) + \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(span_string_0_).size()), \
}, \
::quick_lint_js::Diagnostic_Assertion::Member{ \
.name = QLJS_CPP_QUOTE_U8_SV(member_1_), \
.offset = offsetof(type_, member_1_), \
.type = Diagnostic_Arg_Type::source_code_span, \
.span_begin_offset = \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(begin_offset_1_)), \
.span_end_offset = \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(begin_offset_1_)) + \
::quick_lint_js::narrow_cast<Padded_String_Size>( \
(span_string_1_).size()), \
}, \
}))

// See [_diag-syntax].
//
// Exits the program at run-time if the specification is malformed.
Expand Down
16 changes: 8 additions & 8 deletions test/test-parse-expression-typescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,14 @@ TEST_F(Test_Parse_Expression_TypeScript,
SCOPED_TRACE(code);
Test_Parser p(code.string_view(), typescript_options, capture_diags);
p.parse_and_visit_expression();
EXPECT_THAT(p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
Diag_TypeScript_As_Const_With_Non_Literal_Typeable, //
expression, 0, expression, //
as_const, expression.size() + 1, u8"as const"_sv),
}));
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_TypeScript_As_Const_With_Non_Literal_Typeable, //
expression, 0, expression, //
as_const, expression.size() + 1, u8"as const"_sv),
});
}

test_parse_and_visit_expression(
Expand Down
22 changes: 10 additions & 12 deletions test/test-parse-expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3548,15 +3548,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
SCOPED_TRACE(p.code);
Expression* ast = p.parse_expression();
EXPECT_EQ(summarize(ast), "binary(unary(var a), var b)");
EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Missing_Parentheses_Around_Unary_Lhs_Of_Exponent, //
unary_expression, 0, op + u8"a"s, //
exponent_operator, (op + u8"a "s).size(), u8"**"_sv),
}));
});
}

for (String8_View op : {u8"delete"s, u8"typeof"s, u8"void"s}) {
Expand All @@ -3567,15 +3566,14 @@ TEST_F(Test_Parse_Expression, unary_cannot_mix_with_star_star) {
// TODO(strager): Rewrite the AST into something like the following:
EXPECT_EQ(summarize(ast), "typeof(binary(var a, var b))");
}
EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Missing_Parentheses_Around_Exponent_With_Unary_Lhs, //
exponent_expression, concat(op, u8" "s).size(), u8"a ** b"_sv,
unary_operator, 0, op),
}));
});
}
}

Expand Down
38 changes: 19 additions & 19 deletions test/test-parse-typescript-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,16 +984,16 @@ TEST_F(Test_Parse_TypeScript_Class,
Test_Parser p(code.string_view(), typescript_options, capture_diags);
p.parse_and_visit_statement();

EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Access_Specifier_Must_Precede_Other_Modifiers,
second_modifier,
concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(),
access_specifier, //
first_modifier, u8"class C { "_sv.size(), other_modifier),
}));
});
}
}

Expand All @@ -1005,16 +1005,16 @@ TEST_F(Test_Parse_TypeScript_Class,
Test_Parser p(code.string_view(), typescript_options, capture_diags);
p.parse_and_visit_statement();

EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Access_Specifier_Must_Precede_Other_Modifiers, //
second_modifier,
concat(u8"class C { "_sv, other_modifier, u8" "_sv).size(),
access_specifier, //
first_modifier, u8"class C { "_sv.size(), other_modifier),
}));
});
}
}
}
Expand Down Expand Up @@ -1860,17 +1860,17 @@ TEST_F(Test_Parse_TypeScript_Class, parameter_property_in_constructor) {
SCOPED_TRACE(p.code);
p.parse_and_visit_module();

EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code, Diag_Access_Specifier_Must_Precede_Other_Modifiers,
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Access_Specifier_Must_Precede_Other_Modifiers, //
second_modifier,
u8"class C {\n constructor(readonly "_sv.size(),
access_specifier, //
first_modifier, u8"class C {\n constructor("_sv.size(),
u8"readonly"),
}));
u8"readonly"_sv),
});
}
}

Expand Down
16 changes: 7 additions & 9 deletions test/test-parse-typescript-declare-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,19 +549,17 @@ TEST_F(Test_Parse_TypeScript_Declare_Class,
EXPECT_THAT(p.variable_declarations,
ElementsAreArray(
{func_param_decl(u8"field"_sv), class_decl(u8"C"_sv)}));
EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
// only keyword should report a diagnostic; 'readonly' should not have its
// own diagnostic.
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_TypeScript_Parameter_Property_Not_Allowed_In_Declare_Class, //
property_keyword,
u8"declare class C {\n constructor("_sv.size(), keyword, //
declare_keyword, 0, u8"declare"_sv),
}))
<< "only '" << out_string8(keyword)
<< "' should report a diagnostic; 'readonly' should not have its own "
"diagnostic";
});
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions test/test-parse-var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) {
EXPECT_THAT(p.variable_declarations,
ElementsAreArray(
{let_init_decl(u8"x"_sv), let_noinit_decl(u8"z"_sv)}));
EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code, Diag_Cannot_Update_Variable_During_Declaration, //
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Cannot_Update_Variable_During_Declaration, //
updating_operator, u8"let x "_sv.size(),
compound_assignment_operator, //
declaring_token, 0, u8"let"_sv),
}));
});
}

{
Expand All @@ -544,15 +544,15 @@ TEST_F(Test_Parse_Var, parse_invalid_let) {
EXPECT_THAT(p.variable_declarations,
ElementsAreArray(
{const_init_decl(u8"x"_sv), const_init_decl(u8"y"_sv)}));
EXPECT_THAT(
p.legacy_errors(),
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code, Diag_Cannot_Update_Variable_During_Declaration, //
assert_diagnostics(
p.code, p.errors,
{
DIAGNOSTIC_ASSERTION_2_SPANS(
Diag_Cannot_Update_Variable_During_Declaration, //
updating_operator, u8"const [x, y] "_sv.size(),
compound_assignment_operator, //
declaring_token, 0, u8"const"_sv),
}));
});
}
}

Expand Down

0 comments on commit daf3863

Please sign in to comment.