C++ Utilities  4.10.0
Common C++ classes and routines used by my applications 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 <ostream>
9 #include <string>
10 
11 namespace TestUtilities {
12 
16 enum class WorkingCopyMode {
17  CreateCopy,
18  NoCopy
19 };
20 
22 public:
23  TestApplication(int argc, char **argv);
24  ~TestApplication();
25 
26  operator bool() const;
27  std::string testFilePath(const std::string &name) const;
28 #ifdef PLATFORM_UNIX
29  std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode) const;
30  std::string workingCopyPath(const std::string &name) const;
31  int execApp(const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1) const;
32 #endif
33  bool unitsSpecified() const;
34  const std::vector<const char *> &units() const;
35  static const TestApplication *instance();
36 
37 private:
40  ApplicationUtilities::Argument m_testFilesPathArg;
41  ApplicationUtilities::Argument m_applicationPathArg;
42  ApplicationUtilities::Argument m_workingDirArg;
44  std::string m_testFilesPathArgValue;
45  std::string m_testFilesPathEnvValue;
46  std::string m_workingDir;
47  bool m_valid;
48  static TestApplication *m_instance;
49 };
50 
57 inline TestApplication::operator bool() const
58 {
59  return m_valid;
60 }
61 
66 {
67  return TestApplication::m_instance;
68 }
69 
74 {
75  return m_unitsArg.isPresent();
76 }
77 
82 inline const std::vector<const char *> &TestApplication::units() const
83 {
84  return m_unitsArg.values();
85 }
86 
92 inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &name)
93 {
95 }
96 
97 #ifdef PLATFORM_UNIX
98 
103 inline CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &name)
104 {
105  return TestApplication::instance()->workingCopyPath(name);
106 }
107 
113 inline CPP_UTILITIES_EXPORT std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode)
114 {
115  return TestApplication::instance()->workingCopyPathMode(name, mode);
116 }
117 
123 inline CPP_UTILITIES_EXPORT int execApp(const char *const *args, std::string &output, std::string &errors)
124 {
125  return TestApplication::instance()->execApp(args, output, errors);
126 }
127 
128 CPP_UTILITIES_EXPORT int execHelperApp(
129  const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
130 #endif
131 
136 template <typename T> class AsHexNumber {
137 public:
139  AsHexNumber(const T &value)
140  : value(value)
141  {
142  }
143  const T &value;
144 };
145 
149 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
150 {
151  return lhs.value == rhs.value;
152 }
153 
157 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
158 {
159  return out << std::hex << '0' << 'x' << unsigned(value.value) << std::dec;
160 }
161 
166 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
167 {
168  return AsHexNumber<T>(value);
169 }
170 
171 #ifndef TESTUTILS_ASSERT_EXEC
172 
176 #define TESTUTILS_ASSERT_EXEC(args) CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
177 #endif
178 
182 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>>...>
183 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
184 {
185  for (const auto &item : iteratable)
186  out << item << '\n';
187  return out;
188 }
189 
193 namespace Literals {
198 constexpr std::size_t operator"" _st(unsigned long long size)
199 {
200  return static_cast<std::size_t>(size);
201 }
202 
207 constexpr uint64 operator"" _uint64(unsigned long long size)
208 {
209  return static_cast<uint64>(size);
210 }
211 
216 constexpr int64 operator"" _int64(unsigned long long size)
217 {
218  return static_cast<int64>(size);
219 }
220 } // namespace Literals
221 } // namespace TestUtilities
222 
223 #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:73
The TestApplication class simplifies writing test applications that require opening test files...
Definition: testutils.h:21
std::ostream & operator<<(std::ostream &out, const AsHexNumber< T > &value)
Provides the actual formatting of the output for AsHexNumber class.
Definition: testutils.h:157
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:139
std::string testFilePath(const std::string &name) const
Returns the full path of the test file with the specified name.
Definition: testutils.cpp:148
std::uint64_t uint64
unsigned 64-bit integer
Definition: types.h:49
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:11
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:149
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:82
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:166
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
Definition: testutils.h:16
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:65
#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:136
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:92
The ArgumentParser class provides a means for handling command line arguments.