Skip to content

Commit

Permalink
refactor(test): use ""_diag for more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strager committed Jul 24, 2023
1 parent 3a2b7c4 commit 788b573
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 183 deletions.
32 changes: 9 additions & 23 deletions test/test-parse-statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,31 +776,24 @@ TEST_F(Test_Parse_Statement, missing_if_after_else) {
}

{
Test_Parser p(u8"if (false) {} else () {}"_sv, capture_diags);
p.parse_and_visit_statement();
// Should not report Diag_Missing_Arrow_Operator_In_Arrow_Function.
Spy_Visitor p = test_parse_and_visit_statement(
u8"if (false) {} else () {}"_sv, //
u8" ^^ Diag_Missing_Expression_Between_Parentheses.left_paren_to_right_paren"_diag, //
u8" ` Diag_Missing_If_After_Else"_diag);
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_enter_block_scope", // if
"visit_exit_block_scope", // if
"visit_enter_block_scope", // else
"visit_exit_block_scope", // else
}));
EXPECT_THAT(
p.errors,
UnorderedElementsAreArray({
DIAG_TYPE_OFFSETS(p.code,
Diag_Missing_Expression_Between_Parentheses, //
left_paren_to_right_paren,
u8"if (false) {} else "_sv.size(), u8"()"_sv),
DIAG_TYPE_OFFSETS(p.code, Diag_Missing_If_After_Else, //
expected_if, u8"if (false) {} else"_sv.size(),
u8""_sv),
}))
<< "should not report Diag_Missing_Arrow_Operator_In_Arrow_Function";
}

{
Test_Parser p(u8"if (false) {} else (x, y) {}"_sv, capture_diags);
p.parse_and_visit_statement();
// Should not report Diag_Missing_Arrow_Operator_In_Arrow_Function.
Spy_Visitor p = test_parse_and_visit_statement(
u8"if (false) {} else (x, y) {}"_sv, //
u8" ` Diag_Missing_If_After_Else"_diag);
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_enter_block_scope", // if
"visit_exit_block_scope", // if
Expand All @@ -809,13 +802,6 @@ TEST_F(Test_Parse_Statement, missing_if_after_else) {
"visit_enter_block_scope", // else
"visit_exit_block_scope", // else
}));
EXPECT_THAT(p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Missing_If_After_Else, //
expected_if, u8"if (false) {} else"_sv.size(), u8""_sv),
}))
<< "should not report Diag_Missing_Arrow_Operator_In_Arrow_Function";
}
}

Expand Down
51 changes: 15 additions & 36 deletions test/test-parse-typescript-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,35 +243,21 @@ TEST_F(Test_Parse_TypeScript_Class,
}

{
Test_Parser p(u8"class C { field!: any = init; }"_sv, capture_diags);
p.parse_and_visit_statement();
// Should not also report
// Diag_TypeScript_Assignment_Asserted_Field_Cannot_Have_Initializer.
Spy_Visitor p = test_parse_and_visit_statement(
u8"class C { field!: any = init; }"_sv, //
u8" ^ Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_JavaScript"_diag);
EXPECT_THAT(p.property_declarations, ElementsAreArray({u8"field"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_JavaScript, //
bang, u8"class C { field"_sv.size(), u8"!"_sv),
}))
<< "should not also report "
"Diag_TypeScript_Assignment_Asserted_Field_Cannot_Have_Initializer";
}

{
Test_Parser p(u8"class C { field!; }"_sv, capture_diags);
p.parse_and_visit_statement();
// Should not also report
// Diag_TypeScript_Assignment_Asserted_Field_Must_Have_A_Type.
Spy_Visitor p = test_parse_and_visit_statement(
u8"class C { field!; }"_sv, //
u8" ^ Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_JavaScript"_diag);
EXPECT_THAT(p.property_declarations, ElementsAreArray({u8"field"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_JavaScript, //
bang, u8"class C { field"_sv.size(), u8"!"_sv),
}))
<< "should not also report "
"Diag_TypeScript_Assignment_Asserted_Field_Must_Have_A_Type";
}
}

Expand Down Expand Up @@ -1293,9 +1279,11 @@ TEST_F(Test_Parse_TypeScript_Class, implements_is_not_allowed_in_javascript) {
}

{
Test_Parser p(u8"class C implements I extends Base {}"_sv,
javascript_options, capture_diags);
p.parse_and_visit_module();
// Should not report Diag_TypeScript_Implements_Must_Be_After_Extends.
Spy_Visitor p = test_parse_and_visit_module(
u8"class C implements I extends Base {}"_sv, //
u8" ^^^^^^^^^^ Diag_TypeScript_Class_Implements_Not_Allowed_In_JavaScript"_diag, //
javascript_options);
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_enter_class_scope", // {
"visit_variable_type_use", // I
Expand All @@ -1305,15 +1293,6 @@ TEST_F(Test_Parse_TypeScript_Class, implements_is_not_allowed_in_javascript) {
"visit_variable_declaration", // C
"visit_end_of_module",
}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Class_Implements_Not_Allowed_In_JavaScript, //
implements_keyword, u8"class C "_sv.size(), u8"implements"_sv),
}))
<< "should not report Diag_TypeScript_Implements_Must_Be_After_Extends";
}
}

Expand Down
21 changes: 6 additions & 15 deletions test/test-parse-typescript-declare-class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,21 +366,12 @@ TEST_F(Test_Parse_TypeScript_Declare_Class,
typescript_options);
}

{
Test_Parser p(u8"declare class C { myField!: any = init; }"_sv,
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Declare_Class, //
bang, u8"declare class C { myField"_sv.size(), u8"!"_sv),
}))
<< "Diag_Declare_Class_Fields_Cannot_Have_Initializers should not also "
"be reported";
}
// Diag_Declare_Class_Fields_Cannot_Have_Initializers should not also be
// reported.
test_parse_and_visit_module(
u8"declare class C { myField!: any = init; }"_sv, //
u8" ^ Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Declare_Class"_diag,
typescript_options);
}

TEST_F(Test_Parse_TypeScript_Declare_Class,
Expand Down
100 changes: 29 additions & 71 deletions test/test-parse-typescript-interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,35 +451,21 @@ TEST_F(Test_Parse_TypeScript_Interface,
}

{
Test_Parser p(u8"interface I { fieldName!; }"_sv, typescript_options,
capture_diags);
p.parse_and_visit_statement();
// Missing type annotation should not report two errors.
Spy_Visitor p = test_parse_and_visit_statement(
u8"interface I { fieldName!; }"_sv, //
u8" ^ Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Interfaces"_diag, //
typescript_options);
EXPECT_THAT(p.property_declarations, ElementsAreArray({u8"fieldName"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Interfaces, //
bang, u8"interface I { fieldName"_sv.size(), u8"!"_sv),
}))
<< "missing type annotation should not report two errors";
}

{
Test_Parser p(u8"interface I { fieldName!: any = init; }"_sv,
typescript_options, capture_diags);
p.parse_and_visit_statement();
// Initializer should not report two errors.
Spy_Visitor p = test_parse_and_visit_statement(
u8"interface I { fieldName!: any = init; }"_sv, //
u8" ^ Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Interfaces"_diag, //
typescript_options);
EXPECT_THAT(p.property_declarations, ElementsAreArray({u8"fieldName"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Assignment_Asserted_Fields_Not_Allowed_In_Interfaces, //
bang, u8"interface I { fieldName"_sv.size(), u8"!"_sv),
}))
<< "initializer should not report two errors";
}
}

Expand Down Expand Up @@ -1194,59 +1180,31 @@ TEST_F(Test_Parse_TypeScript_Interface, generator_methods_are_not_allowed) {
TEST_F(Test_Parse_TypeScript_Interface,
static_async_methods_are_definitely_not_allowed) {
{
Test_Parser p(u8"interface I { static async method(); }"_sv,
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
::testing::UnorderedElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Async, //
async_keyword, u8"interface I { static "_sv.size(),
u8"async"_sv),
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Properties_Cannot_Be_Static, //
static_keyword, u8"interface I { "_sv.size(), u8"static"_sv),
}));
Spy_Visitor p = test_parse_and_visit_module(
u8"interface I { static async method(); }"_sv, //
u8" ^^^^^ Diag_Interface_Methods_Cannot_Be_Async"_diag, //
u8" ^^^^^^ Diag_Interface_Properties_Cannot_Be_Static"_diag, //

typescript_options);
}

{
Test_Parser p(u8"interface I { async static method(); }"_sv,
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
::testing::UnorderedElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Async, //
async_keyword, u8"interface I { "_sv.size(), u8"async"_sv),
DIAG_TYPE_OFFSETS(p.code,
Diag_Interface_Properties_Cannot_Be_Static, //
static_keyword,
u8"interface I { async "_sv.size(),
u8"static"_sv),
}));
Spy_Visitor p = test_parse_and_visit_module(
u8"interface I { async static method(); }"_sv, //
u8" ^^^^^^ Diag_Interface_Properties_Cannot_Be_Static"_diag, //
u8" ^^^^^ Diag_Interface_Methods_Cannot_Be_Async"_diag, //

typescript_options);
}

{
Test_Parser p(u8"interface I { async static *method(); }"_sv,
typescript_options, capture_diags);
p.parse_and_visit_module();
EXPECT_THAT(
p.errors,
::testing::UnorderedElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Async, //
async_keyword, u8"interface I { "_sv.size(), u8"async"_sv),
DIAG_TYPE_OFFSETS(
p.code, Diag_Interface_Methods_Cannot_Be_Generators, //
star, u8"interface I { async static "_sv.size(), u8"*"_sv),
DIAG_TYPE_OFFSETS(p.code,
Diag_Interface_Properties_Cannot_Be_Static, //
static_keyword,
u8"interface I { async "_sv.size(),
u8"static"_sv),
}));
Spy_Visitor p = test_parse_and_visit_module(
u8"interface I { async static *method(); }"_sv, //
u8" ^ Diag_Interface_Methods_Cannot_Be_Generators"_diag, //
u8" ^^^^^^ Diag_Interface_Properties_Cannot_Be_Static"_diag, //
u8" ^^^^^ Diag_Interface_Methods_Cannot_Be_Async"_diag, //

typescript_options);
}
}

Expand Down
55 changes: 17 additions & 38 deletions test/test-parse-typescript-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,19 +458,13 @@ TEST_F(Test_Parse_TypeScript_Type, tuple_type_optional_unnamed_element) {
}

{
Test_Parser p(u8"[A?, B?, C]"_sv, typescript_options, capture_diags);
p.parse_and_visit_typescript_type_expression();
// Diagnostic should point to the last optional '?'.
Spy_Visitor p = test_parse_and_visit_typescript_type_expression(
u8"[A?, B?, C]"_sv, //
u8" ` Diag_TypeScript_Required_Tuple_Element_After_Optional_Element.expected_question\n"_diag
u8" ^ .previous_optional_question"_diag, //
typescript_options);
EXPECT_THAT(p.variable_uses, ElementsAreArray({u8"A", u8"B", u8"C"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_2_OFFSETS(
p.code,
Diag_TypeScript_Required_Tuple_Element_After_Optional_Element,
expected_question, u8"[A?, B?, C"_sv.size(), u8""_sv,
previous_optional_question, u8"[A?, B"_sv.size(), u8"?"_sv),
}))
<< "diagnostic should point to the last optional '?'";
}

{
Expand Down Expand Up @@ -719,22 +713,17 @@ TEST_F(Test_Parse_TypeScript_Type, named_tuple_type_with_missing_name) {
}

{
Test_Parser p(u8"[: A, B]"_sv, typescript_options, capture_diags);
p.parse_and_visit_typescript_type_expression();
// Should not also report a missing name for the second element, because
// maybe the ':' was a mistake.
Spy_Visitor p = test_parse_and_visit_typescript_type_expression(
u8"[: A, B]"_sv, //
u8" ^ Diag_TypeScript_Missing_Name_In_Named_Tuple_Type"_diag, //
typescript_options);
EXPECT_THAT(p.visits, ElementsAreArray({
"visit_variable_type_use", // A
"visit_variable_type_use", // B
}));
EXPECT_THAT(p.variable_uses, ElementsAreArray({u8"A", u8"B"}));
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(p.code,
Diag_TypeScript_Missing_Name_In_Named_Tuple_Type,
colon, u8"["_sv.size(), u8":"_sv),
}))
<< "should not also report a missing name for the second element, "
"because maybe the ':' was a mistake";
}

{
Expand Down Expand Up @@ -1578,21 +1567,11 @@ TEST_F(Test_Parse_TypeScript_Type, arrow_function) {

TEST_F(Test_Parse_TypeScript_Type, no_question_in_type_expression) {
{
Test_Parser p(
u8"fs.promises.writeFile(outputPath, result).then((err: Error?) => {if (err) throw err;});"_sv,
typescript_options, capture_diags);
p.parse_and_visit_statement();
EXPECT_THAT(
p.errors,
ElementsAreArray({
DIAG_TYPE_OFFSETS(
p.code,
Diag_TypeScript_Question_In_Type_Expression_Should_Be_Void,
question,
u8"fs.promises.writeFile(outputPath, result).then((err: "
u8"Error"_sv.size(),
u8"?"_sv),
}));
Spy_Visitor p = test_parse_and_visit_statement(
u8"fs.promises.writeFile(outputPath, result).then((err: Error?) => {if (err) throw err;});"_sv, //
u8" ^ Diag_TypeScript_Question_In_Type_Expression_Should_Be_Void"_diag, //

typescript_options);
}

{
Expand Down

0 comments on commit 788b573

Please sign in to comment.