diff options
Diffstat (limited to 'xbmc/utils/test/TestRegExp.cpp')
| -rw-r--r-- | xbmc/utils/test/TestRegExp.cpp | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/xbmc/utils/test/TestRegExp.cpp b/xbmc/utils/test/TestRegExp.cpp new file mode 100644 index 0000000..3a3df8f --- /dev/null +++ b/xbmc/utils/test/TestRegExp.cpp | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | /** @todo gtest/gtest.h needs to come in before utils/RegExp.h. | ||
| 10 | * Investigate why. | ||
| 11 | */ | ||
| 12 | #include "CompileInfo.h" | ||
| 13 | #include "ServiceBroker.h" | ||
| 14 | #include "filesystem/File.h" | ||
| 15 | #include "filesystem/SpecialProtocol.h" | ||
| 16 | #include "utils/RegExp.h" | ||
| 17 | #include "utils/StringUtils.h" | ||
| 18 | #include "utils/log.h" | ||
| 19 | |||
| 20 | #include <gtest/gtest.h> | ||
| 21 | |||
| 22 | TEST(TestRegExp, RegFind) | ||
| 23 | { | ||
| 24 | CRegExp regex; | ||
| 25 | |||
| 26 | EXPECT_TRUE(regex.RegComp("^Test.*")); | ||
| 27 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 28 | |||
| 29 | EXPECT_TRUE(regex.RegComp("^string.*")); | ||
| 30 | EXPECT_EQ(-1, regex.RegFind("Test string.")); | ||
| 31 | } | ||
| 32 | |||
| 33 | TEST(TestRegExp, GetReplaceString) | ||
| 34 | { | ||
| 35 | CRegExp regex; | ||
| 36 | |||
| 37 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 38 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 39 | EXPECT_STREQ("string", regex.GetReplaceString("\\2").c_str()); | ||
| 40 | } | ||
| 41 | |||
| 42 | TEST(TestRegExp, GetFindLen) | ||
| 43 | { | ||
| 44 | CRegExp regex; | ||
| 45 | |||
| 46 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 47 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 48 | EXPECT_EQ(12, regex.GetFindLen()); | ||
| 49 | } | ||
| 50 | |||
| 51 | TEST(TestRegExp, GetSubCount) | ||
| 52 | { | ||
| 53 | CRegExp regex; | ||
| 54 | |||
| 55 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 56 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 57 | EXPECT_EQ(2, regex.GetSubCount()); | ||
| 58 | } | ||
| 59 | |||
| 60 | TEST(TestRegExp, GetSubStart) | ||
| 61 | { | ||
| 62 | CRegExp regex; | ||
| 63 | |||
| 64 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 65 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 66 | EXPECT_EQ(0, regex.GetSubStart(0)); | ||
| 67 | EXPECT_EQ(0, regex.GetSubStart(1)); | ||
| 68 | EXPECT_EQ(5, regex.GetSubStart(2)); | ||
| 69 | } | ||
| 70 | |||
| 71 | TEST(TestRegExp, GetCaptureTotal) | ||
| 72 | { | ||
| 73 | CRegExp regex; | ||
| 74 | |||
| 75 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 76 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 77 | EXPECT_EQ(2, regex.GetCaptureTotal()); | ||
| 78 | } | ||
| 79 | |||
| 80 | TEST(TestRegExp, GetMatch) | ||
| 81 | { | ||
| 82 | CRegExp regex; | ||
| 83 | |||
| 84 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 85 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 86 | EXPECT_STREQ("Test string.", regex.GetMatch(0).c_str()); | ||
| 87 | EXPECT_STREQ("Test", regex.GetMatch(1).c_str()); | ||
| 88 | EXPECT_STREQ("string", regex.GetMatch(2).c_str()); | ||
| 89 | } | ||
| 90 | |||
| 91 | TEST(TestRegExp, GetPattern) | ||
| 92 | { | ||
| 93 | CRegExp regex; | ||
| 94 | |||
| 95 | EXPECT_TRUE(regex.RegComp("^(Test)\\s*(.*)\\.")); | ||
| 96 | EXPECT_STREQ("^(Test)\\s*(.*)\\.", regex.GetPattern().c_str()); | ||
| 97 | } | ||
| 98 | |||
| 99 | TEST(TestRegExp, GetNamedSubPattern) | ||
| 100 | { | ||
| 101 | CRegExp regex; | ||
| 102 | std::string match; | ||
| 103 | |||
| 104 | EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\.")); | ||
| 105 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 106 | EXPECT_TRUE(regex.GetNamedSubPattern("first", match)); | ||
| 107 | EXPECT_STREQ("Test", match.c_str()); | ||
| 108 | EXPECT_TRUE(regex.GetNamedSubPattern("second", match)); | ||
| 109 | EXPECT_STREQ("string", match.c_str()); | ||
| 110 | } | ||
| 111 | |||
| 112 | TEST(TestRegExp, operatorEqual) | ||
| 113 | { | ||
| 114 | CRegExp regex, regexcopy; | ||
| 115 | std::string match; | ||
| 116 | |||
| 117 | EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\.")); | ||
| 118 | regexcopy = regex; | ||
| 119 | EXPECT_EQ(0, regexcopy.RegFind("Test string.")); | ||
| 120 | EXPECT_TRUE(regexcopy.GetNamedSubPattern("first", match)); | ||
| 121 | EXPECT_STREQ("Test", match.c_str()); | ||
| 122 | EXPECT_TRUE(regexcopy.GetNamedSubPattern("second", match)); | ||
| 123 | EXPECT_STREQ("string", match.c_str()); | ||
| 124 | } | ||
| 125 | |||
| 126 | class TestRegExpLog : public testing::Test | ||
| 127 | { | ||
| 128 | protected: | ||
| 129 | TestRegExpLog() = default; | ||
| 130 | ~TestRegExpLog() override { CServiceBroker::GetLogging().Uninitialize(); } | ||
| 131 | }; | ||
| 132 | |||
| 133 | TEST_F(TestRegExpLog, DumpOvector) | ||
| 134 | { | ||
| 135 | CRegExp regex; | ||
| 136 | std::string logfile, logstring; | ||
| 137 | char buf[100]; | ||
| 138 | ssize_t bytesread; | ||
| 139 | XFILE::CFile file; | ||
| 140 | |||
| 141 | std::string appName = CCompileInfo::GetAppName(); | ||
| 142 | StringUtils::ToLower(appName); | ||
| 143 | logfile = CSpecialProtocol::TranslatePath("special://temp/") + appName + ".log"; | ||
| 144 | CServiceBroker::GetLogging().Initialize( | ||
| 145 | CSpecialProtocol::TranslatePath("special://temp/").c_str()); | ||
| 146 | EXPECT_TRUE(XFILE::CFile::Exists(logfile)); | ||
| 147 | |||
| 148 | EXPECT_TRUE(regex.RegComp("^(?<first>Test)\\s*(?<second>.*)\\.")); | ||
| 149 | EXPECT_EQ(0, regex.RegFind("Test string.")); | ||
| 150 | regex.DumpOvector(LOGDEBUG); | ||
| 151 | CServiceBroker::GetLogging().Uninitialize(); | ||
| 152 | |||
| 153 | EXPECT_TRUE(file.Open(logfile)); | ||
| 154 | while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0) | ||
| 155 | { | ||
| 156 | buf[bytesread] = '\0'; | ||
| 157 | logstring.append(buf); | ||
| 158 | } | ||
| 159 | file.Close(); | ||
| 160 | EXPECT_FALSE(logstring.empty()); | ||
| 161 | |||
| 162 | EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str()); | ||
| 163 | |||
| 164 | EXPECT_TRUE(regex.RegComp(".*DEBUG <general>: regexp ovector=\\{\\[0,12\\],\\[0,4\\]," | ||
| 165 | "\\[5,11\\]\\}.*")); | ||
| 166 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 167 | |||
| 168 | EXPECT_TRUE(XFILE::CFile::Delete(logfile)); | ||
| 169 | } | ||
