C++ Utilities  4.14.2
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 << std::hex << '0' << 'x' << 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 
184 #ifndef TESTUTILS_ASSERT_EXEC
185 
189 #define TESTUTILS_ASSERT_EXEC(args) CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
190 #endif
191 
195 template <typename Pair, Traits::EnableIf<Traits::IsSpecializationOf<Pair, std::pair>> * = nullptr>
196 inline std::ostream &operator<<(std::ostream &out, const Pair &pair)
197 {
198  return out << "key: " << pair.first << "; value: " << pair.second << '\n';
199 }
200 
204 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>> * = nullptr>
205 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
206 {
207  out << '\n';
208  std::size_t index = 0;
209  for (const auto &item : iteratable) {
210  out << std::setw(2) << index << ':' << ' ' << item << '\n';
211  ++index;
212  }
213  return out;
214 }
215 
219 namespace Literals {
224 constexpr std::size_t operator"" _st(unsigned long long size)
225 {
226  return static_cast<std::size_t>(size);
227 }
228 
233 constexpr uint64 operator"" _uint64(unsigned long long size)
234 {
235  return static_cast<uint64>(size);
236 }
237 
242 constexpr int64 operator"" _int64(unsigned long long size)
243 {
244  return static_cast<int64>(size);
245 }
246 } // namespace Literals
247 } // namespace TestUtilities
248 
249 #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:170
const char * firstValue() const
Returns the first parameter value of the first occurrence of the argument.
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.