Skip to content

Commit

Permalink
进一步优化代码中的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
nekosilverfox committed Mar 1, 2024
1 parent ffbf445 commit 467dc4c
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
8 changes: 4 additions & 4 deletions App/dal_polychat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace DAL
* @return false in name or group have error,
* if not init local user and return true
*/
static bool initLocalUser(const QString name, const QString groupNumber)
static bool initLocalUser(const QString &name, const QString &groupNumber)
{
if (name.isEmpty() || groupNumber.isEmpty())
{
Expand All @@ -31,7 +31,7 @@ static QString getLocalUserName()
{
return BLL::getLocalUserName();
}
static void setLocalUserName(QString localUserName)
static void setLocalUserName(const QString &localUserName)
{
BLL::setLocalUserName(localUserName);
}
Expand All @@ -41,7 +41,7 @@ static QString getLocalUserGroupNumber()
{
return BLL::getLocalUserGroupNumber();
}
static void setLocalUserGroupNumber(QString localUserGroupNumber)
static void setLocalUserGroupNumber(const QString &localUserGroupNumber)
{
BLL::setLocalUserGroupNumber(localUserGroupNumber);
}
Expand All @@ -51,7 +51,7 @@ static QHostAddress getLocalIpAddress()
{
return BLL::getLocalIpAddress();
}
static void setLocalIpAddress(QHostAddress localIpAddress)
static void setLocalIpAddress(const QHostAddress &localIpAddress)
{
BLL::setLocalIpAddress(localIpAddress);
}
Expand Down
1 change: 1 addition & 0 deletions App/db_localdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "uil_chatlist.h"
#include "chat.h"


/**
* 在此修改配置
*/
Expand Down
3 changes: 3 additions & 0 deletions App/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include "tcpserver.h"

#include "db_localdata.h"

#define ENABLE_UNUSED_FUNCTION 0 // 是否编译未使用的函数

QString localUserName = ""; // User Name (will get in user login)
QString localUserGroupNumber = ""; // Group number (will get in user login)
QHostAddress localIpAddress = QHostAddress();
Expand Down
2 changes: 1 addition & 1 deletion App/tcpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void TcpClient::connectTcpServerAndOpenFile()
bool isFileOpen = file.open(QIODevice::WriteOnly);
if (!isFileOpen)
{
appendTextBrowser(Qt::red, QString("[ERROR] Can not write file %1 to file: %2").arg(fileName, savePath));
appendTextBrowser(Qt::red, QString("[ERROR] Can not write file %1 to %2").arg(fileName, savePath));
return;
}

Expand Down
7 changes: 5 additions & 2 deletions App/uil_chatlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QMessageBox>


ChatList::ChatList(QWidget* parent, QString localUserName, QString localUserGroupNumber, QHostAddress localIpAddress) :
QWidget(parent),
ui(new Ui::ChatList)
Expand Down Expand Up @@ -197,6 +198,7 @@ bool ChatList::isChatExist(const QString &name)
* @param name
* @return
*/
#if ENABLE_UNUSED_FUNCTION
bool ChatList::isChatExist_But_BtnNotExist(const QString &name)
{
if (!isChatExist(name)) return false;
Expand All @@ -210,6 +212,7 @@ bool ChatList::isChatExist_But_BtnNotExist(const QString &name)
return std::any_of(this->vPair_OChat_BtnChat.begin(), this->vPair_OChat_BtnChat.end(),
[&](const auto &pair) { return (name == pair.first->name) && (nullptr == pair.second); });
}
#endif

/** 查找一个端口号是否被占用
* @brief isPortExist
Expand Down Expand Up @@ -338,7 +341,7 @@ bool ChatList::setChatState(const QString &name, bool state)
return false;
}


#if ENABLE_UNUSED_FUNCTION
bool ChatList::updateBtnInvPair(const QString &name, QToolButton* btn)
{
// for (auto i : this->vPair_OChat_BtnChat)
Expand All @@ -364,7 +367,7 @@ bool ChatList::updateBtnInvPair(const QString &name, QToolButton* btn)
qDebug() << "[ERROR] Fail to update btn, ChatBox named" << name << "do not exits in local vPair_OChat_BtnChat";
return false;
}

#endif

/** 根据搜索框中的内容,并使用正则表达式,判断是否需要隐藏按钮
* @brief isNeedHideBtn
Expand Down
6 changes: 3 additions & 3 deletions App/uil_chatlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ChatList : public QWidget
void receiveMessage(); // 接收 UDP 消息

bool isPortExist(const qint16 port); // 查找一个端口号是否被占用
bool isChatExist_But_BtnNotExist(const QString &name);
bool isChatExist_But_BtnNotExist(const QString &name); // UNUSED
bool isChatExist(const QString &name); // 查找一个名称的群聊是否已经存在
bool isChatOpen(const QString &name);

Expand All @@ -38,13 +38,13 @@ class ChatList : public QWidget
qint16 getRandomPort(); //获取一个不重复的随机端口号

bool setChatState(const QString &name, bool state); // 设置聊天窗口为打开或者关闭
bool updateBtnInvPair(const QString &name, QToolButton* btn);
bool updateBtnInvPair(const QString &name, QToolButton* btn); // UNUSED

bool isNeedHideBtn(QString textOnBtn); // 根据正则表达式,判断是否需要隐藏按钮


private:
Ui::ChatList *ui;
Ui::ChatList* ui;

QUdpSocket* udpSocket;

Expand Down
3 changes: 2 additions & 1 deletion Tester/Tester.pro
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ FORMS += \
../App/tcpclient.ui \
../App/tcpserver.ui

QMAKE_CXXFLAGS += -w
QMAKE_CXXFLAGS += -w -fprofile-arcs -ftest-coverage
QMAKE_LFLAGS += -fprofile-arcs -ftest-coverage
14 changes: 9 additions & 5 deletions Tester/tst_PolyChatTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <QTableWidget>

#include "../App/db_localdata.h"
#include "../App/bll_polychat.h"
#include "../App/dal_polychat.h"
#include "../App/uil_loginwidget.h"
#include "../App/uil_addchat.h"
Expand All @@ -32,7 +31,7 @@ QString localUserGroupNumber = ""; // Group number (get in
QHostAddress localIpAddress = QHostAddress();
ChatList* chatList = nullptr; // Widget ChatList (Only one)

unsigned int const TIMER_STEP = 1000; // 对话框弹出市场,缩短此项只会影响展示效果。不会影响测试的最终结果
unsigned int const TIMER_STEP = 1000; // 对话框弹出时长,缩短此项只会影响展示效果。不会影响测试的最终结果

class PolyChatTester : public QObject
{
Expand Down Expand Up @@ -1375,8 +1374,10 @@ void PolyChatTester::mt_chatlist_getNewBtn()
QPair<Chat*, QToolButton*> pair(new Chat("3530904/90102", 6666, false), btn);
widget.vPair_OChat_BtnChat.push_front(pair);

#if ENABLE_UNUSED_FUNCTION
widget.updateBtnInvPair("3530904/90102", btn);
QCOMPARE(widget.isChatOpen("3530904/90102"), false);
#endif
}

/** 当增加新的聊天群组时,ui 界面正确刷新
Expand All @@ -1393,9 +1394,10 @@ void PolyChatTester::mt_chatlist_btnchat_exist()
QPair<Chat*, QToolButton*> pair(new Chat("3530904/90102", 6666, false), btn);
widget.vPair_OChat_BtnChat.push_front(pair);

#if ENABLE_UNUSED_FUNCTION
widget.updateBtnInvPair("3530904/90102", btn);
QCOMPARE(widget.isChatOpen("3530904/90102"), false);

#endif
}

/** 用户加入(用户列表正确刷新)
Expand Down Expand Up @@ -2104,8 +2106,10 @@ void PolyChatTester::e2e_join_chat()
QPair<Chat*, QToolButton*> pair(new Chat("3530904/90102", 6666, false), btn);
chatList.vPair_OChat_BtnChat.push_front(pair);

chatList.updateBtnInvPair("3530904/90102", btn);
QCOMPARE(chatList.isChatOpen("3530904/90102"), false);
#if ENABLE_UNUSED_FUNCTION
widget.updateBtnInvPair("3530904/90102", btn);
QCOMPARE(widget.isChatOpen("3530904/90102"), false);
#endif

QTest::mouseClick(btn, Qt::LeftButton);
}
Expand Down

0 comments on commit 467dc4c

Please sign in to comment.