forked from d99kris/rapidcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittest.h
168 lines (151 loc) · 9.01 KB
/
unittest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#pragma once
#ifndef _MSC_VER
#include <unistd.h>
#else
#include <io.h>
#endif
#include <fstream>
#include <string>
#include <sstream>
#define ExpectEqual(t, a, b) ExpectEqualFun<t>(a, b, #a, #b, __FILE__, __LINE__)
#define ExpectTrue(a) ExpectTrueFun(a, #a, __FILE__, __LINE__)
#define ExpectException(expr, excp) \
do \
{ \
bool success = false; \
try \
{ \
expr; \
} \
catch (const excp &) \
{ \
success = true; \
} \
catch (const std::exception& ex) \
{ \
std::stringstream ss; \
ss << unittest::detail::FileName(__FILE__) << ":" << std::to_string(__LINE__); \
ss << " ExpectException failed: unexpected exception '" << typeid(ex).name(); \
ss << "' thrown." << std::endl; \
throw std::runtime_error(ss.str()); \
} \
\
if (!success) \
{ \
std::stringstream ss; \
ss << unittest::detail::FileName(__FILE__) << ":" << std::to_string(__LINE__); \
ss << " ExpectException failed: expected exception '" << #excp << "' not thrown."; \
ss << std::endl; \
throw std::runtime_error(ss.str()); \
} \
} \
while (0)
#define ExpectExceptionMsg(expr, excp, msg) \
do \
{ \
bool success = false; \
try \
{ \
expr; \
} \
catch (const excp& ex) \
{ \
if (std::string(ex.what()) == msg) \
{ \
success = true; \
} \
else \
{ \
std::stringstream ss; \
ss << unittest::detail::FileName(__FILE__) << ":" << std::to_string(__LINE__); \
ss << " ExpectExceptionMsg failed: unexpected exception message '" << ex.what(); \
ss << "'." << std::endl; \
throw std::runtime_error(ss.str()); \
} \
} \
catch (const std::exception& ex) \
{ \
std::stringstream ss; \
ss << unittest::detail::FileName(__FILE__) << ":" << std::to_string(__LINE__); \
ss << " ExpectExceptionMsg failed: unexpected exception '" << typeid(ex).name(); \
ss << "' thrown." << std::endl; \
throw std::runtime_error(ss.str()); \
} \
\
if (!success) \
{ \
std::stringstream ss; \
ss << unittest::detail::FileName(__FILE__) << ":" << std::to_string(__LINE__); \
ss << " ExpectException failed: expected exception '" << #excp << "' not thrown."; \
ss << std::endl; \
throw std::runtime_error(ss.str()); \
} \
} \
while (0)
namespace unittest
{
namespace detail
{
inline std::string FileName(const std::string& pPath)
{
const std::size_t slash = pPath.rfind("/");
std::string name = (slash != std::string::npos) ? pPath.substr(slash + 1) : pPath;
return name;
}
}
inline std::string TempPath()
{
char name[] = "rapidcsvtest.XX" "XX" "XX";
#ifndef _MSC_VER
int fd = mkstemp(name);
close(fd);
#else
_mktemp_s(name, strlen(name) + 1);
#endif
return std::string(name);
}
inline void WriteFile(const std::string& pPath, const std::string& pData)
{
std::ofstream outfile;
outfile.open(pPath, std::ifstream::out | std::ifstream::binary);
outfile << pData;
outfile.close();
}
inline std::string ReadFile(const std::string& pPath)
{
std::ifstream infile;
infile.open(pPath, std::ifstream::in | std::ifstream::binary);
std::string data((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
infile.close();
return data;
}
inline void DeleteFile(const std::string& pPath)
{
std::remove(pPath.c_str());
}
template<typename T>
inline void ExpectEqualFun(T pTest, T pRef, const std::string& testName,
const std::string& refName, const std::string& filePath, int lineNo)
{
if (pTest != pRef)
{
std::stringstream ss;
ss << detail::FileName(filePath) << ":" << std::to_string(lineNo);
ss << " ExpectEqual failed: " << testName << " != " << refName << std::endl;
ss << testName << " = '" << pTest << "'" << std::endl;
ss << refName << " = '" << pRef << "'" << std::endl;
throw std::runtime_error(ss.str());
}
}
inline void ExpectTrueFun(bool pTest, const std::string& testName, const std::string& filePath,
int lineNo)
{
if (!pTest)
{
std::stringstream ss;
ss << detail::FileName(filePath) << ":" << std::to_string(lineNo);
ss << " ExpectTrue failed: " << testName << " == false" << std::endl;
throw std::runtime_error(ss.str());
}
}
}