| 1 | #ifndef TESTUTILS_H |
| 2 | #define TESTUTILS_H |
| 3 | |
| 4 | #include "../application/argumentparser.h" |
| 5 | #include "../chrono/format.h" |
| 6 | #include "../misc/traits.h" |
| 7 | |
| 8 | #include <iomanip> |
| 9 | #include <optional> |
| 10 | #include <ostream> |
| 11 | #include <string> |
| 12 | |
| 13 | #if defined(PLATFORM_UNIX) || defined(CPP_UTILITIES_BOOST_PROCESS) |
| 14 | #define CPP_UTILITIES_HAS_EXEC_APP |
| 15 | #endif |
| 16 | |
| 17 | // ensure CppUnit's macros produce unique variable names when doing unity builds |
| 18 | #if defined(__COUNTER__) |
| 19 | #undef CPPUNIT_UNIQUE_COUNTER |
| 20 | #define CPPUNIT_UNIQUE_COUNTER __COUNTER__ |
| 21 | #endif |
| 22 | |
| 23 | namespace CppUtilities { |
| 24 | |
| 25 | /*! |
| 26 | * \brief The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::workingCopyPath(). |
| 27 | */ |
| 28 | enum class WorkingCopyMode { |
| 29 | CreateCopy, /**< a working copy of the test file is created */ |
| 30 | NoCopy, /**< only the directory for the working copy is created but not the test file itself */ |
| 31 | Cleanup, /**< the directory for the working copy is created if needed or a previously existing file is deleted */ |
| 32 | }; |
| 33 | |
| 34 | class CPP_UTILITIES_EXPORT TestApplication { |
| 35 | public: |
| 36 | // construction/destruction |
| 37 | explicit TestApplication(); |
| 38 | explicit TestApplication(int argc, const char *const *argv); |
| 39 | ~TestApplication(); |
| 40 | operator bool() const; |
| 41 | |
| 42 | // helper for tests |
| 43 | std::string testFilePath(const std::string &relativeTestFilePath) const; |
| 44 | std::string testDirPath(const std::string &relativeTestDirPath) const; |
| 45 | std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) const; |
| 46 | std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, |
| 47 | WorkingCopyMode mode = WorkingCopyMode::CreateCopy) const; |
| 48 | #ifdef CPP_UTILITIES_HAS_EXEC_APP |
| 49 | int execApp(const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1) const; |
| 50 | #endif |
| 51 | |
| 52 | // read-only accessors |
| 53 | const std::vector<std::string> &testFilePaths() const; |
| 54 | const std::string &workingDirectory() const; |
| 55 | const char *applicationPath(); |
| 56 | bool unitsSpecified() const; |
| 57 | const std::vector<const char *> &units() const; |
| 58 | bool onlyListUnits() const; |
| 59 | |
| 60 | // static read-only accessors |
| 61 | static const TestApplication *instance(); |
| 62 | static const char *appPath(); |
| 63 | |
| 64 | private: |
| 65 | static std::string readTestfilePathFromEnv(); |
| 66 | static std::vector<std::string> readTestfilePathFromSrcRef(); |
| 67 | |
| 68 | ArgumentParser m_parser; |
| 69 | OperationArgument m_listArg; |
| 70 | OperationArgument m_runArg; |
| 71 | ConfigValueArgument m_testFilesPathArg; |
| 72 | ConfigValueArgument m_applicationPathArg; |
| 73 | ConfigValueArgument m_workingDirArg; |
| 74 | ConfigValueArgument m_unitsArg; |
| 75 | std::vector<std::string> m_testFilesPaths; |
| 76 | std::string m_workingDir; |
| 77 | bool m_valid; |
| 78 | static TestApplication *s_instance; |
| 79 | }; |
| 80 | |
| 81 | /*! |
| 82 | * \brief Returns whether the TestApplication instance is valid. |
| 83 | * |
| 84 | * An instance is considered invalid if an error occurred when |
| 85 | * parsing the command line arguments. |
| 86 | */ |
| 87 | inline TestApplication::operator bool() const |
| 88 | { |
| 89 | return m_valid; |
| 90 | } |
| 91 | |
| 92 | /*! |
| 93 | * \brief Returns the current TestApplication instance. |
| 94 | */ |
| 95 | inline const TestApplication *TestApplication::instance() |
| 96 | { |
| 97 | return TestApplication::s_instance; |
| 98 | } |
| 99 | |
| 100 | /*! |
| 101 | * \brief Returns the application path or an empty string if no application path has been set. |
| 102 | */ |
| 103 | inline const char *TestApplication::appPath() |
| 104 | { |
| 105 | return s_instance ? s_instance->applicationPath() : ""; |
| 106 | } |
| 107 | |
| 108 | /*! |
| 109 | * \brief Returns the list of directories to look for test files. |
| 110 | */ |
| 111 | inline const std::vector<std::string> &TestApplication::testFilePaths() const |
| 112 | { |
| 113 | return m_testFilesPaths; |
| 114 | } |
| 115 | |
| 116 | /*! |
| 117 | * \brief Returns the directory which is supposed to used for storing files created by tests. |
| 118 | */ |
| 119 | inline const std::string &TestApplication::workingDirectory() const |
| 120 | { |
| 121 | return m_workingDir; |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | * \brief Returns the application path or an empty string if no application path has been set. |
| 126 | */ |
| 127 | inline const char *TestApplication::applicationPath() |
| 128 | { |
| 129 | return m_applicationPathArg.firstValue() ? m_applicationPathArg.firstValue() : ""; |
| 130 | } |
| 131 | |
| 132 | /*! |
| 133 | * \brief Returns whether particular units have been specified. |
| 134 | */ |
| 135 | inline bool TestApplication::unitsSpecified() const |
| 136 | { |
| 137 | return m_unitsArg.isPresent(); |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | * \brief Returns the specified test units. |
| 142 | * \remarks The units argument must be present. |
| 143 | */ |
| 144 | inline const std::vector<const char *> &TestApplication::units() const |
| 145 | { |
| 146 | return m_unitsArg.values(); |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | * \brief Returns whether the test application should only list available units and not actually run any tests. |
| 151 | */ |
| 152 | inline bool TestApplication::onlyListUnits() const |
| 153 | { |
| 154 | return m_listArg.isPresent(); |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | * \brief Convenience function to invoke TestApplication::testFilePath(). |
| 159 | * \remarks A TestApplication must be present. |
| 160 | */ |
| 161 | inline CPP_UTILITIES_EXPORT std::string testFilePath(const std::string &relativeTestFilePath) |
| 162 | { |
| 163 | return TestApplication::instance()->testFilePath(relativeTestFilePath); |
| 164 | } |
| 165 | |
| 166 | /*! |
| 167 | * \brief Convenience function to invoke TestApplication::testDirPath(). |
| 168 | * \remarks A TestApplication must be present. |
| 169 | */ |
| 170 | inline CPP_UTILITIES_EXPORT std::string testDirPath(const std::string &relativeTestDirPath) |
| 171 | { |
| 172 | return TestApplication::instance()->testDirPath(relativeTestDirPath); |
| 173 | } |
| 174 | |
| 175 | /*! |
| 176 | * \brief Convenience function to invoke TestApplication::workingCopyPath(). |
| 177 | * \remarks A TestApplication must be present. |
| 178 | */ |
| 179 | inline CPP_UTILITIES_EXPORT std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) |
| 180 | { |
| 181 | return TestApplication::instance()->workingCopyPathAs(relativeTestFilePath, relativeTestFilePath, mode); |
| 182 | } |
| 183 | |
| 184 | /*! |
| 185 | * \brief Convenience function to invoke TestApplication::workingCopyPathAs(). |
| 186 | * \remarks A TestApplication must be present. |
| 187 | */ |
| 188 | inline CPP_UTILITIES_EXPORT std::string workingCopyPathAs( |
| 189 | const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode = WorkingCopyMode::CreateCopy) |
| 190 | { |
| 191 | return TestApplication::instance()->workingCopyPathAs(relativeTestFilePath, relativeWorkingCopyPath, mode); |
| 192 | } |
| 193 | |
| 194 | #ifdef CPP_UTILITIES_HAS_EXEC_APP |
| 195 | /*! |
| 196 | * \brief Convenience function which executes the application to be tested with the specified \a args. |
| 197 | * \remarks A TestApplication must be present. |
| 198 | * \sa TestApplication::execApp() |
| 199 | */ |
| 200 | inline CPP_UTILITIES_EXPORT int execApp(const char *const *args, std::string &output, std::string &errors) |
| 201 | { |
| 202 | return TestApplication::instance()->execApp(args, output, errors); |
| 203 | } |
| 204 | |
| 205 | CPP_UTILITIES_EXPORT int execHelperApp( |
| 206 | const char *appPath, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1); |
| 207 | CPP_UTILITIES_EXPORT int execHelperAppInSearchPath( |
| 208 | const char *appName, const char *const *args, std::string &output, std::string &errors, bool suppressLogging = false, int timeout = -1); |
| 209 | #endif |
| 210 | |
| 211 | /*! |
| 212 | * \brief Allows printing std::optional objects so those can be asserted using CPPUNIT_ASSERT_EQUAL. |
| 213 | */ |
| 214 | template <typename Optional, Traits::EnableIf<Traits::IsSpecializationOf<Optional, std::optional>> * = nullptr> |
| 215 | inline std::ostream &operator<<(std::ostream &out, const Optional &optional) |
| 216 | { |
| 217 | if (optional.has_value()) { |
| 218 | return out << *optional; |
| 219 | } else { |
| 220 | return out << "[no value]"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /*! |
| 225 | * \brief The AsHexNumber class allows printing values asserted with cppunit (or similar test framework) using the |
| 226 | * hex system in the error case. |
| 227 | */ |
| 228 | template <typename T> class AsHexNumber { |
| 229 | public: |
| 230 | /// \brief Constructs a new instance; use asHexNumber() for convenience instead. |
| 231 | AsHexNumber(const T &value) |
| 232 | : value(value) |
| 233 | { |
| 234 | } |
| 235 | const T &value; |
| 236 | }; |
| 237 | |
| 238 | /*! |
| 239 | * \brief Provides operator == required by CPPUNIT_ASSERT_EQUAL. |
| 240 | */ |
| 241 | template <typename T> bool operator==(const AsHexNumber<T> &lhs, const AsHexNumber<T> &rhs) |
| 242 | { |
| 243 | return lhs.value == rhs.value; |
| 244 | } |
| 245 | |
| 246 | /*! |
| 247 | * \brief Provides the actual formatting of the output for AsHexNumber class. |
| 248 | */ |
| 249 | template <typename T> std::ostream &operator<<(std::ostream &out, const AsHexNumber<T> &value) |
| 250 | { |
| 251 | return out << '0' << 'x' << std::hex << std::setfill('0') << std::setw(2) << unsigned(value.value) << std::dec; |
| 252 | } |
| 253 | |
| 254 | /*! |
| 255 | * \brief Wraps a value to be printed using the hex system in the error case when asserted |
| 256 | * with cppunit (or similar test framework). |
| 257 | */ |
| 258 | template <typename T> AsHexNumber<T> asHexNumber(const T &value) |
| 259 | { |
| 260 | return AsHexNumber<T>(value); |
| 261 | } |
| 262 | |
| 263 | /*! |
| 264 | * \brief Wraps a value to be printed using the hex system in the error case when asserted |
| 265 | * with cppunit (or similar test framework). |
| 266 | * \remarks Only affects integral types. Values of other types are printed as usual. |
| 267 | */ |
| 268 | template <typename T, Traits::EnableIf<std::is_integral<T>> * = nullptr> AsHexNumber<T> integralsAsHexNumber(const T &value) |
| 269 | { |
| 270 | return AsHexNumber<T>(value); |
| 271 | } |
| 272 | |
| 273 | /*! |
| 274 | * \brief Wraps a value to be printed using the hex system in the error case when asserted |
| 275 | * with cppunit (or similar test framework). |
| 276 | * \remarks Only affects integral types. Values of other types are printed as usual. |
| 277 | */ |
| 278 | template <typename T, Traits::DisableIf<std::is_integral<T>> * = nullptr> const T &integralsAsHexNumber(const T &value) |
| 279 | { |
| 280 | return value; |
| 281 | } |
| 282 | |
| 283 | /*! |
| 284 | * \brief Asserts successful execution of the application with the specified CLI \a args. |
| 285 | * |
| 286 | * The application is executed via TestApplication::execApp(). Output is stored in the std::string variables stdout |
| 287 | * and stderr. |
| 288 | * |
| 289 | * \remarks Requires cppunit. |
| 290 | */ |
| 291 | #define TESTUTILS_ASSERT_EXEC(args) TESTUTILS_ASSERT_EXEC_EXIT_STATUS(args, 0) |
| 292 | |
| 293 | /*! |
| 294 | * \brief Asserts the execution of the application with the specified CLI \a args and the specified \a expectedExitStatus. |
| 295 | * |
| 296 | * The application is executed via TestApplication::execApp(). Output is stored in the std::string variables stdout |
| 297 | * and stderr. |
| 298 | * |
| 299 | * \remarks Requires cppunit. |
| 300 | */ |
| 301 | #ifdef CPP_UTILITIES_BOOST_PROCESS |
| 302 | #define TESTUTILS_ASSERT_EXEC_EXIT_STATUS(args, expectedExitStatus) \ |
| 303 | { \ |
| 304 | const auto status = execApp(args, stdout, stderr); \ |
| 305 | if (status != expectedExitStatus) { \ |
| 306 | CPPUNIT_FAIL(::CppUtilities::argsToString( \ |
| 307 | "app exited with status ", status, " (expected ", expectedExitStatus, ")\nstdout: ", stdout, "\nstderr: ", stderr)); \ |
| 308 | } \ |
| 309 | } |
| 310 | #else |
| 311 | #define TESTUTILS_ASSERT_EXEC_EXIT_STATUS(args, expectedExitStatus) \ |
| 312 | { \ |
| 313 | const auto status = execApp(args, stdout, stderr); \ |
| 314 | if (!WIFEXITED(status)) { \ |
| 315 | CPPUNIT_FAIL(::CppUtilities::argsToString("app did not terminate normally\nstdout: ", stdout, "\nstderr: ", stderr)); \ |
| 316 | } \ |
| 317 | if (const auto exitStatus = WEXITSTATUS(status); exitStatus != expectedExitStatus) { \ |
| 318 | CPPUNIT_FAIL(::CppUtilities::argsToString( \ |
| 319 | "app exited with status ", exitStatus, " (expected ", expectedExitStatus, ")\nstdout: ", stdout, "\nstderr: ", stderr)); \ |
| 320 | } \ |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | /*! |
| 325 | * \brief Asserts whether the specified \a string matches the specified \a regex. |
| 326 | * \remarks Requires cppunit. |
| 327 | */ |
| 328 | #define TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, regexFlags, actualString) \ |
| 329 | CPPUNIT_ASSERT_MESSAGE( \ |
| 330 | CppUtilities::argsToString('\"', actualString, "\"\n not like\n\"", expectedRegex, '\"', "\n\nExpression: " #actualString, message), \ |
| 331 | (std::regex_match(actualString, std::regex(expectedRegex, regexFlags)))) |
| 332 | /*! |
| 333 | * \brief Asserts whether the specified \a string matches the specified \a regex. |
| 334 | * \remarks Requires cppunit. |
| 335 | */ |
| 336 | #define TESTUTILS_ASSERT_LIKE(message, expectedRegex, actualString) \ |
| 337 | TESTUTILS_ASSERT_LIKE_FLAGS(message, expectedRegex, std::regex::ECMAScript, actualString) |
| 338 | |
| 339 | /*! |
| 340 | * \brief Allows printing pairs so key/values of maps/hashes can be asserted using CPPUNIT_ASSERT_EQUAL. |
| 341 | */ |
| 342 | template <typename Pair, CppUtilities::Traits::EnableIf<CppUtilities::Traits::IsSpecializationOf<Pair, std::pair>> * = nullptr> |
| 343 | inline std::ostream &operator<<(std::ostream &out, const Pair &pair) |
| 344 | { |
| 345 | return out << "key: " << pair.first << "; value: " << pair.second << '\n'; |
| 346 | } |
| 347 | |
| 348 | /*! |
| 349 | * \brief Allows printing iteratable objects so those can be asserted using CPPUNIT_ASSERT_EQUAL. |
| 350 | */ |
| 351 | template <typename Iteratable, Traits::EnableIf<Traits::IsIteratable<Iteratable>, Traits::Not<Traits::IsString<Iteratable>>> * = nullptr> |
| 352 | inline std::ostream &operator<<(std::ostream &out, const Iteratable &iteratable) |
| 353 | { |
| 354 | out << '\n'; |
| 355 | std::size_t index = 0; |
| 356 | for (const auto &item : iteratable) { |
| 357 | out << std::setw(2) << index << ':' << ' ' << integralsAsHexNumber(item) << '\n'; |
| 358 | ++index; |
| 359 | } |
| 360 | return out; |
| 361 | } |
| 362 | |
| 363 | /*! |
| 364 | * \brief Contains literals to ease asserting with CPPUNIT_ASSERT_EQUAL. |
| 365 | */ |
| 366 | namespace Literals { |
| 367 | /*! |
| 368 | * \brief Literal for std::size_t to ease asserting std::size_t with CPPUNIT_ASSERT_EQUAL. |
| 369 | * \remarks Just using "ul"-suffix does not compile under 32-bit architecture! |
| 370 | */ |
| 371 | constexpr std::size_t operator""_st(unsigned long long size) |
| 372 | { |
| 373 | return static_cast<std::size_t>(size); |
| 374 | } |
| 375 | |
| 376 | /*! |
| 377 | * \brief Literal for uint64 to ease asserting uint64 with CPPUNIT_ASSERT_EQUAL. |
| 378 | * \remarks Just using "ul"-suffix does not compile under 32-bit architecture! |
| 379 | */ |
| 380 | constexpr std::uint64_t operator""_uint64(unsigned long long size) |
| 381 | { |
| 382 | return static_cast<std::uint64_t>(size); |
| 383 | } |
| 384 | |
| 385 | /*! |
| 386 | * \brief Literal for int64 to ease asserting int64 with CPPUNIT_ASSERT_EQUAL. |
| 387 | * \remarks Just using "l"-suffix does not compile under 32-bit architecture! |
| 388 | */ |
| 389 | constexpr std::int64_t operator""_int64(unsigned long long size) |
| 390 | { |
| 391 | return static_cast<std::int64_t>(size); |
| 392 | } |
| 393 | } // namespace Literals |
| 394 | } // namespace CppUtilities |
| 395 | |
| 396 | #endif // TESTUTILS_H |