C++ Utilities  5.0.1
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 #endif // PLATFORM_UNIX
185 
190 template <typename T> class AsHexNumber {
191 public:
193  AsHexNumber(const T &value)
194  : value(value)
195  {
196  }
197  const T &value;
198 };
199 
203 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
204 {
205  return lhs.value == rhs.value;
206 }
207 
211 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
212 {
213  return out << '0' << 'x' << std::hex << std::setfill('0') << std::setw(2) << unsigned(value.value) << std::dec;
214 }
215 
220 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
221 {
222  return AsHexNumber<T>(value);
223 }
224 
230 template <typename T, Traits::EnableIf<std::is_integral<T>> * = nullptr> AsHexNumber<T> integralsAsHexNumber(const T &value)
231 {
232  return AsHexNumber<T>(value);
233 }
234 
240 template <typename T, Traits::DisableIf<std::is_integral<T>> * = nullptr> const T &integralsAsHexNumber(const T &value)
241 {
242  return value;
243 }
244 
253 #define TESTUTILS_ASSERT_EXEC(args) \
254  { \
255  const auto returnCode = execApp(args, stdout, stderr); \
256  if (returnCode != 0) { \
257  CPPUNIT_FAIL(::CppUtilities::argsToString("app failed with return code ", returnCode, "\nstdout: ", stdout, "\nstderr: ", stderr)); \
258  } \
259  }
260 
265 #define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString) \
266  (CPPUNIT_NS::Asserter::failIf(!(std::regex_match(actualString, std::regex(expectedRegex))), \
267  CPPUNIT_NS::Message( \
268  CppUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"'), "Expression: " #actualString, message), \
269  CPPUNIT_SOURCELINE()))
270 
274 template <typename Pair, CppUtilities::Traits::EnableIf<CppUtilities::Traits::IsSpecializationOf<Pair, std::pair>> * = nullptr>
275 inline std::ostream &operator<<(std::ostream &out, const Pair &pair)
276 {
277  return out << "key: " << pair.first << "; value: " << pair.second << '\n';
278 }
279 
283 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>> * = nullptr>
284 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
285 {
286  out << '\n';
287  std::size_t index = 0;
288  for (const auto &item : iteratable) {
289  out << std::setw(2) << index << ':' << ' ' << integralsAsHexNumber(item) << '\n';
290  ++index;
291  }
292  return out;
293 }
294 
298 namespace Literals {
303 constexpr std::size_t operator"" _st(unsigned long long size)
304 {
305  return static_cast<std::size_t>(size);
306 }
307 
312 constexpr std::uint64_t operator"" _uint64(unsigned long long size)
313 {
314  return static_cast<std::uint64_t>(size);
315 }
316 
321 constexpr std::int64_t operator"" _int64(unsigned long long size)
322 {
323  return static_cast<std::int64_t>(size);
324 }
325 } // namespace Literals
326 } // namespace CppUtilities
327 
328 #endif // TESTUTILS_H
CppUtilities::WorkingCopyMode::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:230
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
CppUtilities::AsHexNumber::AsHexNumber
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:193
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:268
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:230
CppUtilities
Contains all utilities provides by the c++utilities library.
Definition: argumentparser.h:17
CppUtilities::AsHexNumber::value
const T & value
Definition: testutils.h:197
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:190
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:220
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:203
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