C++ Utilities  4.9.2
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 "../misc/traits.h"
6 
7 #include <ostream>
8 #include <string>
9 
10 namespace TestUtilities {
11 
15 enum class WorkingCopyMode {
16  CreateCopy,
17  NoCopy
18 };
19 
21 public:
22  TestApplication(int argc, char **argv);
23  ~TestApplication();
24 
25  operator bool() const;
26  std::string testFilePath(const std::string &name) const;
27 #ifdef PLATFORM_UNIX
28  std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode) const;
29  std::string workingCopyPath(const std::string &name) const;
30  int execApp(const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1) const;
31 #endif
32  bool unitsSpecified() const;
33  const std::vector<const char *> &units() const;
34  static const TestApplication *instance();
35 
36 private:
39  ApplicationUtilities::Argument m_testFilesPathArg;
40  ApplicationUtilities::Argument m_applicationPathArg;
41  ApplicationUtilities::Argument m_workingDirArg;
43  std::string m_testFilesPathArgValue;
44  std::string m_testFilesPathEnvValue;
45  std::string m_workingDir;
46  bool m_valid;
47  static TestApplication *m_instance;
48 };
49 
56 inline TestApplication::operator bool() const
57 {
58  return m_valid;
59 }
60 
65 {
66  return TestApplication::m_instance;
67 }
68 
73 {
74  return m_unitsArg.isPresent();
75 }
76 
81 inline const std::vector<const char *> &TestApplication::units() const
82 {
83  return m_unitsArg.values();
84 }
85 
91 inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &name)
92 {
94 }
95 
96 #ifdef PLATFORM_UNIX
97 
102 inline CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &name)
103 {
104  return TestApplication::instance()->workingCopyPath(name);
105 }
106 
112 inline CPP_UTILITIES_EXPORT std::string workingCopyPathMode(const std::string &name, WorkingCopyMode mode)
113 {
114  return TestApplication::instance()->workingCopyPathMode(name, mode);
115 }
116 
122 inline CPP_UTILITIES_EXPORT int execApp(const char *const *args, std::string &output, std::string &errors)
123 {
124  return TestApplication::instance()->execApp(args, output, errors);
125 }
126 
127 CPP_UTILITIES_EXPORT int execHelperApp(
128  const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
129 #endif
130 
135 template <typename T> class AsHexNumber {
136 public:
138  AsHexNumber(const T &value)
139  : value(value)
140  {
141  }
142  const T &value;
143 };
144 
148 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
149 {
150  return lhs.value == rhs.value;
151 }
152 
156 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
157 {
158  return out << std::hex << '0' << 'x' << unsigned(value.value) << std::dec;
159 }
160 
165 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
166 {
167  return AsHexNumber<T>(value);
168 }
169 
170 #ifndef TESTUTILS_ASSERT_EXEC
171 
175 #define TESTUTILS_ASSERT_EXEC(args) CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
176 #endif
177 
181 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>>...>
182 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
183 {
184  for (const auto &item : iteratable)
185  out << item << '\n';
186  return out;
187 }
188 
192 namespace Literals {
197 constexpr std::size_t operator"" _st(unsigned long long size)
198 {
199  return static_cast<std::size_t>(size);
200 }
201 }
202 }
203 
204 #endif // TESTUTILS_H
bool unitsSpecified() const
Returns whether particular units have been specified.
Definition: testutils.h:72
The TestApplication class simplifies writing test applications that require opening test files...
Definition: testutils.h:20
std::ostream & operator<<(std::ostream &out, const AsHexNumber< T > &value)
Provides the actual formatting of the output for AsHexNumber class.
Definition: testutils.h:156
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:138
std::string testFilePath(const std::string &name) const
Returns the full path of the test file with the specified name.
Definition: testutils.cpp:148
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:10
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:148
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:81
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:165
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
Definition: testutils.h:15
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:64
#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:135
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:91
The ArgumentParser class provides a means for handling command line arguments.