Skip to content

Commit d847435

Browse files
Adding unit test for IncludeManager
1 parent b8312c0 commit d847435

File tree

8 files changed

+1036
-5
lines changed

8 files changed

+1036
-5
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#ifndef RUNCPP2_UNIT_TESTS_INCLUDE_MANAGER_MOCK_COMPONENTS_HPP
2+
#define RUNCPP2_UNIT_TESTS_INCLUDE_MANAGER_MOCK_COMPONENTS_HPP
3+
4+
#include "ghc/filesystem.hpp"
5+
#include "CppOverride.hpp"
6+
7+
#include <sstream>
8+
#include <type_traits>
9+
10+
extern CO_DECLARE_INSTANCE(OverrideInstance);
11+
12+
namespace ghc
13+
{
14+
namespace filesystem
15+
{
16+
CO_OVERRIDE_METHOD( OverrideInstance,
17+
bool,
18+
Mock_exists,
19+
(const path&, std::error_code&),
20+
/* no prepend */,
21+
noexcept)
22+
23+
CO_OVERRIDE_METHOD( OverrideInstance,
24+
bool,
25+
Mock_create_directories,
26+
(const path&, std::error_code&),
27+
/* no prepend */,
28+
noexcept)
29+
30+
CO_OVERRIDE_METHOD( OverrideInstance,
31+
file_time_type,
32+
Mock_last_write_time,
33+
(const path&, std::error_code&),
34+
/* no prepend */,
35+
noexcept)
36+
}
37+
}
38+
39+
namespace std
40+
{
41+
class Mock_ifstream
42+
{
43+
public:
44+
CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ifstream, const ghc::filesystem::path&)
45+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ())
46+
};
47+
48+
class Mock_ofstream
49+
{
50+
public:
51+
std::stringstream StringStream;
52+
53+
CO_OVERRIDE_MEMBER_METHOD_CTOR(OverrideInstance, Mock_ofstream, const ghc::filesystem::path&)
54+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, bool, is_open, ())
55+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, void, close, ())
56+
57+
template<typename T>
58+
friend Mock_ofstream& operator<<(Mock_ofstream&, T const&);
59+
};
60+
61+
template<typename T>
62+
class Mock_hash
63+
{
64+
static_assert(std::is_same<T, std::string>::value, "We are only mocking std string");
65+
public:
66+
CO_OVERRIDE_MEMBER_METHOD(OverrideInstance, std::size_t, operator(), (T))
67+
};
68+
69+
template<typename T>
70+
inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, T const& value)
71+
{
72+
mockStream.StringStream << value;
73+
return mockStream;
74+
}
75+
76+
inline Mock_ofstream& operator<<(Mock_ofstream& mockStream, std::ostream& (*pf)(std::ostream&))
77+
{
78+
mockStream.StringStream << pf;
79+
return mockStream;
80+
}
81+
82+
inline bool Mock_getline(Mock_ifstream& stream, std::string& line)
83+
{
84+
CO_OVERRIDE_IMPL(OverrideInstance, bool, (stream, line));
85+
return false;
86+
}
87+
}
88+
89+
#define exists Mock_exists
90+
#define create_directories Mock_create_directories
91+
#define last_write_time Mock_last_write_time
92+
#define ifstream Mock_ifstream
93+
#define ofstream Mock_ofstream
94+
#define hash Mock_hash
95+
#define getline Mock_getline
96+
97+
#if INTERNAL_RUNCPP2_UNDEF_MOCKS
98+
#undef exists
99+
#undef create_directories
100+
#undef last_write_time
101+
#undef ifstream
102+
#undef ofstream
103+
#undef hash
104+
#undef getline
105+
#endif
106+
107+
#endif

Include/runcpp2/IncludeManager.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include <unordered_map>
66
#include <vector>
77

8+
#if INTERNAL_RUNCPP2_UNIT_TESTS
9+
class IncludeManagerAccessor;
10+
#endif
11+
812
namespace runcpp2
913
{
1014
class IncludeManager
@@ -27,10 +31,14 @@ namespace runcpp2
2731
const ghc::filesystem::file_time_type& recordTime) const;
2832

2933
private:
34+
#if INTERNAL_RUNCPP2_UNIT_TESTS
35+
friend class ::IncludeManagerAccessor;
36+
#endif
37+
3038
ghc::filesystem::path GetRecordPath(const ghc::filesystem::path& sourceFile) const;
3139

3240
ghc::filesystem::path IncludeRecordDir;
3341
};
3442
}
3543

36-
#endif
44+
#endif

Src/Tests/BuildsManagerTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ int main(int argc, char** argv)
379379
ssTEST_OUTPUT_ASSERT( "CreateBuildMapping should succeed",
380380
buildsManager->CreateBuildMapping(scriptsPaths.at(2)), true);
381381
ssTEST_OUTPUT_ASSERT( "Hash script path", hashResult->LastStatusSucceed());
382-
ssLOG_DEBUG("hashResult->GetStatusCount(): " << hashResult->GetStatusCount());
382+
383+
ssTEST_OUTPUT_VALUES_WHEN_FAILED(hashResult->GetStatusCount());
383384

384385
ssTEST_OUTPUT_ASSERT( "New build mapping doesn't exist",
385386
newMappedBuildPathExistsResult->LastStatusSucceed());

Src/Tests/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ target_include_directories(BuildsManagerTest PRIVATE "${CMAKE_CURRENT_LIST_DIR}/
66
target_compile_options(BuildsManagerTest PRIVATE "${STANDARD_COMPILE_FLAGS}")
77
target_link_libraries(BuildsManagerTest PRIVATE ghc_filesystem CppOverride ssTest ssLogger)
88
target_compile_definitions(BuildsManagerTest PRIVATE INTERNAL_RUNCPP2_UNIT_TESTS=1)
9-
# set_target_properties(BuildsManagerTest PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
10-
# LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests"
11-
# RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
9+
10+
add_executable(IncludeManagerTest "${CMAKE_CURRENT_LIST_DIR}/../runcpp2/IncludeManager.cpp"
11+
"${CMAKE_CURRENT_LIST_DIR}/IncludeManagerTest.cpp")
12+
13+
target_include_directories(IncludeManagerTest PRIVATE "${CMAKE_CURRENT_LIST_DIR}/../../Include")
14+
target_compile_options(IncludeManagerTest PRIVATE "${STANDARD_COMPILE_FLAGS}")
15+
target_link_libraries(IncludeManagerTest PRIVATE ghc_filesystem CppOverride ssTest ssLogger)
16+
target_compile_definitions(IncludeManagerTest PRIVATE INTERNAL_RUNCPP2_UNIT_TESTS=1)
1217

1318
function(create_data_test TEST_NAME)
1419
add_executable("${TEST_NAME}" "${CMAKE_CURRENT_LIST_DIR}/Data/${TEST_NAME}.cpp")

0 commit comments

Comments
 (0)