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