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