Skip to content

Commit 6f1eaef

Browse files
committed
fix(test): fix compatibility with newer googletest
googletest changed its assertion format ElementsAreMatcher to include the expected and actual element. Update our matchertests to pass if they see this new format. Implement PrintTo() for Any_Diag_Pointer to prevent googletest from printing raw bytes which are hard to test with.
1 parent f126414 commit 6f1eaef

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

test/quick-lint-js/diag-matcher.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ Variable_Kind Diag_Matcher_Arg::get_variable_kind(
7272
return *static_cast<const Variable_Kind *>(member_data);
7373
}
7474

75+
void PrintTo(const Any_Diag_Pointer &diag, std::ostream *out) {
76+
*out << diag.type;
77+
}
78+
7579
template <class State, class Field>
7680
class Diag_Fields_Matcher_Impl_Base
7781
: public testing::MatcherInterface<const Any_Diag_Pointer &> {

test/quick-lint-js/diag-matcher.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct Any_Diag_Pointer {
5555
const void *data;
5656
};
5757

58+
void PrintTo(const Any_Diag_Pointer &diag, std::ostream *out);
59+
5860
// A mix of ::testing::VariantWith, ::testing::Field, and Offsets_Matcher. These
5961
// are combined into one matcher to significantly reduce compile times.
6062
class Diag_Matcher_2 {

test/test-diagnostic-assertion.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <quick-lint-js/diagnostic-assertion.h>
1010
#include <quick-lint-js/gtest.h>
1111

12+
using ::testing::AnyOf;
13+
1214
namespace quick_lint_js {
1315
namespace {
1416
Diagnostic_Assertion parse_or_fail(
@@ -613,9 +615,14 @@ TEST_F(Test_Diagnostic_Assertion_2, match_error_type_with_1_field_message) {
613615
diags.add(Diag_Invalid_Break{
614616
.break_statement = Source_Code_Span(&code[0], &code[5]),
615617
});
616-
EXPECT_EQ(get_matcher_message(matcher, diags),
617-
"whose element #0 doesn't match, whose type (Diag_Invalid_Break) "
618-
"isn't Diag_Invalid_Continue");
618+
EXPECT_THAT(
619+
get_matcher_message(matcher, diags),
620+
AnyOf("whose element #0 (Diag_Invalid_Break) not "
621+
"Diag_Invalid_Continue{.continue_statement = 0..5}, "
622+
"whose type (Diag_Invalid_Break) isn't Diag_Invalid_Continue",
623+
// Old googletest:
624+
"whose element #0 doesn't match, "
625+
"whose type (Diag_Invalid_Break) isn't Diag_Invalid_Continue"));
619626
}
620627

621628
TEST_F(Test_Diagnostic_Assertion_2, match_offsets_of_1_field_span) {
@@ -656,9 +663,13 @@ TEST_F(Test_Diagnostic_Assertion_2, match_offsets_of_1_field_message) {
656663
diags.add(Diag_Invalid_Continue{
657664
.continue_statement = Source_Code_Span(&code[1], &code[4]),
658665
});
659-
EXPECT_EQ(get_matcher_message(matcher, diags),
660-
"whose element #0 doesn't match, whose .continue_statement (1-4) "
661-
"doesn't equal 0-5");
666+
EXPECT_THAT(get_matcher_message(matcher, diags),
667+
AnyOf("whose element #0 (Diag_Invalid_Continue) not "
668+
"Diag_Invalid_Continue{.continue_statement = 0..5}, "
669+
"whose .continue_statement (1-4) doesn't equal 0-5",
670+
// Old googletest:
671+
"whose element #0 doesn't match, "
672+
"whose .continue_statement (1-4) doesn't equal 0-5"));
662673
EXPECT_EQ(get_matcher_description(matcher),
663674
"1 diagnostic: {\n "
664675
"Diag_Invalid_Continue{.continue_statement = 0..5},\n"
@@ -672,9 +683,13 @@ TEST_F(Test_Diagnostic_Assertion_2, match_offsets_of_1_field_message) {
672683
diags.add(Diag_Invalid_Break{
673684
.break_statement = Source_Code_Span(&code[1], &code[4]),
674685
});
675-
EXPECT_EQ(get_matcher_message(matcher, diags),
676-
"whose element #0 doesn't match, whose .break_statement (1-4) "
677-
"doesn't equal 0-5");
686+
EXPECT_THAT(get_matcher_message(matcher, diags),
687+
AnyOf("whose element #0 (Diag_Invalid_Break) not "
688+
"Diag_Invalid_Break{.break_statement = 0..5}, "
689+
"whose .break_statement (1-4) doesn't equal 0-5",
690+
// Old googletest:
691+
"whose element #0 doesn't match, "
692+
"whose .break_statement (1-4) doesn't equal 0-5"));
678693
EXPECT_EQ(get_matcher_description(matcher),
679694
"1 diagnostic: {\n "
680695
"Diag_Invalid_Break{.break_statement = 0..5},\n"
@@ -718,9 +733,17 @@ TEST_F(Test_Diagnostic_Assertion_2, char8_message) {
718733
.where = Source_Code_Span(&code[0], &code[1]),
719734
.token = u8'(',
720735
});
721-
EXPECT_EQ(get_matcher_message(matcher, diags),
722-
"whose element #0 doesn't match, whose .where (0-1) equals 0-1 and "
723-
"whose .token ('(') doesn't equal ')'");
736+
EXPECT_THAT(get_matcher_message(matcher, diags),
737+
AnyOf("whose element #0 "
738+
"(Diag_Expected_Parenthesis_Around_Do_While_Condition) not "
739+
"Diag_Expected_Parenthesis_Around_Do_While_Condition"
740+
"{.where = 0..1, .token = ')'}, "
741+
"whose .where (0-1) equals 0-1 and "
742+
"whose .token ('(') doesn't equal ')'",
743+
// Old googletest:
744+
"whose element #0 doesn't match, "
745+
"whose .where (0-1) equals 0-1 and "
746+
"whose .token ('(') doesn't equal ')'"));
724747
EXPECT_EQ(get_matcher_description(matcher),
725748
"1 diagnostic: {\n"
726749
" Diag_Expected_Parenthesis_Around_Do_While_Condition"
@@ -764,10 +787,17 @@ TEST_F(Test_Diagnostic_Assertion_2, string8_view_message) {
764787
.characters = Source_Code_Span(&code[0], &code[1]),
765788
.rounded_val = u8"HELLO"_sv,
766789
});
767-
EXPECT_EQ(
790+
EXPECT_THAT(
768791
get_matcher_message(matcher, diags),
769-
"whose element #0 doesn't match, whose .characters (0-1) equals 0-1 and "
770-
"whose .rounded_val (\"HELLO\") doesn't equal \"hello\"");
792+
AnyOf("whose element #0 (Diag_Integer_Literal_Will_Lose_Precision) not "
793+
"Diag_Integer_Literal_Will_Lose_Precision"
794+
"{.characters = 0..1, .rounded_val = \"hello\"}, "
795+
"whose .characters (0-1) equals 0-1 and "
796+
"whose .rounded_val (\"HELLO\") doesn't equal \"hello\"",
797+
// Old googletest:
798+
"whose element #0 doesn't match, "
799+
"whose .characters (0-1) equals 0-1 and "
800+
"whose .rounded_val (\"HELLO\") doesn't equal \"hello\""));
771801
EXPECT_EQ(get_matcher_description(matcher),
772802
"1 diagnostic: {\n"
773803
" Diag_Integer_Literal_Will_Lose_Precision"

0 commit comments

Comments
 (0)