From 0cab1494b52b71e0ecb7d77bba3388492c417876 Mon Sep 17 00:00:00 2001 From: Eric Paniagua Date: Sat, 5 Sep 2020 01:15:39 -0700 Subject: [PATCH] Treat SystemControlReport like a container in tests. Signed-off-by: Eric Paniagua --- testing/common/SystemControlReport.cpp | 1 + testing/common/SystemControlReport.h | 2 +- testing/common/matchers.h | 26 ++++++++++++++++++++ testing/issue_840/test/simulator_test.cpp | 30 ++++++----------------- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/testing/common/SystemControlReport.cpp b/testing/common/SystemControlReport.cpp index d2e54dac..9111259f 100644 --- a/testing/common/SystemControlReport.cpp +++ b/testing/common/SystemControlReport.cpp @@ -25,6 +25,7 @@ SystemControlReport::SystemControlReport(const void* data) { const ReportData& report_data = *static_cast(data); memcpy(&report_data_, &report_data, sizeof(report_data_)); + this->push_back(report_data_.key); } uint8_t SystemControlReport::Key() const { diff --git a/testing/common/SystemControlReport.h b/testing/common/SystemControlReport.h index e9623ae9..b96c07c6 100644 --- a/testing/common/SystemControlReport.h +++ b/testing/common/SystemControlReport.h @@ -24,7 +24,7 @@ namespace kaleidoscope { namespace testing { -class SystemControlReport { +class SystemControlReport : public std::vector { public: typedef HID_SystemControlReport_Data_t ReportData; diff --git a/testing/common/matchers.h b/testing/common/matchers.h index cfaad182..f521bb35 100644 --- a/testing/common/matchers.h +++ b/testing/common/matchers.h @@ -17,6 +17,7 @@ #pragma once #include "kaleidoscope/key_defs.h" +#include "testing/common/SystemControlReport.h" // Out of order because `fix-macros.h` clears the preprocessor environment for // gtest and gmock. @@ -26,6 +27,31 @@ namespace kaleidoscope { namespace testing { +MATCHER_P(Contains, key, negation ? "does not contain" : "contains") { + return arg.Key() == key.getKeyCode(); +} + +class SystemControlReportIsNullMatcher + : public ::testing::MatcherInterface { + public: + bool MatchAndExplain(SystemControlReport report, + ::testing::MatchResultListener* /* ignored */) const override { + return report.Key() == 0; + } + + void DescribeTo(std::ostream* os) const override { + *os << "is empty"; + } + + void DescribeNegationTo(std::ostream* os) const override { + *os << "is not empty"; + } +}; + +::testing::Matcher IsNull() { + return ::testing::MakeMatcher(new SystemControlReportIsNullMatcher); +} + auto Contains(Key key) { return ::testing::Contains(key.getKeyCode()); } } // namespace testing diff --git a/testing/issue_840/test/simulator_test.cpp b/testing/issue_840/test/simulator_test.cpp index 0820bccd..b479b5e3 100644 --- a/testing/issue_840/test/simulator_test.cpp +++ b/testing/issue_840/test/simulator_test.cpp @@ -42,30 +42,24 @@ TEST_F(Issue840, HasNotRegressed) { auto state = RunCycle(); ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_EQ( - state->SystemControlReports(0).Key(), - System_PowerDown.getKeyCode()); + EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown)); sim_.Press(3, 5); // Press System_Sleep state = RunCycle(); ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_EQ( - state->SystemControlReports(0).Key(), - System_Sleep.getKeyCode()); + EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep)); sim_.Release(2, 1); // Release System_PowerDown state = RunCycle(); - ASSERT_EQ(state->SystemControlReports().size(), 0); + EXPECT_EQ(state->SystemControlReports().size(), 0); sim_.Release(3, 5); // Release System_Sleep state = RunCycle(); ASSERT_EQ(state->SystemControlReports().size(), 1); - EXPECT_EQ( - state->SystemControlReports(0).Key(), - 0 /* null report */); + EXPECT_THAT(state->SystemControlReports(0), IsNull()); state = RunCycle(); @@ -77,33 +71,25 @@ TEST_F(Issue840, HasNotRegressed) { // auto state = RunCycle(); // // ASSERT_EQ(state->SystemControlReports().size(), 1); -// EXPECT_EQ( -// state->SystemControlReports(0).Key(), -// System_PowerDown.getKeyCode()); +// EXPECT_THAT(state->SystemControlReports(0), Contains(System_PowerDown)); // // sim_.Press(3, 5); // Press System_Sleep // state = RunCycle(); // // ASSERT_EQ(state->SystemControlReports().size(), 1); -// EXPECT_EQ( -// state->SystemControlReports(0).Key(), -// System_Sleep.getKeyCode()); +// EXPECT_THAT(state->SystemControlReports(0), Contains(System_Sleep)); // // sim_.Release(2, 1); // Release System_PowerDown // state = RunCycle(); // // ASSERT_EQ(state->SystemControlReports().size(), 1); -// EXPECT_EQ( -// state->SystemControlReports(0).Key(), -// 0 /* null report */); +// EXPECT_THAT(state->SystemControlReports(0), IsNull()); // // sim_.Release(3, 5); // Release System_Sleep // state = RunCycle(); // // ASSERT_EQ(state->SystemControlReports().size(), 1); -// EXPECT_EQ( -// state->SystemControlReports(0).Key(), -// 0 /* null report */); +// EXPECT_THAT(state->SystemControlReports(0), IsNull()); // // state = RunCycle(); //