mirror of
https://github.com/falk-werner/webfuse
synced 2024-10-27 20:34:10 +00:00
switched tests to google test
This commit is contained in:
parent
f47868ded7
commit
7f8365811f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
.vscode
|
.vscode
|
||||||
|
.build
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
cmake_minimum_required (VERSION 2.8)
|
cmake_minimum_required (VERSION 2.8)
|
||||||
project(fuse-wsfs)
|
project(fuse-wsfs)
|
||||||
|
|
||||||
|
option(WITH_TESTS "enable unit tests" OFF)
|
||||||
|
|
||||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||||
find_package(Threads REQUIRED)
|
find_package(Threads REQUIRED)
|
||||||
|
|
||||||
@ -63,14 +65,20 @@ target_include_directories(wsfs PUBLIC src ${EXTRA_INCLUDE_DIRS})
|
|||||||
target_compile_options(wsfs PUBLIC ${EXTRA_CFLAGS})
|
target_compile_options(wsfs PUBLIC ${EXTRA_CFLAGS})
|
||||||
|
|
||||||
# tests
|
# tests
|
||||||
|
|
||||||
|
if(WITH_TESTS)
|
||||||
|
|
||||||
|
pkg_check_modules(GTEST gtest_main)
|
||||||
|
|
||||||
add_executable(alltests
|
add_executable(alltests
|
||||||
test-src/test_main.c
|
test-src/test_response_parser.cc
|
||||||
test-src/test_util.c
|
|
||||||
test-src/test_response_parser.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(alltests PUBLIC fuse-wsfs ${EXTRA_LIBS})
|
target_link_libraries(alltests PUBLIC fuse-wsfs ${EXTRA_LIBS} ${GTEST_LIBRARIES})
|
||||||
target_include_directories(alltests PUBLIC src ${EXTRA_INCLUDE_DIRS})
|
target_include_directories(alltests PUBLIC src ${EXTRA_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS})
|
||||||
target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS})
|
target_compile_options(alltests PUBLIC ${EXTRA_CFLAGS} ${GTEST_CFLAGS})
|
||||||
|
|
||||||
|
enable_testing()
|
||||||
|
add_test(alltests alltests)
|
||||||
|
|
||||||
|
endif(WITH_TESTS)
|
@ -9,8 +9,9 @@ fuse-wsfs combines libwebsockets and libfuse. It allows ot attach a remote files
|
|||||||
cd fuse-wsfs
|
cd fuse-wsfs
|
||||||
mkdir ./build
|
mkdir ./build
|
||||||
cd ./build
|
cd ./build
|
||||||
cmake ..
|
cmake .. -DWITH_TESTS=ON
|
||||||
make
|
make
|
||||||
|
CTEST_OUTPUT_ON_FAILUE=1 make test
|
||||||
mkdir test
|
mkdir test
|
||||||
./wsfs -s -f --document_root=`realpath ../src/app/www` --port=4711 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/)
|
- [libfuse3](https://github.com/libfuse/libfuse/)
|
||||||
- [libwebsockets](https://libwebsockets.org/)
|
- [libwebsockets](https://libwebsockets.org/)
|
||||||
- [Jansson](https://jansson.readthedocs.io)
|
- [Jansson](https://jansson.readthedocs.io)
|
||||||
|
- [GoogleTest](https://github.com/google/googletest) *(optional)*
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#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;
|
|
||||||
}
|
|
@ -1,8 +1,9 @@
|
|||||||
#include "test_util.h"
|
#include <cstring>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
#include "wsfs/response_parser.h"
|
#include "wsfs/response_parser.h"
|
||||||
|
}
|
||||||
|
|
||||||
static void wsfs_response_parse_str(
|
static void wsfs_response_parse_str(
|
||||||
char const * buffer,
|
char const * buffer,
|
||||||
@ -12,7 +13,7 @@ static void wsfs_response_parse_str(
|
|||||||
wsfs_response_parse(buffer, length, response);
|
wsfs_response_parse(buffer, length, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_request_parser()
|
TEST(response_parser, test)
|
||||||
{
|
{
|
||||||
struct wsfs_response response;
|
struct wsfs_response response;
|
||||||
|
|
||||||
@ -20,43 +21,43 @@ void test_request_parser()
|
|||||||
wsfs_response_parse_str("", &response);
|
wsfs_response_parse_str("", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(-1, response.id);
|
ASSERT_EQ(-1, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// invalid json
|
// invalid json
|
||||||
wsfs_response_parse_str("invalid_json", &response);
|
wsfs_response_parse_str("invalid_json", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(-1, response.id);
|
ASSERT_EQ(-1, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// no object
|
// no object
|
||||||
wsfs_response_parse_str("[]", &response);
|
wsfs_response_parse_str("[]", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(-1, response.id);
|
ASSERT_EQ(-1, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// empty
|
// empty
|
||||||
wsfs_response_parse_str("{}", &response);
|
wsfs_response_parse_str("{}", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(-1, response.id);
|
ASSERT_EQ(-1, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// no data
|
// no data
|
||||||
wsfs_response_parse_str("{\"id\":42}", &response);
|
wsfs_response_parse_str("{\"id\":42}", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(42, response.id);
|
ASSERT_EQ(42, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// custom error code
|
// custom error code
|
||||||
wsfs_response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
wsfs_response_parse_str("{\"error\":{\"code\": 42}, \"id\": 42}", &response);
|
||||||
ASSERT_NE(WSFS_GOOD, response.status);
|
ASSERT_NE(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(42, response.status);
|
ASSERT_EQ(42, response.status);
|
||||||
ASSERT_EQ(42, response.id);
|
ASSERT_EQ(42, response.id);
|
||||||
ASSERT_EQ(NULL, response.result);
|
ASSERT_EQ(nullptr, response.result);
|
||||||
|
|
||||||
// valid response
|
// valid response
|
||||||
wsfs_response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
wsfs_response_parse_str("{\"result\": true, \"id\": 42}", &response);
|
||||||
ASSERT_EQ(WSFS_GOOD, response.status);
|
ASSERT_EQ(WSFS_GOOD, response.status);
|
||||||
ASSERT_EQ(42, response.id);
|
ASSERT_EQ(42, response.id);
|
||||||
ASSERT_NE(NULL, response.result);
|
ASSERT_NE(nullptr, response.result);
|
||||||
json_decref(response.result);
|
json_decref(response.result);
|
||||||
}
|
}
|
@ -1,15 +0,0 @@
|
|||||||
#include "test_util.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user