Debian Codegraph @

podofo/test/common/PdfTest.h

raw symbols SCIP
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 #define PODOFO_UNIT_TEST(classname) friend class classname
17 #include <podofo/private/PdfDeclarationsPrivate.h>
18 #include <podofo/podofo.h>
19
20 #include <sstream>
21 #include <filesystem>
22
23 /** Check if a suitable error message is returned
24 * Asserts that the given expression throws an exception of the specified type
25 */
26 #define ASSERT_THROW_WITH_ERROR_CODE(expression, errorCode) \
27 do \
28 { \
29 bool cpputCorrectExceptionThrown_ = false; \
30 std::ostringstream stream; \
31 stream << "expected exception not thrown" << std::endl; \
32 stream << "Expected: " #errorCode << std::endl; \
33 \
34 try \
35 { \
36 expression; \
37 } \
38 catch (const PdfError &e) \
39 { \
40 if (e.GetCode() == errorCode) \
41 { \
42 cpputCorrectExceptionThrown_ = true; \
43 } \
44 else \
45 { \
46 stream << "Error type mismatch. Actual: " << #errorCode \
47 << std::endl; \
48 stream << "What: " << e.GetName(); \
49 } \
50 } \
51 catch (const std::exception &e) \
52 { \
53 stream << "Actual std::exception or derived" << std::endl; \
54 stream << "What: " << e.what() << std::endl; \
55 } \
56 catch (...) \
57 { \
58 stream << "Actual exception unknown." << std::endl; \
59 } \
60 \
61 if (cpputCorrectExceptionThrown_) \
62 break; \
63 \
64 FAIL(stream.str()); \
65 } while (false);
66
67
68 #define ASSERT_EQUAL(expected, actual) TestUtils::AssertEqual(expected, actual)
69
70 namespace utls
71 {
72 void CombinePaths(std::filesystem::path& path, std::initializer_list<std::string_view> paths);
73
74 template<typename ... Ts>
75 std::filesystem::path CombinePaths(const std::string_view& path1, const std::string_view& path2,
76 Ts&&... paths)
77 {
78 auto ret = std::filesystem::u8path(path1);
79 ret /= std::filesystem::u8path(path2);
80 CombinePaths(ret, { paths... });
81 return ret;
82 }
83 }
84
85 namespace PoDoFo
86 {
87 namespace fs = std::filesystem;
88
89 /**
90 * This class contains utility methods that are
91 * often needed when writing tests.
92 */
93 class TestUtils final
94 {
95 public:
96 static constexpr double THRESHOLD = 0.001;
97
98 static std::string GetTestOutputFilePath(const std::string_view& filename);
99 static std::string GetTestInputFilePath(const std::string_view& filename);
100
101 template<typename ... Ts>
102 static std::string GetTestOutputFilePath(const std::string_view& path1,
103 Ts&&... paths);
104
105 template<typename ... Ts>
106 static std::string GetTestInputFilePath(const std::string_view& path1,
107 Ts&&... paths);
108
109 static const fs::path& GetTestInputPath();
110 static const fs::path& GetTestOutputPath();
111 static void ReadTestInputFile(const std::string_view& filename, std::string& str);
112 static void WriteTestOutputFile(const std::string_view& filename, const std::string_view& view);
113 static void AssertEqual(double expected, double actual, double threshold = THRESHOLD);
114 static void SaveFramePPM(charbuff& buffer, const void* data,
115 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height);
116 static void SaveFramePPM(OutputStream& stream, const void* data,
117 PdfPixelFormat srcPixelFormat, unsigned width, unsigned height);
118 };
119
120 template<typename ...Ts>
121 inline std::string TestUtils::GetTestOutputFilePath(const std::string_view& path1, Ts && ...paths)
122 {
123 auto ret = GetTestOutputPath() / std::filesystem::u8path(path1);
124 utls::CombinePaths(ret, { paths... });
125 return ret.u8string();
126 }
127
128 template<typename ...Ts>
129 inline std::string TestUtils::GetTestInputFilePath(const std::string_view& path1, Ts && ...paths)
130 {
131 auto ret = GetTestInputPath() / std::filesystem::u8path(path1);
132 utls::CombinePaths(ret, { paths... });
133 return ret.u8string();
134 }
135 }
136
137 #endif // PDF_TEST_H