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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
/** @todo gtest/gtest.h needs to come in before utils/RegExp.h.
* Investigate why.
*/
#include "CompileInfo.h"
#include "ServiceBroker.h"
#include "filesystem/File.h"
#include "filesystem/SpecialProtocol.h"
#include "utils/RegExp.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include <gtest/gtest.h>
TEST(TestRegExp, RegFind)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^Test.*"));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_TRUE(regex.RegComp("^string.*"));
EXPECT_EQ(-1, regex.RegFind("Test string."));
}
TEST(TestRegExp, GetReplaceString)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_STREQ("string", regex.GetReplaceString("\\2").c_str());
}
TEST(TestRegExp, GetFindLen)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_EQ(12, regex.GetFindLen());
}
TEST(TestRegExp, GetSubCount)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_EQ(2, regex.GetSubCount());
}
TEST(TestRegExp, GetSubStart)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_EQ(0, regex.GetSubStart(0));
EXPECT_EQ(0, regex.GetSubStart(1));
EXPECT_EQ(5, regex.GetSubStart(2));
}
TEST(TestRegExp, GetCaptureTotal)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_EQ(2, regex.GetCaptureTotal());
}
TEST(TestRegExp, GetMatch)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_STREQ("Test string.", regex.GetMatch(0).c_str());
EXPECT_STREQ("Test", regex.GetMatch(1).c_str());
EXPECT_STREQ("string", regex.GetMatch(2).c_str());
}
TEST(TestRegExp, GetPattern)
{
CRegExp regex;
EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\."));
EXPECT_STREQ("^(Test)\\s*(.*)\\.", regex.GetPattern().c_str());
}
TEST(TestRegExp, GetNamedSubPattern)
{
CRegExp regex;
std::string match;
EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
EXPECT_TRUE(regex.GetNamedSubPattern("first", match));
EXPECT_STREQ("Test", match.c_str());
EXPECT_TRUE(regex.GetNamedSubPattern("second", match));
EXPECT_STREQ("string", match.c_str());
}
TEST(TestRegExp, operatorEqual)
{
CRegExp regex, regexcopy;
std::string match;
EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\."));
regexcopy = regex;
EXPECT_EQ(0, regexcopy.RegFind("Test string."));
EXPECT_TRUE(regexcopy.GetNamedSubPattern("first", match));
EXPECT_STREQ("Test", match.c_str());
EXPECT_TRUE(regexcopy.GetNamedSubPattern("second", match));
EXPECT_STREQ("string", match.c_str());
}
class TestRegExpLog : public testing::Test
{
protected:
TestRegExpLog() = default;
~TestRegExpLog() override { CServiceBroker::GetLogging().Deinitialize(); }
};
TEST_F(TestRegExpLog, DumpOvector)
{
CRegExp regex;
std::string logfile, logstring;
char buf[100];
ssize_t bytesread;
XFILE::CFile file;
std::string appName = CCompileInfo::GetAppName();
StringUtils::ToLower(appName);
logfile = CSpecialProtocol::TranslatePath("special://temp/") + appName + ".log";
CServiceBroker::GetLogging().Initialize(
CSpecialProtocol::TranslatePath("special://temp/").c_str());
EXPECT_TRUE(XFILE::CFile::Exists(logfile));
EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\."));
EXPECT_EQ(0, regex.RegFind("Test string."));
regex.DumpOvector(LOGDEBUG);
CServiceBroker::GetLogging().Deinitialize();
EXPECT_TRUE(file.Open(logfile));
while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0)
{
buf[bytesread] = '\0';
logstring.append(buf);
}
file.Close();
EXPECT_FALSE(logstring.empty());
EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str());
EXPECT_TRUE(regex.RegComp(".*(debug|DEBUG) <general>: regexp ovector=\\{\\[0,12\\],\\[0,4\\],"
"\\[5,11\\]\\}.*"));
EXPECT_GE(regex.RegFind(logstring), 0);
EXPECT_TRUE(XFILE::CFile::Delete(logfile));
}
|