C++ Utilities  4.8.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 "../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 
132 CPP_UTILITIES_EXPORT int execHelperApp(
133  const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1);
134 #endif
135 
140 template <typename T> class AsHexNumber {
141 public:
143  AsHexNumber(const T &value)
144  : value(value)
145  {
146  }
147  const T &value;
148 };
149 
153 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
154 {
155  return lhs.value == rhs.value;
156 }
157 
161 template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value)
162 {
163  return out << std::hex << '0' << 'x' << unsigned(value.value) << std::dec;
164 }
165 
170 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
171 {
172  return AsHexNumber<T>(value);
173 }
174 
175 #ifndef TESTUTILS_ASSERT_EXEC
176 
180 #define TESTUTILS_ASSERT_EXEC(args) CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
181 #endif
182 
186 template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>>...>
187 inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable)
188 {
189  for (const auto &item : iteratable)
190  out << item << '\n';
191  return out;
192 }
193 
197 namespace Literals {
202 constexpr std::size_t operator"" _st(unsigned long long size)
203 {
204  return static_cast<std::size_t>(size);
205 }
206 }
207 }
208 
209 #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:161
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:143
std::string testFilePath(const std::string &name) const
Returns the full path of the test file with the specified name.
Definition: testutils.cpp:138
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:153
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:170
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:140
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.