doctest instead catch

This commit is contained in:
BlackMATov
2021-01-12 20:53:16 +07:00
parent d35917b9cd
commit 4dcf5dbc79
6 changed files with 6306 additions and 58 deletions

View File

@@ -19,7 +19,7 @@ endif()
# executable # executable
# #
file(GLOB UNTESTS_SOURCES "*.cpp" "*.hpp") file(GLOB_RECURSE UNTESTS_SOURCES "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES}) add_executable(${PROJECT_NAME} ${UNTESTS_SOURCES})
target_link_libraries(${PROJECT_NAME} curly.hpp) target_link_libraries(${PROJECT_NAME} curly.hpp)
@@ -33,23 +33,6 @@ target_compile_options(${PROJECT_NAME}
add_test(${PROJECT_NAME} ${PROJECT_NAME}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
#
# catchorg/catch2
#
include(FetchContent)
FetchContent_Declare(
catchorg_catch2
GIT_REPOSITORY https://github.com/catchorg/catch2
GIT_TAG v2.13.2)
FetchContent_GetProperties(catchorg_catch2)
if(NOT catchorg_catch2_POPULATED)
FetchContent_Populate(catchorg_catch2)
target_include_directories(${PROJECT_NAME}
PRIVATE ${catchorg_catch2_SOURCE_DIR}/single_include)
endif()
# #
# tencent/rapidjson # tencent/rapidjson
# #

View File

@@ -1,8 +0,0 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/curly.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

View File

@@ -4,14 +4,7 @@
* Copyright (C) 2019-2021, by Matvey Cherevko (blackmatov@gmail.com) * Copyright (C) 2019-2021, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/ ******************************************************************************/
#include <catch2/catch.hpp> #include "doctest/doctest.h"
#include <fstream>
#include <utility>
#include <iostream>
#include <rapidjson/document.h>
namespace json = rapidjson;
#include <curly.hpp/curly.hpp> #include <curly.hpp/curly.hpp>
namespace net = curly_hpp; namespace net = curly_hpp;
@@ -19,9 +12,16 @@ namespace net = curly_hpp;
#include <promise.hpp/promise.hpp> #include <promise.hpp/promise.hpp>
namespace netex = promise_hpp; namespace netex = promise_hpp;
#include <rapidjson/document.h>
namespace json = rapidjson;
#include "png_data.h" #include "png_data.h"
#include "jpeg_data.h" #include "jpeg_data.h"
#include <fstream>
#include <utility>
#include <iostream>
namespace namespace
{ {
json::Document json_parse(std::string_view data) { json::Document json_parse(std::string_view data) {
@@ -99,7 +99,7 @@ TEST_CASE("curly") {
net::performer performer; net::performer performer;
performer.wait_activity(net::time_ms_t(10)); performer.wait_activity(net::time_ms_t(10));
SECTION("wait") { SUBCASE("wait") {
{ {
auto req = net::request_builder("https://httpbin.org/delay/1").send(); auto req = net::request_builder("https://httpbin.org/delay/1").send();
REQUIRE(req.status() == net::req_status::pending); REQUIRE(req.status() == net::req_status::pending);
@@ -125,14 +125,14 @@ TEST_CASE("curly") {
} }
} }
SECTION("error") { SUBCASE("error") {
auto req = net::request_builder("|||").send(); auto req = net::request_builder("|||").send();
REQUIRE(req.wait() == net::req_status::failed); REQUIRE(req.wait() == net::req_status::failed);
REQUIRE(req.status() == net::req_status::failed); REQUIRE(req.status() == net::req_status::failed);
REQUIRE_FALSE(req.get_error().empty()); REQUIRE_FALSE(req.get_error().empty());
} }
SECTION("cancel") { SUBCASE("cancel") {
{ {
auto req = net::request_builder("https://httpbin.org/delay/1").send(); auto req = net::request_builder("https://httpbin.org/delay/1").send();
REQUIRE(req.cancel()); REQUIRE(req.cancel());
@@ -148,7 +148,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("is_done/is_pending") { SUBCASE("is_done/is_pending") {
{ {
auto req = net::request_builder(net::http_method::GET) auto req = net::request_builder(net::http_method::GET)
.url("https://httpbin.org/delay/1") .url("https://httpbin.org/delay/1")
@@ -173,7 +173,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("get") { SUBCASE("get") {
{ {
auto req = net::request_builder("https://httpbin.org/status/204").send(); auto req = net::request_builder("https://httpbin.org/status/204").send();
auto resp = req.take(); auto resp = req.take();
@@ -197,7 +197,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("http_methods") { SUBCASE("http_methods") {
{ {
auto req0 = net::request_builder() auto req0 = net::request_builder()
.url("https://httpbin.org/put") .url("https://httpbin.org/put")
@@ -326,7 +326,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("status_codes") { SUBCASE("status_codes") {
{ {
auto req = net::request_builder() auto req = net::request_builder()
.url("https://httpbin.org/status/200") .url("https://httpbin.org/status/200")
@@ -371,7 +371,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("request_inspection") { SUBCASE("request_inspection") {
{ {
auto resp = net::request_builder() auto resp = net::request_builder()
.url("https://httpbin.org/headers") .url("https://httpbin.org/headers")
@@ -426,7 +426,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("response_inspection") { SUBCASE("response_inspection") {
{ {
auto req = net::request_builder() auto req = net::request_builder()
.url("https://httpbin.org/response-headers?hello=world&world=hello") .url("https://httpbin.org/response-headers?hello=world&world=hello")
@@ -492,7 +492,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("dynamic_data") { SUBCASE("dynamic_data") {
{ {
auto req = net::request_builder() auto req = net::request_builder()
.url("https://httpbin.org/base64/SFRUUEJJTiBpcyBhd2Vzb21l") .url("https://httpbin.org/base64/SFRUUEJJTiBpcyBhd2Vzb21l")
@@ -533,7 +533,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("binary") { SUBCASE("binary") {
{ {
auto resp = net::request_builder() auto resp = net::request_builder()
.url("https://httpbin.org/bytes/5") .url("https://httpbin.org/bytes/5")
@@ -635,7 +635,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("redirects") { SUBCASE("redirects") {
{ {
{ {
auto req = net::request_builder() auto req = net::request_builder()
@@ -695,7 +695,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("request_body") { SUBCASE("request_body") {
{ {
auto resp = net::request_builder() auto resp = net::request_builder()
.url("https://httpbin.org/anything") .url("https://httpbin.org/anything")
@@ -749,7 +749,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("ssl_verification") { SUBCASE("ssl_verification") {
{ {
auto req0 = net::request_builder("https://expired.badssl.com") auto req0 = net::request_builder("https://expired.badssl.com")
.method(net::http_method::HEAD) .method(net::http_method::HEAD)
@@ -802,7 +802,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("cancelled_handlers") { SUBCASE("cancelled_handlers") {
{ {
auto req = net::request_builder("https://httpbin.org/anything") auto req = net::request_builder("https://httpbin.org/anything")
.verbose(true) .verbose(true)
@@ -829,7 +829,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("callback") { SUBCASE("callback") {
{ {
std::atomic_size_t call_once{0u}; std::atomic_size_t call_once{0u};
auto req = net::request_builder("http://www.httpbin.org/get") auto req = net::request_builder("http://www.httpbin.org/get")
@@ -890,7 +890,7 @@ TEST_CASE("curly") {
} }
} }
SECTION("callback_exception") { SUBCASE("callback_exception") {
auto req = net::request_builder("http://www.httpbin.org/post") auto req = net::request_builder("http://www.httpbin.org/post")
.callback([](net::request request){ .callback([](net::request request){
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(10));
@@ -911,7 +911,7 @@ TEST_CASE("curly") {
TEST_CASE("curly_examples") { TEST_CASE("curly_examples") {
net::performer performer; net::performer performer;
SECTION("Get Requests") { SUBCASE("Get Requests") {
// makes a GET request and async send it // makes a GET request and async send it
auto request = net::request_builder() auto request = net::request_builder()
.method(net::http_method::GET) .method(net::http_method::GET)
@@ -940,7 +940,7 @@ TEST_CASE("curly_examples") {
// } // }
} }
SECTION("Post Requests") { SUBCASE("Post Requests") {
auto request = net::request_builder() auto request = net::request_builder()
.method(net::http_method::POST) .method(net::http_method::POST)
.url("http://www.httpbin.org/post") .url("http://www.httpbin.org/post")
@@ -973,7 +973,7 @@ TEST_CASE("curly_examples") {
// Content Length: 389 // Content Length: 389
} }
SECTION("Query Parameters") { SUBCASE("Query Parameters") {
auto request = net::request_builder() auto request = net::request_builder()
.url("http://httpbin.org/anything") .url("http://httpbin.org/anything")
.qparam("hello", "world") .qparam("hello", "world")
@@ -985,7 +985,7 @@ TEST_CASE("curly_examples") {
// Last URL: http://httpbin.org/anything?hello=world // Last URL: http://httpbin.org/anything?hello=world
} }
SECTION("Error Handling") { SUBCASE("Error Handling") {
auto request = net::request_builder() auto request = net::request_builder()
.url("http://unavailable.site.com") .url("http://unavailable.site.com")
.send(); .send();
@@ -1005,7 +1005,7 @@ TEST_CASE("curly_examples") {
// Error message: Could not resolve host: unavailable.site.com // Error message: Could not resolve host: unavailable.site.com
} }
SECTION("Request Callbacks") { SUBCASE("Request Callbacks") {
auto request = net::request_builder("http://www.httpbin.org/get") auto request = net::request_builder("http://www.httpbin.org/get")
.callback([](net::request request){ .callback([](net::request request){
if ( request.is_done() ) { if ( request.is_done() ) {
@@ -1020,7 +1020,7 @@ TEST_CASE("curly_examples") {
// Status code: 200 // Status code: 200
} }
SECTION("Streamed Requests") { SUBCASE("Streamed Requests") {
{ {
class file_dowloader : public net::download_handler { class file_dowloader : public net::download_handler {
public: public:
@@ -1071,7 +1071,7 @@ TEST_CASE("curly_examples") {
} }
} }
SECTION("Promised Requests") { SUBCASE("Promised Requests") {
auto promise = download("https://httpbin.org/image/png") auto promise = download("https://httpbin.org/image/png")
.then([](const net::content_t& content){ .then([](const net::content_t& content){
std::cout << content.size() << " bytes downloaded" << std::endl; std::cout << content.size() << " bytes downloaded" << std::endl;

View File

@@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

6260
untests/doctest/doctest.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
#pragma once
#include "doctest.h"
#define STATIC_REQUIRE(...)\
static_assert(__VA_ARGS__, #__VA_ARGS__);\
REQUIRE(__VA_ARGS__);
#define STATIC_REQUIRE_FALSE(...)\
static_assert(!(__VA_ARGS__), "!(" #__VA_ARGS__ ")");\
REQUIRE(!(__VA_ARGS__));