|
| 1 | +#include "infra/util/LogAndAbort.hpp" |
| 2 | +#include "services/ble/StateGuard.hpp" |
| 3 | +#include "gmock/gmock.h" |
| 4 | +#include <cstdarg> |
| 5 | +#include <cstdio> |
| 6 | + |
| 7 | +namespace services |
| 8 | +{ |
| 9 | + namespace |
| 10 | + { |
| 11 | + class StateGuardMock |
| 12 | + : public StateGuard |
| 13 | + { |
| 14 | + public: |
| 15 | + MOCK_METHOD(GapState, DetermineCurrentState, (), (const, override)); |
| 16 | + }; |
| 17 | + |
| 18 | + class TestStateGuard |
| 19 | + : public testing::Test |
| 20 | + { |
| 21 | + public: |
| 22 | + ~TestStateGuard() |
| 23 | + { |
| 24 | + infra::RegisterLogAndAbortHook(nullptr); |
| 25 | + } |
| 26 | + |
| 27 | + void RegisterStderrHook() |
| 28 | + { |
| 29 | + infra::RegisterLogAndAbortHook([]([[maybe_unused]] const char* reason, [[maybe_unused]] const char* file, [[maybe_unused]] int line, const char* format, va_list* args) |
| 30 | + { |
| 31 | + std::vfprintf(stderr, format, *args); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + testing::NiceMock<StateGuardMock> stateGuard; |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + TEST_F(TestStateGuard, state_is_returns_true_when_current_state_matches) |
| 40 | + { |
| 41 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::standby)); |
| 42 | + |
| 43 | + EXPECT_THAT(stateGuard.StateIs({ GapState::standby }), testing::IsTrue()); |
| 44 | + } |
| 45 | + |
| 46 | + TEST_F(TestStateGuard, state_is_returns_true_when_current_state_matches_one_of_many) |
| 47 | + { |
| 48 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::connected)); |
| 49 | + |
| 50 | + EXPECT_THAT(stateGuard.StateIs({ GapState::standby, GapState::connected }), testing::IsTrue()); |
| 51 | + } |
| 52 | + |
| 53 | + TEST_F(TestStateGuard, state_is_returns_false_when_current_state_does_not_match) |
| 54 | + { |
| 55 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::advertising)); |
| 56 | + |
| 57 | + EXPECT_THAT(stateGuard.StateIs({ GapState::standby, GapState::connected }), testing::IsFalse()); |
| 58 | + } |
| 59 | + |
| 60 | + TEST_F(TestStateGuard, assert_state_is_does_not_abort_when_state_matches) |
| 61 | + { |
| 62 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::standby)); |
| 63 | + |
| 64 | + stateGuard.AssertStateIs({ GapState::standby, GapState::connected }); |
| 65 | + } |
| 66 | + |
| 67 | +#ifndef EMIL_MUTATION_TESTING |
| 68 | + TEST_F(TestStateGuard, assert_state_is_aborts_with_message_for_single_expected_state) |
| 69 | + { |
| 70 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::advertising)); |
| 71 | + RegisterStderrHook(); |
| 72 | + |
| 73 | + EXPECT_DEATH(stateGuard.AssertStateIs({ GapState::standby }), "Unexpected state found: 3, expected: \\[0\\]"); |
| 74 | + } |
| 75 | + |
| 76 | + TEST_F(TestStateGuard, assert_state_is_aborts_with_message_for_multiple_expected_states) |
| 77 | + { |
| 78 | + ON_CALL(stateGuard, DetermineCurrentState()).WillByDefault(testing::Return(GapState::initiating)); |
| 79 | + RegisterStderrHook(); |
| 80 | + |
| 81 | + EXPECT_DEATH(stateGuard.AssertStateIs({ GapState::standby, GapState::connected }), "Unexpected state found: 4, expected: \\[0,2\\]"); |
| 82 | + } |
| 83 | +#endif |
| 84 | +} |
0 commit comments