Skip to content

Commit

Permalink
add(test): add test for get_valid_confg_file_path, use gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
ak0327 committed Aug 23, 2023
1 parent de90ce8 commit 5c114b8
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 3 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/gtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: GOOGLE_TEST
on: [push]
jobs:
run-google-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: build
run: |
cmake -S . -B build
cmake --build build
- name: run-all-test
run: |
cd build
ctest --rerun-failed --output-on-failure
- uses: sarisia/actions-status-discord@v1
if: always()
with:
webhook: ${{ secrets.DISCORD_WEBHOOK }}
status: ${{ job.status }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
!Makefile
!CPPLINT.cfg
!webserv.drawio
!CMakeLists.txt

.idea
.vscode
.DS_Store
CMakeLists.txt
CMakeCache.txt
54 changes: 54 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.23)
project(webserv)

set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


# google test ------------------------------------------------------------------
include(FetchContent)
include(GoogleTest)

FetchContent_Declare(
googletest
DOWNLOAD_EXTRACT_TIMESTAMP true
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
enable_testing()


# when you .cpp or .hpp is added, add the following
# include, webserv srcs, unit_test_srcs
# includes ---------------------------------------------------------------------
include_directories(
includes
)

# webserv_srcs -----------------------------------------------------------------
set(webserv_srcs
srcs/get_valid_config_file_path.cpp
)

#add_executable(webserv
# srcs/main.cpp
# ${webserv_srcs}
#)

# unit_test_srcs ---------------------------------------------------------------
set (unit_test_srcs
test/unit_test/is_valid_file_path/test_get_valid_config_file_path.cpp)

add_executable(unit_test
${webserv_srcs}
${unit_test_srcs}
)

# test -------------------------------------------------------------------------
target_link_libraries(
unit_test
GTest::gtest_main
)

gtest_discover_tests(unit_test)
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ re : fclean all
lint :
cpplint --recursive srcs

#.PHONY : unit
#unit :
# ./test/unit_test/run_unit_test.sh

.PHONY : unit
unit :
./test/unit_test/run_unit_test.sh

rm -rf build
cmake -S . -B build
cmake --build build
cd build && ctest

-include $(DEPS)
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "gtest/gtest.h"
#include "webserv.hpp"

TEST(TEST_get_valid_config_file_path, simple_test) {
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.CONF", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.CONF"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.Conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.Conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/file.conF", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file.conF"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/file"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/nothing.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/nothing"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/a.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/ a.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf "));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/ a.conf "));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/X.conf"));

EXPECT_EQ("../test/unit_test/is_valid_file_path/files/a.b.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.b.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/a.conf.b"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf.a"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/conf.a."));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)""));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"."));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)".."));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir.conf"));
}

TEST(TEST_get_valid_config_file_path, test_dir_rwx) {
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_rrr.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr.conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_rr-.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rr-.conf"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_r-r.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r-r.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-rr"));
EXPECT_EQ("../test/unit_test/is_valid_file_path/files/dir/file_r--.conf", get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r--.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-r-"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_--r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_---"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/this_is_dir.conf"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_rr-"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-rr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r-r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_-r-"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_--r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_---"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/file_r--"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/dir/this_is_dir"));

}

TEST(TEST_get_valid_config_file_path, test_dir_rw) {
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rrr.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rr-.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r-r.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-rr.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r--.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-r-.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_--r.conf"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_---.conf"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/thid_is_dir.conf"));

EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rrr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_rr-"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r-r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-rr"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_r--"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_-r-"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_--r"));
// EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/file_---"));
EXPECT_ANY_THROW(get_valid_config_file_path((char *)"../test/unit_test/is_valid_file_path/files/no_x_dir/thid_is_dir"));
}

0 comments on commit 5c114b8

Please sign in to comment.