Tests: Add "tst_ruletextparser.h"

This commit is contained in:
Nodir Temirkhodjaev 2024-11-06 13:08:44 +05:00
parent 9242eb5788
commit 5b852bb78d
5 changed files with 48 additions and 18 deletions

View File

@ -7,6 +7,7 @@ HEADERS += \
tst_fileutil.h \
tst_ioccontainer.h \
tst_netutil.h \
tst_ruletextparser.h \
tst_stringutil.h
SOURCES += \

View File

@ -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 <QCoreApplication>

View File

@ -0,0 +1,28 @@
#pragma once
#include <QDebug>
#include <googletest.h>
#include <util/conf/ruletextparser.h>
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);
}

View File

@ -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;
}

View File

@ -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<RuleFilter> &ruleFilterArray() const { return m_ruleFilterArray; }
const QVector<RuleFilter> &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<RuleFilter> m_ruleFilterArray;
QVector<RuleFilter> m_ruleFilters;
};
#endif // RULETEXTPARSER_H