xref: /openbmc/telemetry/tests/src/test_ensure.cpp (revision 1cdd7e4f)
1*1cdd7e4fSSzymon Dompke #include "helpers.hpp"
2f7ea2997SKrzysztof Grobelny #include "utils/ensure.hpp"
3f7ea2997SKrzysztof Grobelny 
4f7ea2997SKrzysztof Grobelny #include <gmock/gmock.h>
5f7ea2997SKrzysztof Grobelny 
6f7ea2997SKrzysztof Grobelny using namespace testing;
7f7ea2997SKrzysztof Grobelny 
8f7ea2997SKrzysztof Grobelny class TestEnsure : public Test
9f7ea2997SKrzysztof Grobelny {
10f7ea2997SKrzysztof Grobelny   public:
11f7ea2997SKrzysztof Grobelny     utils::Ensure<std::function<void()>> makeEnsure()
12f7ea2997SKrzysztof Grobelny     {
13f7ea2997SKrzysztof Grobelny         return [this] { ++executed; };
14f7ea2997SKrzysztof Grobelny     }
15f7ea2997SKrzysztof Grobelny 
16f7ea2997SKrzysztof Grobelny     utils::Ensure<std::function<void()>> sut;
17f7ea2997SKrzysztof Grobelny     size_t executed = 0u;
18f7ea2997SKrzysztof Grobelny };
19f7ea2997SKrzysztof Grobelny 
20f7ea2997SKrzysztof Grobelny TEST_F(TestEnsure, executesCallbackOnceWhenDestroyed)
21f7ea2997SKrzysztof Grobelny {
22f7ea2997SKrzysztof Grobelny     sut = makeEnsure();
23f7ea2997SKrzysztof Grobelny     sut = nullptr;
24f7ea2997SKrzysztof Grobelny 
25f7ea2997SKrzysztof Grobelny     EXPECT_THAT(executed, Eq(1u));
26f7ea2997SKrzysztof Grobelny }
27f7ea2997SKrzysztof Grobelny 
28f7ea2997SKrzysztof Grobelny TEST_F(TestEnsure, executesCallbackOnceWhenMoved)
29f7ea2997SKrzysztof Grobelny {
30f7ea2997SKrzysztof Grobelny     sut = makeEnsure();
31f7ea2997SKrzysztof Grobelny     auto copy = std::move(sut);
32f7ea2997SKrzysztof Grobelny     copy = nullptr;
33f7ea2997SKrzysztof Grobelny 
34f7ea2997SKrzysztof Grobelny     EXPECT_THAT(executed, Eq(1u));
35f7ea2997SKrzysztof Grobelny }
36f7ea2997SKrzysztof Grobelny 
37f7ea2997SKrzysztof Grobelny TEST_F(TestEnsure, executesCallbackTwiceWhenReplaced)
38f7ea2997SKrzysztof Grobelny {
39f7ea2997SKrzysztof Grobelny     sut = makeEnsure();
40f7ea2997SKrzysztof Grobelny     sut = makeEnsure();
41f7ea2997SKrzysztof Grobelny     sut = nullptr;
42f7ea2997SKrzysztof Grobelny 
43f7ea2997SKrzysztof Grobelny     EXPECT_THAT(executed, Eq(2u));
44f7ea2997SKrzysztof Grobelny }
45f7ea2997SKrzysztof Grobelny 
46f7ea2997SKrzysztof Grobelny TEST_F(TestEnsure, executesCallbackTwiceWhenNewCallbackAssigned)
47f7ea2997SKrzysztof Grobelny {
48f7ea2997SKrzysztof Grobelny     sut = makeEnsure();
49f7ea2997SKrzysztof Grobelny     sut = [this] { executed += 10; };
50f7ea2997SKrzysztof Grobelny     sut = nullptr;
51f7ea2997SKrzysztof Grobelny 
52f7ea2997SKrzysztof Grobelny     EXPECT_THAT(executed, Eq(11u));
53f7ea2997SKrzysztof Grobelny }
54