1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libqxp project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #include <algorithm>
10 #include <string>
11 #include <map>
12 #include <memory>
13 #include <vector>
14
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
17
18 #include <librevenge-stream/librevenge-stream.h>
19
20 #include "libqxp_utils.h"
21 #include "QXPDetector.h"
22 #include "QXPHeader.h"
23 #include "QXPTextParser.h"
24 #include "QXPTypes.h"
25
26 #if !defined TEST_DATA_DIR
27 #error TEST_DATA_DIR not defined, cannot test
28 #endif
29
30 namespace test
31 {
32
33 using libqxp::QXPHeader;
34 using libqxp::QXPDetector;
35 using libqxp::QXPTextParser;
36 using libqxp::CharFormat;
37 using libqxp::ParagraphFormat;
38
39 using librevenge::RVNGInputStream;
40 using std::string;
41 using std::map;
42 using std::make_shared;
43 using std::shared_ptr;
44 using std::vector;
45
46 namespace
47 {
48
createParser(const string & name)49 shared_ptr<QXPTextParser> createParser(const string &name)
50 {
51 const shared_ptr<RVNGInputStream> input(new librevenge::RVNGFileStream((string(TEST_DATA_DIR) + "/" + name).c_str()));
52 QXPDetector detector;
53 detector.detect(input);
54 CPPUNIT_ASSERT(detector.header());
55 CPPUNIT_ASSERT(detector.input());
56 CPPUNIT_ASSERT(detector.header()->load(detector.input()));
57 return make_shared<QXPTextParser>(detector.input(), detector.header());
58 }
59
60 }
61
62 class QXPTextParserTest : public CPPUNIT_NS::TestFixture
63 {
64 public:
65 virtual void setUp() override;
66 virtual void tearDown() override;
67
68 private:
69 CPPUNIT_TEST_SUITE(QXPTextParserTest);
70 CPPUNIT_TEST(testParseText);
71 CPPUNIT_TEST_SUITE_END();
72
73 private:
74 void testParseText();
75 };
76
setUp()77 void QXPTextParserTest::setUp()
78 {
79 }
80
tearDown()81 void QXPTextParserTest::tearDown()
82 {
83 }
84
testParseText()85 void QXPTextParserTest::testParseText()
86 {
87 string expectedText = "123" + string(252, 't') + "456";
88 map<string, unsigned> fileBlockMap =
89 {
90 { "qxp33mac_text", 0xb },
91 { "qxp33win_text.qxd", 0x10 },
92 { "qxp4mac_text", 0x3f },
93 { "qxp4win_text.qxd", 0x44 },
94 };
95
96 for (const auto &item : fileBlockMap)
97 {
98 const auto name = item.first;
99 const auto index = item.second;
100
101 vector<shared_ptr<CharFormat>> charFormats;
102 charFormats.emplace_back(make_shared<CharFormat>(CharFormat()));
103 vector<shared_ptr<ParagraphFormat>> paragraphFormats;
104 paragraphFormats.emplace_back(make_shared<ParagraphFormat>(ParagraphFormat()));
105
106 auto parser = createParser(name);
107 auto text = parser->parseText(index, charFormats, paragraphFormats);
108 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, expectedText, text->text);
109 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, 258u, text->charFormats[0].length);
110 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, 0u, text->charFormats[0].startIndex);
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, charFormats[0].get(), text->charFormats[0].format.get());
112 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, 258u, text->paragraphs[0].length);
113 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, 0u, text->paragraphs[0].startIndex);
114 CPPUNIT_ASSERT_EQUAL_MESSAGE(name, paragraphFormats[0].get(), text->paragraphs[0].format.get());
115 }
116 }
117
118 CPPUNIT_TEST_SUITE_REGISTRATION(QXPTextParserTest);
119
120 }
121
122 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
123