From 7f8365811fca2ec8a234b34f900b835c1fc53a80 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Wed, 30 Jan 2019 21:53:57 +0100 Subject: [PATCH] switched tests to google test --- .gitignore | 1 + CMakeLists.txt | 22 ++++++++----- README.md | 4 ++- test-src/test_main.c | 13 -------- ...ponse_parser.c => test_response_parser.cc} | 23 +++++++------- test-src/test_util.c | 15 --------- test-src/test_util.h | 31 ------------------- 7 files changed, 31 insertions(+), 78 deletions(-) delete mode 100644 test-src/test_main.c rename test-src/{test_response_parser.c => test_response_parser.cc} (78%) delete mode 100644 test-src/test_util.c delete mode 100644 test-src/test_util.h diff --git a/.gitignore b/.gitignore index 722d5e7..6d1d824 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +.build diff --git a/CMakeLists.txt b/CMakeLists.txt index 56f0c07..78c7082 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required (VERSION 2.8) project(fuse-wsfs) +option(WITH_TESTS "enable unit tests" OFF) + set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED) @@ -63,14 +65,20 @@ target_include_directories(wsfs PUBLIC src ${EXTRA_INCLUDE_DIRS}) target_compile_options(wsfs PUBLIC ${EXTRA_CFLAGS}) # tests - + +if(WITH_TESTS) + +pkg_check_modules(GTEST gtest_main) + add_executable(alltests - test-src/test_main.c - test-src/test_util.c - test-src/test_response_parser.c + test-src/test_response_parser.cc ) -target_link_libraries(alltests PUBLIC fuse-wsfs ${EXTRA_LIBS}) -target_include_directories(alltests PUBLIC src ${EXTRA_INCLUDE_DIRS}) -target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS}) +target_link_libraries(alltests PUBLIC fuse-wsfs ${EXTRA_LIBS} ${GTEST_LIBRARIES}) +target_include_directories(alltests PUBLIC src ${EXTRA_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}) +target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GTEST_CFLAGS}) +enable_testing() +add_test(alltests alltests) + +endif(WITH_TESTS) \ No newline at end of file diff --git a/README.md b/README.md index ca11da7..a9d61c4 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ fuse-wsfs combines libwebsockets and libfuse. It allows ot attach a remote files cd fuse-wsfs mkdir ./build cd ./build - cmake .. + cmake .. -DWITH_TESTS=ON make + CTEST_OUTPUT_ON_FAILUE=1 make test mkdir test ./wsfs -s -f --document_root=`realpath ../src/app/www` --port=4711 test @@ -20,5 +21,6 @@ fuse-wsfs combines libwebsockets and libfuse. It allows ot attach a remote files - [libfuse3](https://github.com/libfuse/libfuse/) - [libwebsockets](https://libwebsockets.org/) - [Jansson](https://jansson.readthedocs.io) +- [GoogleTest](https://github.com/google/googletest) *(optional)* diff --git a/test-src/test_main.c b/test-src/test_main.c deleted file mode 100644 index 0b3e468..0000000 --- a/test-src/test_main.c +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include - -#include "test_util.h" -#include "wsfs/util.h" - -extern void test_request_parser(); - -int main(int WSFS_UNUSED_PARAM(argc), char* WSFS_UNUSED_PARAM(argv[])) -{ - test_request_parser(); - return EXIT_SUCCESS; -} diff --git a/test-src/test_response_parser.c b/test-src/test_response_parser.cc similarity index 78% rename from test-src/test_response_parser.c rename to test-src/test_response_parser.cc index 06aff53..dd09bb4 100644 --- a/test-src/test_response_parser.c +++ b/test-src/test_response_parser.cc @@ -1,8 +1,9 @@ -#include "test_util.h" - -#include +#include +#include +extern "C" { #include "wsfs/response_parser.h" +} static void wsfs_response_parse_str( char const * buffer, @@ -12,7 +13,7 @@ static void wsfs_response_parse_str( wsfs_response_parse(buffer, length, response); } -void test_request_parser() +TEST(response_parser, test) { struct wsfs_response response; @@ -20,43 +21,43 @@ void test_request_parser() wsfs_response_parse_str("", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(-1, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // invalid json wsfs_response_parse_str("invalid_json", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(-1, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // no object wsfs_response_parse_str("[]", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(-1, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // empty wsfs_response_parse_str("{}", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(-1, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // no data wsfs_response_parse_str("{\"id\":42}", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(42, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // custom error code wsfs_response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response); ASSERT_NE(WSFS_GOOD, response.status); ASSERT_EQ(42, response.status); ASSERT_EQ(42, response.id); - ASSERT_EQ(NULL, response.result); + ASSERT_EQ(nullptr, response.result); // valid response wsfs_response_parse_str("{\"result\": true, \"id\": 42}", &response); ASSERT_EQ(WSFS_GOOD, response.status); ASSERT_EQ(42, response.id); - ASSERT_NE(NULL, response.result); + ASSERT_NE(nullptr, response.result); json_decref(response.result); } diff --git a/test-src/test_util.c b/test-src/test_util.c deleted file mode 100644 index b021c8d..0000000 --- a/test-src/test_util.c +++ /dev/null @@ -1,15 +0,0 @@ -#include "test_util.h" - -#include -#include - -void fail( - char const * file_name, - int line, - char const * message -) -{ - fprintf(stderr, "error: %s:%d: %s\n", file_name, line, message); - exit(EXIT_FAILURE); -} - diff --git a/test-src/test_util.h b/test-src/test_util.h deleted file mode 100644 index 28e6aff..0000000 --- a/test-src/test_util.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef _WSFS_TEST_UTIL_H -#define _WSFS_TEST_UTIL_H - -#define ASSERT_EQ(expected, actual) \ - do \ - { \ - if ((expected) != (actual)) \ - { \ - fail(__FILE__, __LINE__, "expected " #expected ", but was " #actual); \ - } \ - } \ - while (0) - -#define ASSERT_NE(expected, actual) \ - do \ - { \ - if ((expected) == (actual)) \ - { \ - fail(__FILE__, __LINE__, "expected " #expected ", but was " #actual); \ - } \ - } \ - while (0) - -extern void fail( - char const * file_name, - int line, - char const * message -); - -#endif -