C++ Utilities  4.15.0
Useful C++ classes and routines such as argument parser, IO and conversion utilities
testutils.h
Go to the documentation of this file.
1 #ifndef TESTUTILS_H
2 #define TESTUTILS_H
3 
4 #include "../application/argumentparser.h"
5 #include "../conversion/types.h"
6 #include "../misc/traits.h"
7 
8 #include <iomanip>
9 #include <ostream>
10 #include <string>
11 
12 namespace TestUtilities {
13 
17 enum class WorkingCopyMode {
18  CreateCopy,
19  NoCopy
20 };
21 
23 public:
24  TestApplication(int argc, char **argv);
25  ~TestApplication();
26 
27  operator bool() const;
28  std::string testFilePath(const std::string &name) const;
29 #ifdef PLATFORM_UNIX
30  std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode) const;
31  std::string workingCopyPath(const std::string &name) const;
32  int execApp(const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1) const;
33 #endif
34  bool unitsSpecified() const;
35  const std::vector<const char *> &units() const;
36  static const TestApplication *instance();
37  static const char *appPath();
38 
39 private:
40  static std::string readTestfilePathFromEnv();
41  static std::string readTestfilePathFromSrcRef();
42 
45  ApplicationUtilities::Argument m_testFilesPathArg;
46  ApplicationUtilities::Argument m_applicationPathArg;
47  ApplicationUtilities::Argument m_workingDirArg;
49  std::string m_testFilesPath;
50  std::string m_fallbackTestFilesPath;
51  std::string m_workingDir;
52  bool m_valid;
53  static TestApplication *m_instance;
54 };
55 
62 inline TestApplication::operator bool() const
63 {
64  return m_valid;
65 }
66 
71 {
72  return TestApplication::m_instance;
73 }
74 
78 inline const char *TestApplication::appPath()
79 {
80  return m_instance && m_instance->m_applicationPathArg.firstValue() ? m_instance->m_applicationPathArg.firstValue() : "";
81 }
82 
87 {
88  return m_unitsArg.isPresent();
89 }
90 
95 inline const std::vector<const char *> &TestApplication::units() const
96 {
97  return m_unitsArg.values();
98 }
99 
105 inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &name)
106 {
107  return TestApplication::instance()->testFilePath(name);
108 }
109 
110 #ifdef PLATFORM_UNIX
111 
116 inline CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &name)
117 {
118  return TestApplication::instance()->workingCopyPath(name);
119 }
120 
126 inline CPP_UTILITIES_EXPORT std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode)
127 {
128  return TestApplication::instance()->workingCopyPathMode(name, mode);
129 }
130 
136 inline CPP_UTILITIES_EXPORT int execApp(const char *const *args, std::string &output, std::string &errors)
137 {
138  return TestApplication::instance()->execApp(args, output, errors);
139 }
140 
141 CPP_UTILITIES_EXPORT int execHelperApp(
142  const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
143 #endif
144 
149 template <typename T> class AsHexNumber {
150 public:
152  AsHexNumber(const T &value)
153  : value(value)
154  {
155  }
156  const T &value;
157 };
158 
162 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
163 {
164  return lhs.value == rhs.value;
165 }
166 
170 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
171 {
172  return out << '0' << 'x' << std::hex << std::setfill('0') << std::setw(2) << unsigned(value.value) << std::dec;
173 }
174 
179 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
180 {
181  return AsHexNumber<T>(value);
182 }
183 
189 template <typename T, Traits::EnableIf<std::is_integral<T>> * = nullptr> AsHexNumber<T> integralsAsHexNumber(const T &value)
190 {
191  return AsHexNumber<T>(value);
192 }
193 
199 template <typename T, Traits::DisableIf<std::is_integral<T>> * = nullptr> const T &integralsAsHexNumber(const T &value)
200 {
201  return value;
202 }
203 
212 #define TESTUTILS_ASSERT_EXEC(args) CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
213 
218 #define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString) \
219  (CPPUNIT_NS::Asserter::failIf(!(std::regex_match(actualString, std::regex(expectedRegex))), \
220  CPPUNIT_NS::Message(ConversionUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"'), \
221  "Expression: " #actualString, message), \
222  CPPUNIT_SOURCELINE()))
223 
227 template <typename Pair, Traits::EnableIf<Traits::IsSpecializationOf<Pair, std::pair>> * = nullptr>
228 inline std::ostream &operator<<(std::ostream &out, const Pair &pair)
229 {
230  return out << "key: " << pair.first << "; value: " << pair.second << '\n';
231 }
232 
236 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>> * = nullptr>
237 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
238 {
239  out << '\n';
240  std::size_t index = 0;
241  for (const auto &item : iteratable) {
242  out << std::setw(2) << index << ':' << ' ' << integralsAsHexNumber(item) << '\n';
243  ++index;
244  }
245  return out;
246 }
247 
251 namespace Literals {
256 constexpr std::size_t operator"" _st(unsigned long long size)
257 {
258  return static_cast<std::size_t>(size);
259 }
260 
265 constexpr uint64 operator"" _uint64(unsigned long long size)
266 {
267  return static_cast<uint64>(size);
268 }
269 
274 constexpr int64 operator"" _int64(unsigned long long size)
275 {
276  return static_cast<int64>(size);
277 }
278 } // namespace Literals
279 } // namespace TestUtilities
280 
281 #endif // TESTUTILS_H
std::int64_t int64
signed 64-bit integer
Definition: types.h:29
bool unitsSpecified() const
Returns whether particular units have been specified.
Definition: testutils.h:86
The TestApplication class simplifies writing test applications that require opening test files...
Definition: testutils.h:22
std::ostream & operator<<(std::ostream &out, const AsHexNumber< T > &value)
Provides the actual formatting of the output for AsHexNumber class.
Definition: testutils.h:170
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:152
std::string testFilePath(const std::string &name) const
Returns the full path of the test file with the specified name.
Definition: testutils.cpp:171
const char * firstValue() const
Returns the first parameter value of the first occurrence of the argument.
AsHexNumber< T > integralsAsHexNumber(const T &value)
Wraps a value to be printed using the hex system in the error case when asserted with cppunit (or sim...
Definition: testutils.h:189
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
static const char * appPath()
Returns the application path or an empty string if no application path has been set.
Definition: testutils.h:78
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:12
const std::vector< const char * > & values(std::size_t occurrence=0) const
Returns the parameter values for the specified occurrence of argument.
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:162
The Argument class is a wrapper for command line argument information.
const std::vector< const char * > & units() const
Returns the specified test units.
Definition: testutils.h:95
AsHexNumber< T > asHexNumber(const T &value)
Wraps a value to be printed using the hex system in the error case when asserted with cppunit (or sim...
Definition: testutils.h:179
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
Definition: testutils.h:17
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
The HelpArgument class prints help information for an argument parser when present (–help...
static const TestApplication * instance()
Returns the current TestApplication instance.
Definition: testutils.h:70
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
The AsHexNumber class allows printing values asserted with cppunit (or similar test framework) using ...
Definition: testutils.h:149
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &name)
Convenience function which returns the full path of the test file with the specified name...
Definition: testutils.h:105
The ArgumentParser class provides a means for handling command line arguments.