summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOleg Morozenkov <reo7sp@users.noreply.github.com>2017-01-15 02:01:22 +0300
committerOleg Morozenkov <reo7sp@users.noreply.github.com>2017-01-15 02:01:22 +0300
commitad676623168e7b7b083f66617087c6cf424000d1 (patch)
tree64a893a0c17e2a29c227ebdb53076e40ace19af0
parentbbf030e1d315d6ba108a75a5ce0113485a2319ba (diff)
Fix tests
l---------.dockerignore1
-rw-r--r--.gitignore9
-rw-r--r--.travis.yml3
-rw-r--r--CMakeLists.txt36
-rw-r--r--Dockerfile12
-rw-r--r--Dockerfile_test27
-rw-r--r--README.md138
-rw-r--r--src/net/HttpParser.cpp2
-rw-r--r--src/net/Url.cpp5
-rw-r--r--src/tools/StringTools.cpp2
-rw-r--r--test/tgbot/net/HttpParser.cpp9
-rw-r--r--test/tgbot/tools/StringTools.cpp10
-rw-r--r--test/utils.h45
13 files changed, 169 insertions, 130 deletions
diff --git a/.dockerignore b/.dockerignore
new file mode 120000
index 0000000..3e4e48b
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1 @@
+.gitignore \ No newline at end of file
diff --git a/.gitignore b/.gitignore
index 8006986..01735f9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,11 @@
CMakeCache.txt
-CMakeFiles/
+CMakeFiles
Makefile
-cmake_install.cmake
+*.cmake
install_manifest.txt
.idea/
Thumbs.db
-doc/ \ No newline at end of file
+doc/
+*.cbp
+*.a
+test/TgBot_test \ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index 5b6de8c..47656fe 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,4 +4,5 @@ services:
- docker
script:
- - docker build -t reo7sp/tgbot-cpp .
+ - docker build -t reo7sp/tgbot-cpp -f Dockerfile_test .
+ - docker run --rm reo7sp/tgbot-cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c13a633..9acb2f0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,15 +8,15 @@ option(ENABLE_TESTS "Set to ON to enable building of tests" OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(include)
set(SRC_LIST
- src/Api.cpp
- src/TgTypeParser.cpp
- src/TgException.cpp
- src/net/Url.cpp
- src/net/HttpClient.cpp
- src/net/HttpParser.cpp
- src/net/TgLongPoll.cpp
- src/tools/StringTools.cpp
- src/types/InlineQueryResult.cpp)
+ src/Api.cpp
+ src/TgTypeParser.cpp
+ src/TgException.cpp
+ src/net/Url.cpp
+ src/net/HttpClient.cpp
+ src/net/HttpParser.cpp
+ src/net/TgLongPoll.cpp
+ src/tools/StringTools.cpp
+ src/types/InlineQueryResult.cpp)
### libs
# threads
@@ -28,13 +28,17 @@ include_directories(${OPENSSL_INCLUDE_DIR})
# boost
set(Boost_USE_MULTITHREADED ON)
-find_package(Boost COMPONENTS system iostreams unit_test_framework REQUIRED)
+if (ENABLE_TESTS)
+ find_package(Boost COMPONENTS system iostreams unit_test_framework REQUIRED)
+else()
+ find_package(Boost COMPONENTS system iostreams REQUIRED)
+endif()
include_directories(${Boost_INCLUDE_DIR})
set(LIB_LIST
- ${CMAKE_THREAD_LIBS_INIT}
- ${OPENSSL_LIBRARIES}
- ${Boost_LIBRARIES}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${OPENSSL_LIBRARIES}
+ ${Boost_LIBRARIES}
)
### building project
@@ -45,7 +49,7 @@ install(DIRECTORY include/ DESTINATION include)
### tests
if (ENABLE_TESTS)
- message(STATUS "Building of tests is enabled")
- enable_testing()
- add_subdirectory(test)
+ message(STATUS "Building of tests is enabled")
+ enable_testing()
+ add_subdirectory(test)
endif()
diff --git a/Dockerfile b/Dockerfile
index 9e37311..1b5b9c3 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,15 +1,15 @@
FROM debian:latest
-MAINTAINER Oleg Morozenkov
+MAINTAINER Oleg Morozenkov <a@reo7sp.ru>
RUN apt-get -qq update && \
- apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev libboost-test-dev
+ apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev
WORKDIR /tmp/tgbot-cpp
COPY include include
COPY src src
COPY CMakeLists.txt ./
-RUN \
- cmake . && \
+
+RUN cmake . && \
make -j4 && \
- make install && \
- rm -rf /tmp/tgbot-cpp
+ make install && \
+ rm -rf /tmp/tgbot-cpp/*
diff --git a/Dockerfile_test b/Dockerfile_test
new file mode 100644
index 0000000..0d6f409
--- /dev/null
+++ b/Dockerfile_test
@@ -0,0 +1,27 @@
+FROM debian:latest
+MAINTAINER Oleg Morozenkov <a@reo7sp.ru>
+
+RUN apt-get -qq update && \
+ apt-get -qq install -y g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev libboost-test-dev
+
+WORKDIR /tmp/tgbot-cpp
+COPY include include
+COPY src src
+COPY test test
+COPY samples samples
+COPY CMakeLists.txt ./
+
+RUN cmake -DENABLE_TESTS=ON . && \
+ make -j4 && \
+ make install
+
+WORKDIR /tmp/tgbot-cpp/samples/echobot
+RUN rm -rf CMakeCache.txt CMakeFiles/ && \
+ cmake . && make -j4
+
+WORKDIR /tmp/tgbot-cpp/samples/inline-keyboard
+RUN rm -rf CMakeCache.txt CMakeFiles/ && \
+ cmake . && make -j4
+
+WORKDIR /tmp/tgbot-cpp
+CMD make test
diff --git a/README.md b/README.md
index 24bd589..21973b8 100644
--- a/README.md
+++ b/README.md
@@ -1,69 +1,69 @@
-# tgbot-cpp
-
-[![Build Status](https://travis-ci.org/reo7sp/tgbot-cpp.svg?branch=master)](https://travis-ci.org/reo7sp/tgbot-cpp)
-[![Join the chat at https://gitter.im/reo7sp/tgbot-cpp](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/reo7sp/tgbot-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
-[![Donate using Yandex.Money](https://i.imgur.com/8Fi2DnS.png)](https://money.yandex.ru/to/410011554060830)
-
-C++ library for Telegram bot API.
-
-Documentaion is located [here](http://reo7sp.github.io/tgbot-cpp)
-
-## Compilation
-
-Firstly you need to install some dependencies such as Boost and build tools such as CMake. On Debian-based distibutives you can do it with these commands:
-```sh
-sudo apt-get install g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev libboost-test-dev
-```
-
-To compile the library execute this commands:
-```sh
-cd /path/where/you/have/cloned/the/library/repository
-cmake .
-make -j4
-sudo make install
-```
-
-That's all. All you have to do now is just link compiled library to your project.
-
-If you want, you can also use Docker to build and run your bot. Just set the base image of your's Dockerfile to [reo7sp/tgbot-cpp](https://hub.docker.com/r/reo7sp/tgbot-cpp/).
-
-## Samples
-
-Simple echo bot which sends everything it recieves:
-```cpp
-#include <stdio.h>
-#include <tgbot/tgbot.h>
-
-int main() {
- TgBot::Bot bot("PLACE YOUR TOKEN HERE");
- bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
- bot.getApi().sendMessage(message->chat->id, "Hi!");
- });
- bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
- printf("User wrote %s\n", message->text.c_str());
- if (StringTools::startsWith(message->text, "/start")) {
- return;
- }
- bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
- });
- try {
- printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
- TgBot::TgLongPoll longPoll(bot);
- while (true) {
- printf("Long poll started\n");
- longPoll.start();
- }
- } catch (TgBot::TgException& e) {
- printf("error: %s\n", e.what());
- }
- return 0;
-}
-```
-
-All samples are located [here](samples)
-
-## Feedback
-Feel free to [create new issues on GitHub](https://github.com/reo7sp/tgbot-cpp/issues) or [contact me on Telegram](https://telegram.me/reo7sp)
-
-## Licence
-[The MIT License](http://opensource.org/licenses/MIT)
+# tgbot-cpp
+
+[![Build Status](https://travis-ci.org/reo7sp/tgbot-cpp.svg?branch=master)](https://travis-ci.org/reo7sp/tgbot-cpp)
+[![Join the chat at https://gitter.im/reo7sp/tgbot-cpp](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/reo7sp/tgbot-cpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
+[![Donate using Yandex.Money](https://i.imgur.com/8Fi2DnS.png)](https://money.yandex.ru/to/410011554060830)
+
+C++ library for Telegram bot API.
+
+Documentation is located [here](http://reo7sp.github.io/tgbot-cpp/docs)
+
+## Compilation
+
+Firstly you need to install some dependencies such as Boost and build tools such as CMake. On Debian-based distibutives you can do it with these commands:
+```sh
+sudo apt-get install g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev
+```
+
+To compile the library execute this commands:
+```sh
+cd /path/where/you/have/cloned/the/library/repository
+cmake .
+make -j4
+sudo make install
+```
+
+That's all. All you have to do now is just link compiled library to your project.
+
+If you want, you can also use Docker to build and run your bot. Just set the base image of your's Dockerfile to [reo7sp/tgbot-cpp](https://hub.docker.com/r/reo7sp/tgbot-cpp/).
+
+## Samples
+
+Simple echo bot which sends everything it receives:
+```cpp
+#include <stdio.h>
+#include <tgbot/tgbot.h>
+
+int main() {
+ TgBot::Bot bot("PLACE YOUR TOKEN HERE");
+ bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
+ bot.getApi().sendMessage(message->chat->id, "Hi!");
+ });
+ bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
+ printf("User wrote %s\n", message->text.c_str());
+ if (StringTools::startsWith(message->text, "/start")) {
+ return;
+ }
+ bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
+ });
+ try {
+ printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
+ TgBot::TgLongPoll longPoll(bot);
+ while (true) {
+ printf("Long poll started\n");
+ longPoll.start();
+ }
+ } catch (TgBot::TgException& e) {
+ printf("error: %s\n", e.what());
+ }
+ return 0;
+}
+```
+
+All samples are located [here](samples)
+
+## Feedback
+Feel free to [create new issues on GitHub](https://github.com/reo7sp/tgbot-cpp/issues) or [contact me on Telegram](https://t.me/reo7sp)
+
+## Licence
+[The MIT License](http://opensource.org/licenses/MIT)
diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp
index 4b57bc1..befbc3b 100644
--- a/src/net/HttpParser.cpp
+++ b/src/net/HttpParser.cpp
@@ -99,7 +99,7 @@ string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, con
result += item.value;
result += "\r\n";
}
- result += "--" + bondary + "--";
+ result += "--" + bondary + "--\r\n";
return result;
}
diff --git a/src/net/Url.cpp b/src/net/Url.cpp
index 44d9089..005506c 100644
--- a/src/net/Url.cpp
+++ b/src/net/Url.cpp
@@ -75,11 +75,6 @@ Url::Url(const string& url) {
fragment += c;
}
}
-
- host = StringTools::urlEncode(host, ".");
- path = StringTools::urlEncode(path, "/");
- query = StringTools::urlEncode(query, "&");
- fragment = StringTools::urlEncode(fragment);
}
}
diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp
index d5270db..a48b4eb 100644
--- a/src/tools/StringTools.cpp
+++ b/src/tools/StringTools.cpp
@@ -56,6 +56,8 @@ bool endsWith(const string& str1, const string& str2) {
string::const_iterator begin1(str1.begin());
string::const_iterator it2(str2.end());
string::const_iterator begin2(str2.begin());
+ --begin1;
+ --begin2;
while (it1 != begin1 && it2 != begin2) {
if (*it1 != *it2) {
return false;
diff --git a/test/tgbot/net/HttpParser.cpp b/test/tgbot/net/HttpParser.cpp
index 236eb90..9272736 100644
--- a/test/tgbot/net/HttpParser.cpp
+++ b/test/tgbot/net/HttpParser.cpp
@@ -41,7 +41,7 @@ BOOST_AUTO_TEST_CASE(generateRequest) {
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 49\r\n"
"\r\n"
- "email=test%40example.com&text=Hello%2c%20world%21";
+ "email=test%40example.com&text=Hello%2C%20world%21";
BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
@@ -55,17 +55,18 @@ BOOST_AUTO_TEST_CASE(generateMultipartFormData) {
"\r\n"
"test@example.com\r\n"
"--" + boundary + "\r\n"
- "Content-Disposition: form-data; name=\"text\"\r\n"
+ "Content-Disposition: form-data; name=\"text\"; filename=\"\"\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
- "Hello, world!\r\n";
+ "Hello, world!\r\n"
+ "--" + boundary + "--\r\n";
BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
BOOST_AUTO_TEST_CASE(generateWwwFormUrlencoded) {
vector<HttpReqArg> args = { HttpReqArg("email", "test@example.com"), HttpReqArg("text", "Hello, world!") };
string t = HttpParser::getInstance().generateWwwFormUrlencoded(args);
- string e = "email=test%40example.com&text=Hello%2c%20world%21";
+ string e = "email=test%40example.com&text=Hello%2C%20world%21";
BOOST_CHECK_MESSAGE(t == e, diffS(t, e));
}
diff --git a/test/tgbot/tools/StringTools.cpp b/test/tgbot/tools/StringTools.cpp
index 453919c..81e469a 100644
--- a/test/tgbot/tools/StringTools.cpp
+++ b/test/tgbot/tools/StringTools.cpp
@@ -36,12 +36,18 @@ BOOST_AUTO_TEST_SUITE(tStringTools)
BOOST_AUTO_TEST_CASE(startsWith) {
BOOST_CHECK(StringTools::startsWith("abc123", "abc"));
- BOOST_CHECK(StringTools::startsWith("abc", "abc123"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "aac"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "Xabc"));
+ BOOST_CHECK(!StringTools::startsWith("abc123", "abcX"));
+ BOOST_CHECK(!StringTools::startsWith("abc", "abc123"));
}
BOOST_AUTO_TEST_CASE(endsWith) {
BOOST_CHECK(StringTools::endsWith("abc123", "123"));
- BOOST_CHECK(StringTools::endsWith("123", "abc123"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "113"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "X123"));
+ BOOST_CHECK(!StringTools::endsWith("abc123", "123X"));
+ BOOST_CHECK(!StringTools::endsWith("123", "abc123"));
}
BOOST_AUTO_TEST_CASE(split) {
diff --git a/test/utils.h b/test/utils.h
index abd2f07..93f64ff 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -29,30 +29,8 @@
#include <boost/lexical_cast.hpp>
-std::string diffS(const std::string& test, const std::string& expected) {
- std::vector<std::string> v1, v2;
- std::istringstream ss1(test);
- std::istringstream ss2(expected);
- std::string s1, s2;
- bool r1, r2;
- do {
- r1 = std::getline(ss1, s1) ? true : false;
- r2 = std::getline(ss2, s2) ? true : false;
- if (r1) {
- v1.push_back(s1);
- }
- if (r2) {
- v2.push_back(s2);
- }
- } while (r1 || r2);
- return "";
- //return diff(v1, v2, [](const std::string& item) {
- //return item;
- //});
-}
-
template<typename T>
-std::string diff(const T& test, const T& expected, std::string (*toStringFunc)(const typename T::value_type&)) {
+inline std::string diff(const T& test, const T& expected, std::string (*toStringFunc)(const typename T::value_type&)) {
std::string result;
result += "\n*** BEGIN *** Count: t=";
result += boost::lexical_cast<std::string>(test.size());
@@ -102,4 +80,25 @@ std::string diff(const T& test, const T& expected, std::string (*toStringFunc)(c
return result;
}
+inline std::string diffS(const std::string& test, const std::string& expected) {
+ std::vector<std::string> v1, v2;
+ std::istringstream ss1(test);
+ std::istringstream ss2(expected);
+ std::string s1, s2;
+ bool r1, r2;
+ do {
+ r1 = std::getline(ss1, s1) ? true : false;
+ r2 = std::getline(ss2, s2) ? true : false;
+ if (r1) {
+ v1.push_back(s1);
+ }
+ if (r2) {
+ v2.push_back(s2);
+ }
+ } while (r1 || r2);
+ return diff(v1, v2, [](const std::string& item) {
+ return item;
+ });
+}
+
#endif //TGBOT_UTILS_H