| 1 |
/** |
| 2 |
* Copyright (C) 2010 by Dominik Seichter <[email protected]> |
| 3 |
* Copyright (C) 2021 by Francesco Pretto <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under GNU Library General Public 2.0 or later. |
| 6 |
* Some rights reserved. See COPYING, AUTHORS. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef PDF_TEST_H |
| 10 |
#define PDF_TEST_H |
| 11 |
|
| 12 |
#include "catch.hpp" |
| 13 |
|
| 14 |
#include <PdfTestConfig.h> |
| 15 |
|
| 16 |
#include <podofo/private/PdfDeclarationsPrivate.h> |
| 17 |
#include <podofo/podofo.h> |
| 18 |
|
| 19 |
#include <sstream> |
| 20 |
#include <filesystem> |
| 21 |
|
| 22 |
/** Check if a suitable error message is returned |
| 23 |
* Asserts that the given expression throws an exception of the specified type |
| 24 |
*/ |
| 25 |
#define ASSERT_THROW_WITH_ERROR_CODE(expression, errorCode) \ |
| 26 |
do \ |
| 27 |
{ \ |
| 28 |
bool cpputCorrectExceptionThrown_ = false; \ |
| 29 |
std::ostringstream stream; \ |
| 30 |
stream << "expected exception not thrown" << std::endl; \ |
| 31 |
stream << "Expected: " #errorCode << std::endl; \ |
| 32 |
\ |
| 33 |
try \ |
| 34 |
{ \ |
| 35 |
expression; \ |
| 36 |
} \ |
| 37 |
catch (const PdfError &e) \ |
| 38 |
{ \ |
| 39 |
if (e.GetCode() == errorCode) \ |
| 40 |
{ \ |
| 41 |
cpputCorrectExceptionThrown_ = true; \ |
| 42 |
} \ |
| 43 |
else \ |
| 44 |
{ \ |
| 45 |
stream << "Error type mismatch. Actual: " << #errorCode \ |
| 46 |
<< std::endl; \ |
| 47 |
stream << "What: " << e.GetName(); \ |
| 48 |
} \ |
| 49 |
} \ |
| 50 |
catch (const std::exception &e) \ |
| 51 |
{ \ |
| 52 |
stream << "Actual std::exception or derived" << std::endl; \ |
| 53 |
stream << "What: " << e.what() << std::endl; \ |
| 54 |
} \ |
| 55 |
catch (...) \ |
| 56 |
{ \ |
| 57 |
stream << "Actual exception unknown." << std::endl; \ |
| 58 |
} \ |
| 59 |
\ |
| 60 |
if (cpputCorrectExceptionThrown_) \ |
| 61 |
break; \ |
| 62 |
\ |
| 63 |
FAIL(stream.str()); \ |
| 64 |
} while (false); |
| 65 |
|
| 66 |
|
| 67 |
#define ASSERT_EQUAL(expected, actual) TestUtils::AssertEqual(expected, actual) |
| 68 |
|
| 69 |
namespace utls |
| 70 |
{ |
| 71 |
void CombinePaths(std::filesystem::path& path, std::initializer_list<std::string_view> paths); |
| 72 |
|
| 73 |
template<typename ... Ts> |
| 74 |
std::filesystem::path CombinePaths(const std::string_view& path1, const std::string_view& path2, |
| 75 |
Ts&&... paths) |
| 76 |
{ |
| 77 |
auto ret = std::filesystem::u8path(path1); |
| 78 |
ret /= std::filesystem::u8path(path2); |
| 79 |
CombinePaths(ret, { paths... }); |
| 80 |
return ret; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
namespace PoDoFo |
| 85 |
{ |
| 86 |
namespace fs = std::filesystem; |
| 87 |
|
| 88 |
/** |
| 89 |
* This class contains utility methods that are |
| 90 |
* often needed when writing tests. |
| 91 |
*/ |
| 92 |
class TestUtils final |
| 93 |
{ |
| 94 |
public: |
| 95 |
static constexpr double THRESHOLD = 0.001; |
| 96 |
|
| 97 |
static std::string GetTestOutputFilePath(const std::string_view& filename); |
| 98 |
static std::string GetTestInputFilePath(const std::string_view& filename); |
| 99 |
|
| 100 |
template<typename ... Ts> |
| 101 |
static std::string GetTestOutputFilePath(const std::string_view& path1, |
| 102 |
Ts&&... paths); |
| 103 |
|
| 104 |
template<typename ... Ts> |
| 105 |
static std::string GetTestInputFilePath(const std::string_view& path1, |
| 106 |
Ts&&... paths); |
| 107 |
|
| 108 |
template<typename ... Ts> |
| 109 |
static void ReadTestInputFileTo(std::string& str, const std::string_view& path1, |
| 110 |
Ts&&... paths); |
| 111 |
|
| 112 |
static const fs::path& GetTestInputPath(); |
| 113 |
static const fs::path& GetTestOutputPath(); |
| 114 |
static void ReadTestInputFileTo(std::string& str, const std::string_view& filename); |
| 115 |
static void WriteTestOutputFileTo(const std::string_view& filename, const std::string_view& view); |
| 116 |
static void AssertEqual(double expected, double actual, double threshold = THRESHOLD); |
| 117 |
static void SaveFramePPM(charbuff& buffer, const void* data, |
| 118 |
PdfPixelFormat srcPixelFormat, unsigned width, unsigned height); |
| 119 |
static void SaveFramePPM(OutputStream& stream, const void* data, |
| 120 |
PdfPixelFormat srcPixelFormat, unsigned width, unsigned height); |
| 121 |
|
| 122 |
static bool IsBufferEqual(const bufferview& buffer, const std::string_view& filename); |
| 123 |
|
| 124 |
static bool AreFilesEqual(const std::string_view& filename1, const std::string_view& filename2); |
| 125 |
|
| 126 |
private: |
| 127 |
static void readTestInputFileTo(std::string& str, const std::string_view& filepath); |
| 128 |
}; |
| 129 |
|
| 130 |
template<typename ...Ts> |
| 131 |
inline std::string TestUtils::GetTestOutputFilePath(const std::string_view& path1, Ts && ...paths) |
| 132 |
{ |
| 133 |
auto ret = GetTestOutputPath() / std::filesystem::u8path(path1); |
| 134 |
utls::CombinePaths(ret, { paths... }); |
| 135 |
return ret.u8string(); |
| 136 |
} |
| 137 |
|
| 138 |
template<typename ...Ts> |
| 139 |
inline std::string TestUtils::GetTestInputFilePath(const std::string_view& path1, Ts && ...paths) |
| 140 |
{ |
| 141 |
auto ret = GetTestInputPath() / std::filesystem::u8path(path1); |
| 142 |
utls::CombinePaths(ret, { paths... }); |
| 143 |
return ret.u8string(); |
| 144 |
} |
| 145 |
|
| 146 |
template<typename ...Ts> |
| 147 |
inline void TestUtils::ReadTestInputFileTo(std::string& str, const std::string_view& path1, Ts && ...paths) |
| 148 |
{ |
| 149 |
auto filepath = GetTestInputPath() / std::filesystem::u8path(path1); |
| 150 |
utls::CombinePaths(filepath, { paths... }); |
| 151 |
readTestInputFileTo(str, filepath.u8string()); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
#endif // PDF_TEST_H |