Debian Codegraph @

podofo/test/unit/NameTest.cpp

raw
1 // SPDX-FileCopyrightText: 2007 Dominik Seichter <[email protected]>
2 // SPDX-FileCopyrightText: 2021 Francesco Pretto <[email protected]>
3 // SPDX-License-Identifier: MIT-0
4
5 #include <PdfTest.h>
6
7 using namespace PoDoFo;
8 using namespace std;
9
10 void TestEscapedName(const string_view& nameStr, const string_view& expectedEncoded);
11 void TestEncodedName(const string_view& pszString, const string_view& pszExpected);
12 void TestNameEquality(const string_view& name1, const string_view& name2);
13 void TestNameWrite(const string_view& view, const string_view& expected);
14 void TestFromEscape(const string_view& name1, const string_view& name2);
15
16 TEST_CASE("TestParseAndWrite")
17 {
18 const char* data = "/#E5#8A#A8#E6#80#81#E8#BF#9E#E6#8E#A5#E7#BA#BF";
19 auto device = std::make_shared<SpanStreamDevice>(data);
20 PdfTokenizer tokenizer;
21
22 string_view token;
23 PdfTokenType type;
24 bool gotToken = tokenizer.TryReadNextToken(*device, token, type);
25
26 REQUIRE(gotToken);
27 REQUIRE(type == PdfTokenType::Slash);
28
29 gotToken = tokenizer.TryReadNextToken(*device, token, type);
30
31 REQUIRE(gotToken);
32 REQUIRE(type == PdfTokenType::Literal);
33
34 // Test with const char* constructor
35 PdfName name = PdfName::FromEscaped(token);
36 PdfVariant var(name);
37 string str;
38 var.ToString(str);
39
40 REQUIRE(str == data);
41 // str.c_str() + 1 <- ignore leading slash
42 REQUIRE(name.GetEscapedName() == (string_view)str.substr(1));
43
44 // Test with string constructor
45 PdfName name2 = PdfName::FromEscaped(token);
46 PdfVariant var2(name);
47 string str2;
48 var.ToString(str2);
49
50 REQUIRE(str2 == data);
51 // str.c_str() + 1 <- ignore leading slash
52 REQUIRE(name2.GetEscapedName() == (string_view)str2.substr(1));
53 }
54
55 TEST_CASE("TestNameEncoding")
56 {
57 // Test some names. The first argument is the unencoded representation, the second
58 // is the expected encoded result. The result must not only be /a/ correct encoded
59 // name for the unencoded form, but must be the exact one PoDoFo should produce.
60 TestEscapedName("Length With Spaces", "Length#20With#20Spaces");
61 TestEscapedName("Length\001\002\003Spaces", "Length#01#02#03Spaces");
62 TestEscapedName("Length#01#02#03Spaces#7F", "Length#2301#2302#2303Spaces#237F");
63 TestEscapedName("Tab\tTest", "Tab#09Test");
64 }
65
66 TEST_CASE("TestEncodedNames")
67 {
68 // Test some pre-encoded names. The first argument is the encoded name that'll be
69 // read from the PDF; the second is the expected representation.
70 TestEncodedName("PANTONE#205757#20CV", "PANTONE 5757 CV");
71 TestEncodedName("paired#28#29parentheses", "paired()parentheses");
72 TestEncodedName("The_Key_of_F#23_Minor", "The_Key_of_F#_Minor");
73 TestEncodedName("A#42", "AB");
74 TestEncodedName("ANPA#20723-0#20AdPro", "ANPA 723-0 AdPro");
75 }
76
77 TEST_CASE("TestEquality")
78 {
79 // Make sure differently encoded names compare equal if their decoded values
80 // are equal.
81 TestNameEquality("With Spaces", "With#20Spaces");
82 TestNameEquality("#57#69#74#68#20#53#70#61#63#65#73", "With#20Spaces");
83 }
84
85 TEST_CASE("TestWrite")
86 {
87 // Make sure all names are written correctly to an output device!
88 TestNameWrite("Length With Spaces", "/Length#20With#20Spaces");
89 TestNameWrite("Length\001\002\003Spaces", "/Length#01#02#03Spaces");
90 TestNameWrite("Tab\tTest", "/Tab#09Test");
91 TestNameWrite("ANPA 723-0 AdPro", "/ANPA#20723-0#20AdPro");
92 }
93
94 TEST_CASE("TestFromEscaped")
95 {
96 TestFromEscape("ANPA#20723-0#20AdPro", "ANPA 723-0 AdPro");
97 TestFromEscape("Length#20With#20Spaces", "Length With Spaces");
98 }
99
100 //
101 // Test encoding of names.
102 // pszString : internal representation, ie unencoded name
103 // pszExpectedEncoded: the encoded string PoDoFo should produce
104 //
105 void TestEscapedName(const string_view& nameStr, const string_view& expectedEncoded)
106 {
107 INFO(utls::Format("Testing name: {}", nameStr));
108
109 PdfName name(nameStr);
110 INFO(utls::Format(" -> Expected Value: {}", expectedEncoded));
111 INFO(utls::Format(" -> Got Value: {}", name.GetEscapedName()));
112 INFO(utls::Format(" -> Unescaped Value: {}", name.GetString()));
113
114 REQUIRE(name.GetEscapedName() == expectedEncoded);
115
116 // Ensure the encoded string compares equal to its unencoded
117 // variant
118 REQUIRE(PdfName::FromEscaped(expectedEncoded) == name);
119 }
120
121 void TestEncodedName(const string_view& escaped, const string_view& expected)
122 {
123 PdfName name(PdfName::FromEscaped(escaped));
124 INFO(utls::Format("Testing encoded name: {}", escaped));
125 INFO(utls::Format(" -> Expected Value: {}", expected));
126 INFO(utls::Format(" -> Got Value: {}", name.GetString()));
127 INFO(utls::Format(" -> Escaped Value: {}", name.GetEscapedName()));
128
129 REQUIRE(name == expected);
130
131 // Ensure the name compares equal with one constructed from the
132 // expected unescaped form
133 REQUIRE(PdfName(expected) == name);
134 }
135
136 void TestNameEquality(const string_view& name1Str, const string_view& name2Str)
137 {
138 PdfName name1(PdfName::FromEscaped(name1Str));
139 PdfName name2(PdfName::FromEscaped(name2Str));
140
141 INFO(utls::Format("Testing equality of encoded names '{}' and '{}'", name1Str, name2Str));
142 INFO(utls::Format(" -> Name1 Decoded Value: {}", name1.GetString()));
143 INFO(utls::Format(" -> Name2 Decoded Value: {}", name2.GetString()));
144
145 REQUIRE(name1 == name2); // use operator==
146 REQUIRE(!(name1 != name2)); // use operator!=
147 }
148
149 void TestNameWrite(const string_view& view, const string_view& expected)
150 {
151 PdfName name(view);
152 REQUIRE(name.ToString() == expected);
153 }
154
155 void TestFromEscape(const string_view& name1, const string_view& name2)
156 {
157 PdfName name = PdfName::FromEscaped(name1);
158 REQUIRE(name == name2);
159 }