C++ Utilities  4.6.1
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 
6 #include <string>
7 #include <ostream>
8 
9 namespace TestUtilities {
10 
14 enum class WorkingCopyMode
15 {
16  CreateCopy,
17  NoCopy
18 };
19 
21 {
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 #endif
128 
133 template <typename T> class AsHexNumber
134 {
135 public:
137  AsHexNumber(const T &value) : value(value) {}
138  const T &value;
139 };
140 
144 template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs)
145 {
146  return lhs.value == rhs.value;
147 }
148 
152 template <typename T> std::ostream &operator<< (std::ostream &out, const AsHexNumber<T> &value)
153 {
154  return out << std::hex << '0' << 'x' << unsigned(value.value) << std::dec;
155 }
156 
161 template <typename T> AsHexNumber<T> asHexNumber(const T &value)
162 {
163  return AsHexNumber<T>(value);
164 }
165 
166 #ifndef TESTUTILS_ASSERT_EXEC
167 
171 # define TESTUTILS_ASSERT_EXEC(args) \
172  CPPUNIT_ASSERT_EQUAL(0, execApp(args, stdout, stderr))
173 #endif
174 
175 }
176 
177 #endif // TESTUTILS_H
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:20
AsHexNumber(const T &value)
Constructs a new instance; use asHexNumber() for convenience instead.
Definition: testutils.h:137
std::string testFilePath(const std::string &name) const
Returns the full path of the test file with the specified name.
Definition: testutils.cpp:137
Contains classes and functions utilizing creating of test applications.
Definition: testutils.h:9
bool operator==(const AsHexNumber< T > &lhs, const AsHexNumber< T > &rhs)
Provides operator == required by CPPUNIT_ASSERT_EQUAL.
Definition: testutils.h:144
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:161
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
Definition: testutils.h:14
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:133
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.