Skip to content

Commit

Permalink
refactor(test): replace some uses of DIAG_TYPE with ""_diag
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Jul 25, 2023
1 parent b8998dd commit f8550e6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 92 deletions.
10 changes: 3 additions & 7 deletions test/test-parse-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,13 +976,9 @@ TEST_F(Test_Parse_Function, function_with_invalid_parameters) {
u8"x.prop"_sv,
u8"html`<strong>hello</strong>`"_sv,
}) {
Test_Parser p(concat(u8"function f("_sv, parameter_list, u8") {}"_sv),
capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_statement();
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Invalid_Parameter),
}));
test_parse_and_visit_statement(
concat(u8"function f("_sv, parameter_list, u8") {}"_sv),
u8"Diag_Invalid_Parameter"_diag);
}

{
Expand Down
17 changes: 4 additions & 13 deletions test/test-parse-jsx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,8 @@ TEST_F(Test_Parse_JSX, begin_and_end_tags_must_match) {
u8"<A></A:A>"_sv,
u8"<A:A></A>"_sv,
}) {
Test_Parser p(concat(u8"c = "_sv, jsx, u8";"_sv), jsx_options,
capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_module();
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Mismatched_JSX_Tags),
}));
test_parse_and_visit_module(concat(u8"c = "_sv, jsx, u8";"_sv),
u8"Diag_Mismatched_JSX_Tags"_diag, jsx_options);
}
}

Expand Down Expand Up @@ -447,16 +442,12 @@ TEST_F(Test_Parse_JSX, adjacent_tags_without_outer_fragment) {

// Second element should be visited like normal.
{
Test_Parser p(
Spy_Visitor p = test_parse_and_visit_module(
u8R"(c = <FirstComponent></FirstComponent> <SecondComponent>{child}</SecondComponent>;)"_sv,
jsx_options, capture_diags);
p.parse_and_visit_module();
u8"Diag_Adjacent_JSX_Without_Parent"_diag, jsx_options);
EXPECT_THAT(
p.variable_uses,
ElementsAreArray({u8"FirstComponent", u8"SecondComponent", u8"child"}));
EXPECT_THAT(p.errors, ElementsAreArray({
DIAG_TYPE(Diag_Adjacent_JSX_Without_Parent),
}));
}

// Because the second element is on its own line, ASI should kick in, and the
Expand Down
26 changes: 9 additions & 17 deletions test/test-parse-typescript-enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,11 @@ TEST_F(Test_Parse_TypeScript_Enum,
}

{
Test_Parser p(concat(decl, u8" E { A = f(), B, C, D }"_sv),
typescript_options, capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_module();
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_TypeScript_Enum_Value_Must_Be_Constant),
}))
<< "shouldn't complain about auto member following computed member";
// Shouldn't complain about auto member following computed member.
test_parse_and_visit_module(
concat(decl, u8" E { A = f(), B, C, D }"_sv),
u8"Diag_TypeScript_Enum_Value_Must_Be_Constant"_diag,
typescript_options);
}

{
Expand All @@ -414,14 +410,10 @@ TEST_F(Test_Parse_TypeScript_Enum,
}

{
Test_Parser p(concat(decl, u8" E { A = this }"_sv), typescript_options,
capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_module();
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_TypeScript_Enum_Value_Must_Be_Constant),
}));
test_parse_and_visit_module(
concat(decl, u8" E { A = this }"_sv),
u8"Diag_TypeScript_Enum_Value_Must_Be_Constant"_diag,
typescript_options);
}
}
}
Expand Down
11 changes: 4 additions & 7 deletions test/test-parse-typescript-function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ TEST_F(Test_Parse_TypeScript_Function,
TEST_F(Test_Parse_TypeScript_Function,
arrow_cannot_have_parenthesized_return_type_annotation) {
{
Test_Parser p(u8"((param): (number) => {})"_sv, typescript_options,
capture_diags);
p.parse_and_visit_statement();
test_parse_and_visit_statement(
u8"((param): (number) => {})"_sv,
u8"Diag_TypeScript_Type_Annotation_In_Expression"_diag,
typescript_options);
// FIXME(strager): The above code is illegal TypeScript.
//
// Our parser currently interprets the above code as if '(param)' is a
Expand All @@ -326,10 +327,6 @@ TEST_F(Test_Parse_TypeScript_Function,
// We should parse intelligently and instead interpret '(param)' as a
// parameter list, '(number)' as the return type and '{}' as the function's
// body, and also produce a nice diagnostic.
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_TypeScript_Type_Annotation_In_Expression),
}));
}
}

Expand Down
52 changes: 20 additions & 32 deletions test/test-parse-typescript-interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,9 +1096,12 @@ TEST_F(Test_Parse_TypeScript_Interface, generator_methods_are_not_allowed) {
SCOPED_TRACE(out_string8(method_name));

{
Test_Parser p(concat(u8"interface I { *"_sv, method_name, u8"(); }"_sv),
typescript_options, capture_diags);
p.parse_and_visit_module();
// clang-format off
Spy_Visitor p = test_parse_and_visit_module(
concat(u8"interface I { *"_sv, method_name, u8"(); }"_sv), //
/* */ u8" ^ Diag_Interface_Methods_Cannot_Be_Generators"_diag, //
typescript_options);
// clang-format on
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_declaration", // I
"visit_enter_interface_scope", //
Expand All @@ -1108,41 +1111,26 @@ TEST_F(Test_Parse_TypeScript_Interface, generator_methods_are_not_allowed) {
"visit_exit_interface_scope", //
"visit_end_of_module",
}));
assert_diagnostics(
p.code, p.errors,
{
u8" ^ Diag_Interface_Methods_Cannot_Be_Generators"_diag,
});
}

{
Test_Parser p(
concat(u8"interface I { static *"_sv, method_name, u8"(); }"_sv),
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
::testing::UnorderedElementsAreArray({
DIAG_TYPE(Diag_Interface_Properties_Cannot_Be_Static),
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Generators, //
star, u8"interface I { static "_sv.size(), u8"*"_sv),
}));
// clang-format off
test_parse_and_visit_module(
concat(u8"interface I { static *"_sv, method_name, u8"(); }"_sv), //
/* */ u8" ^ Diag_Interface_Methods_Cannot_Be_Generators"_diag, //
/* */ u8"Diag_Interface_Properties_Cannot_Be_Static"_diag, //
typescript_options);
// clang-format on
}

{
Test_Parser p(
concat(u8"interface I { async *"_sv, method_name, u8"(); }"_sv),
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
::testing::UnorderedElementsAreArray({
DIAG_TYPE(Diag_Interface_Methods_Cannot_Be_Async),
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Generators, //
star, u8"interface I { async "_sv.size(), u8"*"_sv),
}));
// clang-format off
test_parse_and_visit_module(
concat(u8"interface I { async *"_sv, method_name, u8"(); }"_sv), //
/* */ u8" ^ Diag_Interface_Methods_Cannot_Be_Generators"_diag, //
/* */ u8"Diag_Interface_Methods_Cannot_Be_Async"_diag, //
typescript_options);
// clang-format on
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions test/test-parse-typescript-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,15 +2009,10 @@ TEST_F(Test_Parse_TypeScript_Type, typeof_generic_does_not_allow_dots_after) {
}

for (String8 keyword : keywords) {
Test_Parser p(concat(u8"typeof Class<T>."_sv, keyword), typescript_options,
capture_diags);
SCOPED_TRACE(p.code);
p.parse_and_visit_typescript_type_expression();
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Dot_Not_Allowed_After_Generic_Arguments_In_Type),
}));
test_parse_and_visit_typescript_type_expression(
concat(u8"typeof Class<T>."_sv, keyword),
u8"Diag_Dot_Not_Allowed_After_Generic_Arguments_In_Type"_diag,
typescript_options);
}
}

Expand Down
9 changes: 2 additions & 7 deletions test/test-parse-warning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,8 @@ TEST_F(Test_Parse_Warning, condition_with_assignment_from_literal) {
u8"for (; x = 'hello'; ) {}"_sv,
u8"do {} while (x = 'hello');"_sv,
}) {
SCOPED_TRACE(out_string8(code));
Test_Parser p(code, capture_diags);
p.parse_and_visit_statement();
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE(Diag_Assignment_Makes_Condition_Constant),
}));
test_parse_and_visit_statement(
code, u8"Diag_Assignment_Makes_Condition_Constant"_diag);
}
}

Expand Down

0 comments on commit f8550e6

Please sign in to comment.