C++ Utilities  5.6.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 "../misc/traits.h"
6 
7 #include <iomanip>
8 #include <ostream>
9 #include <string>
10 
11 namespace CppUtilities {
12 
16 enum class WorkingCopyMode {
17  CreateCopy,
18  NoCopy
19 };
20 
22 public:
23  // construction/destruction
24  explicit TestApplication();
25  explicit TestApplication(int argc, const char *const *argv);
26  ~TestApplication();
27  operator bool() const;
28 
29  // helper for tests
30  std::string testFilePath(const std::string &relativeTestFilePath) const;
31  std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) const;
32  std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath,
34 #ifdef PLATFORM_UNIX
35  int execApp(const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1) const;
36 #endif
37 
38  // read-only accessors
39  const std::vector<std::string> &testFilePaths() const;
40  const std::string &workingDirectory() const;
41  const char *applicationPath();
42  bool unitsSpecified() const;
43  const std::vector<const char *> &units() const;
44  bool onlyListUnits() const;
45 
46  // static read-only accessors
47  static const TestApplication *instance();
48  static const char *appPath();
49 
50 private:
51  static std::string readTestfilePathFromEnv();
52  static std::string readTestfilePathFromSrcRef();
53 
54  ArgumentParser m_parser;
55  OperationArgument m_listArg;
56  OperationArgument m_runArg;
57  ConfigValueArgument m_testFilesPathArg;
58  ConfigValueArgument m_applicationPathArg;
59  ConfigValueArgument m_workingDirArg;
60  ConfigValueArgument m_unitsArg;
61  std::vector<std::string> m_testFilesPaths;
62  std::string m_workingDir;
63  bool m_valid;
64  static TestApplication *s_instance;
65 };
66 
73 inline TestApplication::operator bool() const
74 {
75  return m_valid;
76 }
77 
82 {
83  return TestApplication::s_instance;
84 }
85 
89 inline const char *TestApplication::appPath()
90 {
91  return s_instance ? s_instance->applicationPath() : "";
92 }
93 
97 inline const std::vector<std::string> &TestApplication::testFilePaths() const
98 {
99  return m_testFilesPaths;
100 }
101 
105 inline const std::string &TestApplication::workingDirectory() const
106 {
107  return m_workingDir;
108 }
109 
114 {
115  return m_applicationPathArg.firstValue() ? m_applicationPathArg.firstValue() : "";
116 }
117 
122 {
123  return m_unitsArg.isPresent();
124 }
125 
130 inline const std::vector<const char *> &TestApplication::units() const
131 {
132  return m_unitsArg.values();
133 }
134 
139 {
140  return m_listArg.isPresent();
141 }
142 
147 inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
148 {
149  return TestApplication::instance()->testFilePath(relativeTestFilePath);
150 }
151 
156 inline CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy)
157 {
158  return TestApplication::instance()->workingCopyPathAs(relativeTestFilePath, relativeTestFilePath, mode);
159 }
160 
166  const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy)
167 {
168  return TestApplication::instance()->workingCopyPathAs(relativeTestFilePath, relativeWorkingCopyPath, mode);
169 }
170 
171 #ifdef PLATFORM_UNIX
172 
177 inline CPP_UTILITIES_EXPORT int execApp(const char *const *args, std::string &output, std::string &errors)
178 {
179  return TestApplication::instance()->execApp(args, output, errors);
180 }
181 
182 CPP_UTILITIES_EXPORT int execHelperApp(
183  const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
184 CPP_UTILITIES_EXPORT int execHelperAppInSearchPath(
185  const char *appName, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
186 #endif // PLATFORM_UNIX
187 
192 template <typename T> class AsHexNumber {
193 public:
195  AsHexNumber(const T &value)
196  : value(value)
197  {
198  }
199  const T &value;
200 };
201 
205 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
206 {
207  return lhs.value == rhs.value;
208 }
209 
213 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
214 {
215  return out << '0' << 'x' << std::hex << std::setfill('0') << std::setw(2) << unsigned(value.value) << std::dec;
216 }
217 
222 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
223 {
224  return AsHexNumber<T>(value);
225 }
226 
232 template <typename T, Traits::EnableIf<std::is_integral<T>> * = nullptr> AsHexNumber<T> integralsAsHexNumber(const T &value)
233 {
234  return AsHexNumber<T>(value);
235 }
236 
242 template <typename T, Traits::DisableIf<std::is_integral<T>> * = nullptr> const T &integralsAsHexNumber(const T &value)
243 {
244  return value;
245 }
246 
255 #define TESTUTILS_ASSERT_EXEC(args) \
256  { \
257  const auto returnCode = execApp(args, stdout, stderr); \
258  if (returnCode != 0) { \
259  CPPUNIT_FAIL(::CppUtilities::argsToString("app failed with return code ", returnCode, "\nstdout: ", stdout, "\nstderr: ", stderr)); \
260  } \
261  }
262 
267 #define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString) \
268  (CPPUNIT_NS::Asserter::failIf(!(std::regex_match(actualString, std::regex(expectedRegex, regexFlags))), \
269  CPPUNIT_NS::Message( \
270  CppUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"'), "Expression: " #actualString, message), \
271  CPPUNIT_SOURCELINE()))
272 
277 #define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString) \
278  TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, std::regex::ECMAScript, actualString)
279 
283 template <typename Pair, CppUtilities::Traits::EnableIf<CppUtilities::Traits::IsSpecializationOf<Pair, std::pair>> * = nullptr>
284 inline std::ostream &operator<<(std::ostream &out, const Pair &pair)
285 {
286  return out << "key: " << pair.first << "; value: " << pair.second << '\n';
287 }
288 
292 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>> * = nullptr>
293 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
294 {
295  out << '\n';
296  std::size_t index = 0;
297  for (const auto &item : iteratable) {
298  out << std::setw(2) << index << ':' << ' ' << integralsAsHexNumber(item) << '\n';
299  ++index;
300  }
301  return out;
302 }
303 
307 namespace Literals {
312 constexpr std::size_t operator"" _st(unsigned long long size)
313 {
314  return static_cast<std::size_t>(size);
315 }
316 
321 constexpr std::uint64_t operator"" _uint64(unsigned long long size)
322 {
323  return static_cast<std::uint64_t>(size);
324 }
325 
330 constexpr std::int64_t operator"" _int64(unsigned long long size)
331 {
332  return static_cast<std::int64_t>(size);
333 }
334 } // namespace Literals
335 } // namespace CppUtilities
336 
337 #endif // TESTUTILS_H
CppUtilities::WorkingCopyMode::NoCopy
@ NoCopy
CppUtilities::TestApplication::applicationPath
const char * applicationPath()
Returns the application path or an empty string if no application path has been set.
Definition: testutils.h:113
CppUtilities::TestApplication::unitsSpecified
bool unitsSpecified() const
Returns whether particular units have been specified.
Definition: testutils.h:121
CppUtilities::testFilePath
CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath)
Convenience function to invoke TestApplication::testFilePath().
Definition: testutils.h:147
CppUtilities::integralsAsHexNumber
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:232
CppUtilities::OperationArgument
The OperationArgument class is an Argument where denotesOperation() is true by default.
Definition: argumentparser.h:429
CppUtilities::workingCopyPathAs
CPP_UTILITIES_EXPORT std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPathAs().
Definition: testutils.h:165
CppUtilities::TestApplication::units
const std::vector< const char * > & units() const
Returns the specified test units.
Definition: testutils.h:130
CppUtilities::TestApplication::workingDirectory
const std::string & workingDirectory() const
Returns the directory which is supposed to used for storing files created by tests.
Definition: testutils.h:105
CppUtilities::Argument::values
const std::vector< const char * > & values(std::size_t occurrence=0) const
Returns the parameter values for the specified occurrence of argument.
Definition: argumentparser.h:626
CppUtilities::TestApplication
The TestApplication class simplifies writing test applications that require opening test files.
Definition: testutils.h:21
CppUtilities::WorkingCopyMode::CreateCopy
@ CreateCopy
CppUtilities::AsHexNumber::AsHexNumber
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:195
CppUtilities::ConfigValueArgument
The ConfigValueArgument class is an Argument where setCombinable() is true by default.
Definition: argumentparser.h:434
CppUtilities::TestApplication::testFilePaths
const std::vector< std::string > & testFilePaths() const
Returns the list of directories to look for test files.
Definition: testutils.h:97
CppUtilities::operator<<
CPP_UTILITIES_EXPORT std::ostream & operator<<(std::ostream &out, Indentation indentation)
Definition: commandlineutils.h:83
CppUtilities::Argument::firstValue
const char * firstValue() const
Returns the first parameter value of the first occurrence of the argument.
Definition: argumentparser.cpp:491
CppUtilities::TestApplication::workingCopyPathAs
std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy) const
Returns the full path to a working copy of the test file with the specified relativeTestFilePath.
Definition: testutils.cpp:274
CppUtilities::TestApplication::testFilePath
std::string testFilePath(const std::string &relativeTestFilePath) const
Returns the full path of the test file with the specified relativeTestFilePath.
Definition: testutils.cpp:236
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::AsHexNumber::value
const T & value
Definition: testutils.h:199
CppUtilities::Argument::isPresent
bool isPresent() const
Returns an indication whether the argument could be detected when parsing.
Definition: argumentparser.h:738
CppUtilities::WorkingCopyMode
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
Definition: testutils.h:16
CppUtilities::TestApplication::onlyListUnits
bool onlyListUnits() const
Returns whether the test application should only list available units and not actually run any tests.
Definition: testutils.h:138
CppUtilities::AsHexNumber
The AsHexNumber class allows printing values asserted with cppunit (or similar test framework) using ...
Definition: testutils.h:192
CppUtilities::asHexNumber
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:222
CppUtilities::TestApplication::appPath
static const char * appPath()
Returns the application path or an empty string if no application path has been set.
Definition: testutils.h:89
CppUtilities::TestApplication::instance
static const TestApplication * instance()
Returns the current TestApplication instance.
Definition: testutils.h:81
CPP_UTILITIES_EXPORT
#define CPP_UTILITIES_EXPORT
Marks the symbol to be exported by the c++utilities library.
CppUtilities::operator==
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:205
CppUtilities::workingCopyPath
CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy)
Convenience function to invoke TestApplication::workingCopyPath().
Definition: testutils.h:156
CppUtilities::ArgumentParser
The ArgumentParser class provides a means for handling command line arguments.
Definition: argumentparser.h:450