|
| 1 | +#ifndef RUNCPP2_UNIT_TESTS_BUILDS_MANAGER_MOCK_COMPONENTS_HPP |
| 2 | +#define RUNCPP2_UNIT_TESTS_BUILDS_MANAGER_MOCK_COMPONENTS_HPP |
| 3 | + |
| 4 | +#include "ghc/filesystem.hpp" |
| 5 | + |
| 6 | +#include "CppOverride.hpp" |
| 7 | + |
| 8 | +#include <sstream> |
| 9 | +#include <type_traits> |
| 10 | + |
| 11 | +extern CO_DECLARE_INSTANCE(OverrideInstance); |
| 12 | + |
| 13 | +namespace ghc |
| 14 | +{ |
| 15 | + namespace filesystem |
| 16 | + { |
| 17 | + CO_OVERRIDE_METHOD( OverrideInstance, |
| 18 | + bool, |
| 19 | + Mock_exists, |
| 20 | + (const path&, std::error_code&), |
| 21 | + /* no prepend */, |
| 22 | + noexcept) |
| 23 | + |
| 24 | + CO_OVERRIDE_METHOD( OverrideInstance, |
| 25 | + bool, |
| 26 | + Mock_create_directories, |
| 27 | + (const path&, std::error_code&), |
| 28 | + /* no prepend */, |
| 29 | + noexcept) |
| 30 | + |
| 31 | + CO_OVERRIDE_METHOD( OverrideInstance, |
| 32 | + bool, |
| 33 | + Mock_remove_all, |
| 34 | + (const path&, std::error_code&), |
| 35 | + /* no prepend */, |
| 36 | + noexcept) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +namespace std |
| 41 | +{ |
| 42 | + class Mock_ifstream |
| 43 | + { |
| 44 | + public: |
| 45 | + CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ifstream, const ghc::filesystem::path&) |
| 46 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, std::string, rdbuf, ()) |
| 47 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ()) |
| 48 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, void, close, ()) |
| 49 | + }; |
| 50 | + |
| 51 | + class Mock_ofstream |
| 52 | + { |
| 53 | + public: |
| 54 | + std::stringstream StringStream; |
| 55 | + |
| 56 | + CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ofstream, const ghc::filesystem::path&) |
| 57 | + |
| 58 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ()) |
| 59 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, void, close, ()) |
| 60 | + |
| 61 | + template<typename T> |
| 62 | + friend Mock_ofstream& operator<<(Mock_ofstream&, T const&); |
| 63 | + }; |
| 64 | + |
| 65 | + template<typename T> |
| 66 | + class Mock_hash |
| 67 | + { |
| 68 | + static_assert(std::is_same<T, std::string>::value, "We are only mocking std string"); |
| 69 | + public: |
| 70 | + CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, std::size_t, operator(), (T)) |
| 71 | + }; |
| 72 | + |
| 73 | + template<typename T> |
| 74 | + inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, T const& value) |
| 75 | + { |
| 76 | + //CO_OVERRIDE_IMPL(FreeFunctionOverrideInstance, Mock_ofstream&, (mockStream, value)); |
| 77 | + mockStream.StringStream << value; |
| 78 | + return mockStream; |
| 79 | + } |
| 80 | + |
| 81 | + inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, std::ostream& (*pf)(std::ostream&)) |
| 82 | + { |
| 83 | + //NOTE: This is called when std::endl is passed to the << operator. |
| 84 | + // Unfortunately function pointer is not tested/implemented yet for CppOverride |
| 85 | + // void* will do for now |
| 86 | + //void* dummyPointer = nullptr; |
| 87 | + //CO_OVERRIDE_IMPL(FreeFunctionOverrideInstance, Mock_ofstream&, (mockStream, dummyPointer)); |
| 88 | + |
| 89 | + mockStream.StringStream << pf; |
| 90 | + return mockStream; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#define exists Mock_exists |
| 95 | +#define create_directories Mock_create_directories |
| 96 | +#define remove_all Mock_remove_all |
| 97 | +#define ifstream Mock_ifstream |
| 98 | +#define ofstream Mock_ofstream |
| 99 | +#define hash Mock_hash |
| 100 | + |
| 101 | +#if INTERNAL_RUNCPP2_UNDEF_MOCKS |
| 102 | + #undef exists |
| 103 | + #undef create_directories |
| 104 | + #undef remove_all |
| 105 | + #undef ifstream |
| 106 | + #undef ofstream |
| 107 | + #undef hash |
| 108 | +#endif |
| 109 | + |
| 110 | +#endif |
0 commit comments