From 5b852bb78d5d40bee20805c916ecefbf350ca8f0 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Wed, 6 Nov 2024 13:08:44 +0500 Subject: [PATCH] Tests: Add "tst_ruletextparser.h" --- src/tests/UtilTest/UtilTest.pro | 1 + src/tests/UtilTest/tst_main.cpp | 1 + src/tests/UtilTest/tst_ruletextparser.h | 28 +++++++++++++++++++++++++ src/ui/util/conf/ruletextparser.cpp | 23 ++++++++++---------- src/ui/util/conf/ruletextparser.h | 13 ++++++------ 5 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 src/tests/UtilTest/tst_ruletextparser.h diff --git a/src/tests/UtilTest/UtilTest.pro b/src/tests/UtilTest/UtilTest.pro index 28fa153f..8a81bdd4 100644 --- a/src/tests/UtilTest/UtilTest.pro +++ b/src/tests/UtilTest/UtilTest.pro @@ -7,6 +7,7 @@ HEADERS += \ tst_fileutil.h \ tst_ioccontainer.h \ tst_netutil.h \ + tst_ruletextparser.h \ tst_stringutil.h SOURCES += \ diff --git a/src/tests/UtilTest/tst_main.cpp b/src/tests/UtilTest/tst_main.cpp index 5db4d3c1..d49bb16b 100644 --- a/src/tests/UtilTest/tst_main.cpp +++ b/src/tests/UtilTest/tst_main.cpp @@ -4,6 +4,7 @@ #include "tst_fileutil.h" #include "tst_ioccontainer.h" #include "tst_netutil.h" +#include "tst_ruletextparser.h" #include "tst_stringutil.h" #include diff --git a/src/tests/UtilTest/tst_ruletextparser.h b/src/tests/UtilTest/tst_ruletextparser.h new file mode 100644 index 00000000..91f820ac --- /dev/null +++ b/src/tests/UtilTest/tst_ruletextparser.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include + +#include + +class RuleTextParserTest : public Test +{ + // Test interface +protected: + void SetUp(); + void TearDown(); +}; + +void RuleTextParserTest::SetUp() { } + +void RuleTextParserTest::TearDown() { } + +TEST_F(RuleTextParserTest, emptyList) +{ + RuleTextParser p("{}"); + + ASSERT_TRUE(p.parse()); + + ASSERT_EQ(p.ruleFilters().size(), 0); +} diff --git a/src/ui/util/conf/ruletextparser.cpp b/src/ui/util/conf/ruletextparser.cpp index 9ea62c7b..4ee44d29 100644 --- a/src/ui/util/conf/ruletextparser.cpp +++ b/src/ui/util/conf/ruletextparser.cpp @@ -66,15 +66,15 @@ bool RuleFilter::isTypeAddress() const || type == FORT_RULE_FILTER_TYPE_ADDRESS_UDP; } -RuleTextParser::RuleTextParser(const QString &text, QObject *parent) : QObject(parent) +RuleTextParser::RuleTextParser(const QString &text, QObject *parent) : QObject(parent), m_text(text) { - setupText(text); + setupCharPtr(); } -void RuleTextParser::setupText(const QString &text) +void RuleTextParser::setupCharPtr() { - m_p = text.data(); - m_end = m_p + text.size(); + m_p = m_text.data(); + m_end = m_p + m_text.size(); } bool RuleTextParser::parse() @@ -341,14 +341,14 @@ void RuleTextParser::resetFilter() void RuleTextParser::addFilter() { - m_ruleFilterArray.append(m_ruleFilter); + m_ruleFilters.append(m_ruleFilter); resetFilter(); } int RuleTextParser::beginList(qint8 listType) { - const int nodeIndex = m_ruleFilterArray.size(); + const int nodeIndex = m_ruleFilters.size(); m_ruleFilter.type = listType; @@ -359,14 +359,14 @@ int RuleTextParser::beginList(qint8 listType) void RuleTextParser::endList(int nodeIndex) { - const int currentIndex = m_ruleFilterArray.size(); + const int currentIndex = m_ruleFilters.size(); - if (currentIndex == nodeIndex) { - m_ruleFilterArray.removeLast(); // Empty list + if (currentIndex == nodeIndex + 1) { + m_ruleFilters.removeLast(); // Empty list return; } - RuleFilter &ruleFilter = m_ruleFilterArray[nodeIndex]; + RuleFilter &ruleFilter = m_ruleFilters[nodeIndex]; ruleFilter.listCount = currentIndex - nodeIndex; } @@ -433,7 +433,6 @@ bool RuleTextParser::checkNextCharType(RuleCharTypes expectedCharTypes, const QC } if ((m_charType & expectedCharTypes) == 0) { - setErrorMessage(tr("Unexpected symbol: %1").arg(c)); return false; } diff --git a/src/ui/util/conf/ruletextparser.h b/src/ui/util/conf/ruletextparser.h index 755bf785..f7e4e172 100644 --- a/src/ui/util/conf/ruletextparser.h +++ b/src/ui/util/conf/ruletextparser.h @@ -26,8 +26,8 @@ enum RuleCharType : RuleCharTypes { CharNot = (1 << 13), // ! CharExtra = (1 << 14), // Name | Value // Complex types - CharAnyBegin = - (CharListBegin | CharBracketBegin | CharLetter | CharDigit | CharValueBegin | CharNot), + CharAnyBegin = (CharListBegin | CharListEnd | CharBracketBegin | CharLetter | CharDigit + | CharValueBegin | CharNot), CharName = (CharLetter | CharExtra), // a-zA-Z_ CharValue = (CharDigit | CharExtra), // 0-9.:-/ CharSpaceComment = (CharSpace | CharComment), @@ -63,14 +63,14 @@ public: bool hasError() const { return !errorMessage().isEmpty(); } - const QVector &ruleFilterArray() const { return m_ruleFilterArray; } + const QVector &ruleFilters() const { return m_ruleFilters; } bool parse(); private: void setErrorMessage(const QString &errorMessage) { m_errorMessage = errorMessage; } - void setupText(const QString &text); + void setupCharPtr(); void parseLines(); bool parseLine(); @@ -103,7 +103,7 @@ private: const QChar *currentCharPtr() const { return m_p; } const QChar *parsedCharPtr() const { return m_p - 1; } - RuleFilter &listNode(int listIndex) { return m_ruleFilterArray[listIndex]; } + RuleFilter &listNode(int listIndex) { return m_ruleFilters[listIndex]; } bool skipComments(RuleCharTypes expectedCharTypes); @@ -129,9 +129,10 @@ private: const QChar *m_p = nullptr; const QChar *m_end = nullptr; + QString m_text; QString m_errorMessage; - QVector m_ruleFilterArray; + QVector m_ruleFilters; }; #endif // RULETEXTPARSER_H