Skip to content

Commit

Permalink
Test packaging (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
RaisinTen committed Apr 2, 2024
1 parent 69a53a6 commit cab4554
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ endif()

if(BENONI_TESTS)
enable_testing()
add_subdirectory(test)
add_subdirectory(test/unit)

if(PROJECT_IS_TOP_LEVEL)
add_subdirectory(test/packaging)
endif()
endif()
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ configure: .always
$(CMAKE) -B build -DBENONI_TESTS:BOOL=ON -DBENONI_EXAMPLES:BOOL=ON

build: .always
$(CLANG_FORMAT) --style=file -i include/benoni/http.h src/apple/http.mm src/win32/http.cc src/linux/http.cc examples/http_example.cc test/postman-echo-get.cc
$(CLANG_FORMAT) --style=file -i include/benoni/http.h src/apple/http.mm src/win32/http.cc src/linux/http.cc examples/http_example.cc test/unit/postman-echo-get.cc test/packaging/project/project.cc
$(CMAKE) --build build

example: .always
Expand Down
12 changes: 12 additions & 0 deletions test/packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_test(NAME packaging.project_configure COMMAND
"${CMAKE_COMMAND}"
-S "${CMAKE_CURRENT_SOURCE_DIR}/project"
-B "${CMAKE_CURRENT_BINARY_DIR}/project"
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}")

add_test(NAME packaging.project_build COMMAND
"${CMAKE_COMMAND}"
--build "${CMAKE_CURRENT_BINARY_DIR}/project")

set_tests_properties(packaging.project_build
PROPERTIES DEPENDS packaging.project_configure)
11 changes: 11 additions & 0 deletions test/packaging/project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.18)
project(project VERSION 0.0.1 LANGUAGES CXX DESCRIPTION "Project")

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(
"${PROJECT_SOURCE_DIR}/../../.."
"${CMAKE_CURRENT_BINARY_DIR}/benoni_build")

add_executable(project project.cc)
target_link_libraries(project PRIVATE benoni)
61 changes: 61 additions & 0 deletions test/packaging/project/project.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <benoni/http.h>

#if defined(__APPLE__)
#include <CoreFoundation/CoreFoundation.h>
#elif defined(linux)
#include <glib.h>
#endif

#include <iostream>
#include <variant>

using benoni::Method;
using benoni::request;
using benoni::RequestOptions;
using benoni::RequestOptionsBuilder;
using benoni::Response;

int main() {
#if defined(linux)
GMainLoop *loop = g_main_loop_new(nullptr, FALSE);
#endif

std::string url{"https://postman-echo.com/get"};
std::cout << "sending request to: \"" << url << "\"" << std::endl;

request(std::move(url), RequestOptionsBuilder{}.build(),
[](std::variant<std::string, Response> result) {
// This callback is run in a background thread on Windows and Apple
// and on the main thread on Gtk Linux.
if (std::holds_alternative<std::string>(result)) {
std::cerr << "error: [" << std::get<std::string>(result) << "]"
<< std::endl;
exit(EXIT_FAILURE);
}

Response response{std::get<Response>(result)};

std::cout << "response status: " << response.status << std::endl;
std::cout << "response headers: [" << std::endl;
for (const auto &[key, value] : response.headers) {
std::cout << " \"" << key << "\": \"" << value << "\","
<< std::endl;
}
std::cout << "]" << std::endl;
std::cout << "response body: \"" << response.body << "\""
<< std::endl;
exit(EXIT_SUCCESS);
});

#if defined(__APPLE__)
CFRunLoopRun();
#elif defined(linux)
g_main_loop_run(loop);
g_main_loop_unref(loop);
#else
while (true)
;
#endif

return 0;
}
File renamed without changes.
File renamed without changes.

0 comments on commit cab4554

Please sign in to comment.