From 75ee9cec62706e909ae109274a99d896383c1b1b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 4 Sep 2015 22:18:04 +0200 Subject: [PATCH] Add QueryTest --- Makefile.in | 2 +- test/QueryTest.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 test/QueryTest.cpp diff --git a/Makefile.in b/Makefile.in index 3f4d5046..bd4d371a 100644 --- a/Makefile.in +++ b/Makefile.in @@ -54,7 +54,7 @@ BIN_SRCS := src/main.cpp LIB_OBJS := $(patsubst %cpp,%o,$(LIB_SRCS)) BIN_OBJS := $(patsubst %cpp,%o,$(BIN_SRCS)) TESTS := StringTest ConfigTest UtilsTest ThreadTest NickTest ClientTest NetworkTest \ - MessageTest ModulesTest IRCSockTest + MessageTest ModulesTest IRCSockTest QueryTest TESTS := $(addprefix test/,$(addsuffix .o,$(TESTS))) CLEAN := znc src/*.o test/*.o core core.* .version_extra .depend modules/.depend \ unittest $(LIBZNC) diff --git a/test/QueryTest.cpp b/test/QueryTest.cpp new file mode 100644 index 00000000..c9ea54aa --- /dev/null +++ b/test/QueryTest.cpp @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2004-2015 ZNC, see the NOTICE file for details. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using ::testing::SizeIs; +using ::testing::ElementsAre; +using ::testing::MatchesRegex; + +class QueryTest : public ::testing::Test { +protected: + void SetUp() { CZNC::CreateInstance(); } + void TearDown() { CZNC::DestroyInstance(); } +}; + +TEST_F(QueryTest, Name) { + CUser user("user"); + CIRCNetwork network(&user, "network"); + + CQuery query("query", &network); + EXPECT_EQ("query", query.GetName()); +} + +TEST_F(QueryTest, AddClearBuffer) { + CUser user("user"); + CIRCNetwork network(&user, "network"); + + CQuery query("query", &network); + EXPECT_TRUE(query.GetBuffer().IsEmpty()); + query.AddBuffer("foo"); + EXPECT_EQ(1, query.GetBuffer().Size()); + query.AddBuffer("bar"); + EXPECT_EQ(2, query.GetBuffer().Size()); + query.ClearBuffer(); + EXPECT_TRUE(query.GetBuffer().IsEmpty()); +} + +TEST_F(QueryTest, BufferSize) { + CUser user("user"); + CIRCNetwork network(&user, "network"); + + CQuery query("query", &network); + EXPECT_EQ(50, user.GetQueryBufferSize()); + EXPECT_EQ(50, query.GetBufferCount()); + + EXPECT_EQ(500, CZNC::Get().GetMaxBufferSize()); + EXPECT_FALSE(query.SetBufferCount(1000, false)); + EXPECT_EQ(50, query.GetBufferCount()); + EXPECT_TRUE(query.SetBufferCount(500, false)); + EXPECT_EQ(500, query.GetBufferCount()); + EXPECT_TRUE(query.SetBufferCount(1000, true)); + EXPECT_EQ(1000, query.GetBufferCount()); +} + +class TestClient : public CClient { +public: + bool Write(const CString& sData) override { + lines.push_back(sData); + return true; + } + VCString lines; +}; + +TEST_F(QueryTest, SendBuffer) { + CUser user("user"); + CIRCNetwork network(&user, "network"); + CDebug::SetDebug(false); + + TestClient client; + client.SetNick("me"); + client.AcceptLogin(user); + client.lines.clear(); + + CQuery query("query", &network); + query.AddBuffer(":sender PRIVMSG {target} :{text}", "a message"); + query.AddBuffer(":me PRIVMSG someone :{text}", "a self-message"); + query.AddBuffer(":sender NOTICE #znc :{text}", "a notice"); + + client.lines.clear(); + query.SendBuffer(&client); + EXPECT_THAT(client.lines, ElementsAre(MatchesRegex(R"(:sender PRIVMSG me :\[\d\d:\d\d:\d\d\] a message)"), + MatchesRegex(R"(:sender NOTICE #znc :\[\d\d:\d\d:\d\d\] a notice)"))); + + client.lines.clear(); + user.SetTimestampPrepend(false); + query.SendBuffer(&client); + EXPECT_THAT(client.lines, ElementsAre(":sender PRIVMSG me :a message", ":sender NOTICE #znc :a notice")); + + client.lines.clear(); + user.SetTimestampAppend(true); + query.SendBuffer(&client); + EXPECT_THAT(client.lines, ElementsAre(MatchesRegex(R"(:sender PRIVMSG me :a message \[\d\d:\d\d:\d\d\])"), + MatchesRegex(R"(:sender NOTICE #znc :a notice \[\d\d:\d\d:\d\d\])"))); + + network.ClientDisconnected(&client); +}