1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
/*
* Copyright (C) 2021 Matthieu Gautier <mgautier@kymeria.fr>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <zim/writer/contentProvider.h>
#include "../src/writer/defaultIndexData.h"
#include "gtest/gtest.h"
namespace {
std::unique_ptr<zim::writer::IndexData> index_data(const std::string& content, const std::string& title)
{
std::unique_ptr<zim::writer::ContentProvider> contentProvider(new zim::writer::StringProvider(content));
return std::unique_ptr<zim::writer::IndexData>(new zim::writer::DefaultIndexData(std::move(contentProvider), title));
}
TEST(DefaultIndexdata, empty) {
auto indexData = index_data("", "A Title");
ASSERT_EQ(indexData->hasIndexData(), false);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "");
ASSERT_EQ(indexData->getKeywords(), "");
ASSERT_EQ(indexData->getWordCount(), 0);
ASSERT_EQ(indexData->getGeoPosition(), std::make_tuple(false, 0, 0));
}
TEST(DefaultIndexdata, simple) {
auto indexData = index_data("<html><body>Some <b>bold</b> words</body><html>", "A Title");
ASSERT_EQ(indexData->hasIndexData(), true);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "some bold words");
ASSERT_EQ(indexData->getKeywords(), "");
ASSERT_EQ(indexData->getWordCount(), 3);
ASSERT_EQ(indexData->getGeoPosition(), std::make_tuple(false, 0, 0));
}
TEST(DefaultIndexdata, noindexhead) {
auto indexData = index_data(R"(<html><head><meta name="robots" content="noindex"></head><body>Some <b>bold</b> words</body><html>)", "A Title");
ASSERT_EQ(indexData->hasIndexData(), false);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "");
ASSERT_EQ(indexData->getKeywords(), "");
ASSERT_EQ(indexData->getWordCount(), 0);
ASSERT_EQ(indexData->getGeoPosition(), std::make_tuple(false, 0, 0));
}
TEST(DefaultIndexdata, noindexnone) {
auto indexData = index_data(R"(<html><head><meta name="robots" content="none"></head><body>Some <b>bold</b> words</body><html>)", "A Title");
ASSERT_EQ(indexData->hasIndexData(), false);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "");
ASSERT_EQ(indexData->getKeywords(), "");
ASSERT_EQ(indexData->getWordCount(), 0);
ASSERT_EQ(indexData->getGeoPosition(), std::make_tuple(false, 0, 0));
}
TEST(DefaultIndexdata, noindexbody) {
auto indexData = index_data("<html><body>NOINDEXSome <b>bold</b> words</body><html>", "A Title");
ASSERT_EQ(indexData->hasIndexData(), false);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "noindexsome bold words");
ASSERT_EQ(indexData->getKeywords(), "");
ASSERT_EQ(indexData->getWordCount(), 3);
ASSERT_EQ(indexData->getGeoPosition(), std::make_tuple(false, 0, 0));
}
TEST(DefaultIndexdata, full) {
auto indexData = index_data(R"(<html><head><meta name="keywords" content="some keyword important"><meta name="geo.position" content="45.005;10.100"></head><body>Some <b>bold</b> words</body><html>)", "A Title");
ASSERT_EQ(indexData->hasIndexData(), true);
ASSERT_EQ(indexData->getTitle(), "a title");
ASSERT_EQ(indexData->getContent(), "some bold words");
ASSERT_EQ(indexData->getKeywords(), "some keyword important");
ASSERT_EQ(indexData->getWordCount(), 3);
auto geoPos = indexData->getGeoPosition();
ASSERT_TRUE(std::get<0>(geoPos));
ASSERT_TRUE(std::abs(std::get<1>(geoPos)-45.005) < 0.00001);
ASSERT_TRUE(std::abs(std::get<2>(geoPos)-10.1) < 0.00001);
}
}
|