| 1 | /** |
| 2 | * Copyright (C) 2022 by Francesco Pretto <[email protected]> |
| 3 | * |
| 4 | * Licensed under GNU Library General Public 2.0 or later. |
| 5 | * Some rights reserved. See COPYING, AUTHORS. |
| 6 | */ |
| 7 | |
| 8 | #include <PdfTest.h> |
| 9 | |
| 10 | #include <podofo/private/XMPUtils.h> |
| 11 | |
| 12 | using namespace std; |
| 13 | using namespace PoDoFo; |
| 14 | |
| 15 | static string removeFirstAndLastLine(const string& str); |
| 16 | |
| 17 | static void TestNormalizeXMP(string_view filename) |
| 18 | { |
| 19 | string sourceXmp; |
| 20 | TestUtils::ReadTestInputFile(string(filename) + ".xml", sourceXmp); |
| 21 | |
| 22 | unique_ptr<PdfXMPPacket> packet; |
| 23 | auto metadata = PoDoFo::GetXMPMetadata(sourceXmp, packet); |
| 24 | auto normalizedXmp = packet->ToString(); |
| 25 | |
| 26 | string expectedXmp; |
| 27 | TestUtils::ReadTestInputFile(string(filename) + "-Expected.xml", expectedXmp); |
| 28 | |
| 29 | // NOTE: This just fixes tests with last resources as in recent PoDoFo versions |
| 30 | // we do better normalization of the XMP packet begin/end tags |
| 31 | normalizedXmp = removeFirstAndLastLine(normalizedXmp); |
| 32 | expectedXmp = removeFirstAndLastLine(expectedXmp); |
| 33 | |
| 34 | REQUIRE(normalizedXmp == expectedXmp); |
| 35 | } |
| 36 | |
| 37 | TEST_CASE("TestNormalizeXMP") |
| 38 | { |
| 39 | TestNormalizeXMP("TestXMP1"); |
| 40 | TestNormalizeXMP("TestXMP5"); |
| 41 | TestNormalizeXMP("TestXMP7"); |
| 42 | } |
| 43 | |
| 44 | string removeFirstAndLastLine(const string& str) |
| 45 | { |
| 46 | // Trim trailing newlines first |
| 47 | size_t end = str.size(); |
| 48 | while (end > 0 && (str[end - 1] == '\n' || str[end - 1] == '\r')) |
| 49 | --end; |
| 50 | |
| 51 | // Work on the trimmed view [0, end) |
| 52 | size_t firstNewline = str.find('\n'); |
| 53 | if (firstNewline == string::npos || firstNewline >= end) |
| 54 | return ""; // Only one (non-empty) line |
| 55 | |
| 56 | size_t lastNewline = str.rfind('\n', end - 1); |
| 57 | |
| 58 | if (firstNewline == lastNewline) |
| 59 | return ""; // Only two lines, nothing left after removing both |
| 60 | |
| 61 | return str.substr(firstNewline + 1, lastNewline - firstNewline - 1); |
| 62 | } |