95 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
		
		
			
		
	
	
			95 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
								 | 
							
								#pragma once
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace lt::test {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								template<typename _Signature>
							 | 
						||
| 
								 | 
							
								class Mock;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								template<typename Return_Type, typename... Arg_Types>
							 | 
						||
| 
								 | 
							
								class Mock<Return_Type(Arg_Types...)>
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								public:
							 | 
						||
| 
								 | 
							
									auto at_least() -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return *this;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									auto operator&&(Mock &mock) -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										return mock;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									auto operator()(Arg_Types... arguments) -> Return_Type
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										++m_call_index;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										for (auto &side_effect : m_side_effects)
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											side_effect(std::forward<Arg_Types>(arguments)...);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										if (m_return_func)
							 | 
						||
| 
								 | 
							
										{
							 | 
						||
| 
								 | 
							
											return m_return_func(std::forward<Arg_Types>(arguments)...);
							 | 
						||
| 
								 | 
							
										}
							 | 
						||
| 
								 | 
							
										return m_return_value;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/** With any arguments. */
							 | 
						||
| 
								 | 
							
									template<uint32_t counter = 1>
							 | 
						||
| 
								 | 
							
									auto expect() -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										m_expected_counter = counter;
							 | 
						||
| 
								 | 
							
										return *this;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									auto apply(std::function<void(Arg_Types...)> side_effect) -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										m_side_effects.emplace_back(std::move(side_effect));
							 | 
						||
| 
								 | 
							
										return *this;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/** Returns a fixed value. */
							 | 
						||
| 
								 | 
							
									auto returns(Return_Type value) -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										m_return_value = value;
							 | 
						||
| 
								 | 
							
										return *this;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									/** Returns a value based on input. */
							 | 
						||
| 
								 | 
							
									auto returns(std::function<Return_Type(Arg_Types...)> func) -> Mock &
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										m_return_func = std::move(func);
							 | 
						||
| 
								 | 
							
										return *this;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								private:
							 | 
						||
| 
								 | 
							
									Return_Type m_return_value {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									std::function<Return_Type(Arg_Types...)> m_return_func {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									std::vector<std::function<void(Arg_Types...)>> m_side_effects {};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									uint32_t m_call_index = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									std::vector<std::pair<std::tuple<Arg_Types...>, uint32_t>> m_expected_args;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									uint32_t m_expected_counter {};
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace mock::range {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[[nodiscard]] auto is_empty() -> bool
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									return false;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}; // namespace mock::range
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								[[nodiscard]] auto eq(auto rhs) -> bool
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
									return false;
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								} // namespace lt::test
							 |