Skip to content

Commit 85d91ed

Browse files
Merge pull request #61 from Neko-Box-Coder/MockFix
Using custom namespace for mocking std instead of directly inserting to std namespace
2 parents 8110746 + b9c3a69 commit 85d91ed

File tree

7 files changed

+184
-74
lines changed

7 files changed

+184
-74
lines changed

Include/Tests/BuildsManager/MockComponents.hpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#include <sstream>
1212
#include <type_traits>
13+
#include <string>
14+
#include <unordered_map>
1315

1416
extern CO_DECLARE_INSTANCE(OverrideInstance);
1517

@@ -20,27 +22,27 @@ namespace ghc
2022
CO_INSERT_METHOD( OverrideInstance,
2123
bool,
2224
Mock_exists,
23-
(const path&, std::error_code&),
25+
(const ghc::filesystem::path&, std::error_code&),
2426
/* no prepend */,
2527
noexcept)
2628

2729
CO_INSERT_METHOD( OverrideInstance,
2830
bool,
2931
Mock_create_directories,
30-
(const path&, std::error_code&),
32+
(const ghc::filesystem::path&, std::error_code&),
3133
/* no prepend */,
3234
noexcept)
3335

3436
CO_INSERT_METHOD( OverrideInstance,
3537
bool,
3638
Mock_remove_all,
37-
(const path&, std::error_code&),
39+
(const ghc::filesystem::path&, std::error_code&),
3840
/* no prepend */,
3941
noexcept)
4042
}
4143
}
4244

43-
namespace std
45+
namespace Mock_std
4446
{
4547
class Mock_ifstream
4648
{
@@ -92,6 +94,36 @@ namespace std
9294
mockStream.StringStream << pf;
9395
return mockStream;
9496
}
97+
98+
CO_FORWARD_TYPE(std, string);
99+
CO_FORWARD_TYPE(std, stringstream);
100+
CO_FORWARD_TYPE(std, istringstream);
101+
CO_FORWARD_TYPE(std, error_code);
102+
CO_FORWARD_TYPE(std, size_t);
103+
CO_FORWARD_TEMPLATE_TYPE(std, unordered_map);
104+
105+
namespace
106+
{
107+
auto& cout = std::cout;
108+
}
109+
110+
template< class CharT, class Traits >
111+
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os )
112+
{
113+
return std::endl(os);
114+
}
115+
116+
template<typename T>
117+
inline std::string to_string(T val)
118+
{
119+
return std::to_string(val);
120+
}
121+
122+
template<typename T>
123+
std::istream& getline(std::istream& is, T& val)
124+
{
125+
return std::getline(is, val);
126+
}
95127
}
96128

97129
#define exists Mock_exists
@@ -100,6 +132,7 @@ namespace std
100132
#define ifstream Mock_ifstream
101133
#define ofstream Mock_ofstream
102134
#define hash Mock_hash
135+
#define std Mock_std
103136

104137
#if INTERNAL_RUNCPP2_UNDEF_MOCKS
105138
#undef exists
@@ -108,6 +141,7 @@ namespace std
108141
#undef ifstream
109142
#undef ofstream
110143
#undef hash
144+
#undef std
111145
#endif
112146

113147
#endif

Include/Tests/ConfigParsing/MockComponents.hpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <sstream>
1111
#include <type_traits>
12+
#include <vector>
1213

1314
extern CO_DECLARE_INSTANCE(OverrideInstance);
1415

@@ -32,7 +33,7 @@ namespace ghc
3233
}
3334
}
3435

35-
namespace std
36+
namespace Mock_std
3637
{
3738
class Mock_ifstream
3839
{
@@ -47,16 +48,52 @@ namespace std
4748
(Mock_ifstream&, T const&),
4849
template<typename T> friend)
4950
};
51+
52+
CO_FORWARD_TEMPLATE_TYPE(std, vector);
53+
CO_FORWARD_TYPE(std, stringstream);
54+
CO_FORWARD_TYPE(std, string);
55+
CO_FORWARD_TEMPLATE_TYPE(std, stack);
56+
CO_FORWARD_TYPE(std, error_code);
57+
CO_FORWARD_TYPE(std, exception);
58+
CO_FORWARD_TEMPLATE_TYPE(std, unordered_map);
59+
CO_FORWARD_TYPE(std, ofstream);
60+
CO_FORWARD_TYPE(std, ios);
61+
CO_FORWARD_TYPE(std, ios_base);
62+
63+
template<typename T>
64+
inline std::string to_string(T val)
65+
{
66+
return std::to_string(val);
67+
}
68+
69+
namespace
70+
{
71+
auto& cout = std::cout;
72+
}
73+
74+
template< class CharT, class Traits >
75+
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os )
76+
{
77+
return std::endl(os);
78+
}
79+
80+
inline int stoi(const std::string& str, std::size_t* pos = nullptr, int base = 10)
81+
{
82+
return std::stoi(str, pos, base);
83+
}
5084
}
5185

86+
5287
#define exists Mock_exists
5388
#define is_directory Mock_is_directory
5489
#define ifstream Mock_ifstream
90+
#define std Mock_std
5591

5692
#if INTERNAL_RUNCPP2_UNDEF_MOCKS
5793
#undef exists
5894
#undef is_directory
5995
#undef ifstream
96+
#undef std
6097
#endif
6198

6299
#endif

Include/Tests/IncludeManager/MockComponents.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace ghc
3939
}
4040
}
4141

42-
namespace std
42+
namespace Mock_std
4343
{
4444
class Mock_ifstream
4545
{
@@ -87,6 +87,30 @@ namespace std
8787
CO_INSERT_IMPL(OverrideInstance, bool, (stream, line));
8888
return false;
8989
}
90+
91+
CO_FORWARD_TYPE(std, string);
92+
CO_FORWARD_TYPE(std, stringstream);
93+
CO_FORWARD_TYPE(std, error_code);
94+
CO_FORWARD_TYPE(std, exception);
95+
CO_FORWARD_TYPE(std, size_t);
96+
CO_FORWARD_TEMPLATE_TYPE(std, vector);
97+
98+
namespace
99+
{
100+
auto& cout = std::cout;
101+
}
102+
103+
template< class CharT, class Traits >
104+
std::basic_ostream<CharT, Traits>& endl( std::basic_ostream<CharT, Traits>& os )
105+
{
106+
return std::endl(os);
107+
}
108+
109+
template<typename T>
110+
inline std::string to_string(T val)
111+
{
112+
return std::to_string(val);
113+
}
90114
}
91115

92116
#define exists Mock_exists
@@ -96,6 +120,7 @@ namespace std
96120
#define ofstream Mock_ofstream
97121
#define hash Mock_hash
98122
#define getline Mock_getline
123+
#define std Mock_std
99124

100125
#if INTERNAL_RUNCPP2_UNDEF_MOCKS
101126
#undef exists
@@ -105,6 +130,7 @@ namespace std
105130
#undef ofstream
106131
#undef hash
107132
#undef getline
133+
#undef std
108134
#endif
109135

110136
#endif

0 commit comments

Comments
 (0)