Skip to content

Commit

Permalink
Merge pull request #17 from st235/fix/build_interface_compile_options
Browse files Browse the repository at this point in the history
Hide compilation of tests under a flag, move -Werror compilation options to test target to do not affect library clients
  • Loading branch information
st235 authored Jul 26, 2024
2 parents ea53dda + f56db22 commit d1d850b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 55 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build-and-run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
with:
source-dir: ${{github.workspace}}
build-dir: ${{github.workspace}}/build/
options: ASSERT=ON
options: |
COMPILE_TESTS=ON
ASSERT=ON
- name: Run tests
working-directory: ${{github.workspace}}/build/
Expand Down
116 changes: 63 additions & 53 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ cmake_minimum_required(VERSION 3.26)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(JSONC VERSION 0.9.3 DESCRIPTION "JSONC is a lightweight implementation of JSON Data Interchange Standard for C++ programming language.")
project(JSONC VERSION 0.9.4 DESCRIPTION "JSONC is a lightweight implementation of JSON Data Interchange Standard for C++ programming language.")

# Library targets.

add_library(jsonc INTERFACE)

# Enable all possible warnings and treat them as errors.
if(MSVC)
target_compile_options(jsonc INTERFACE /W4 /WX)
else()
target_compile_options(jsonc INTERFACE -Wall -Wextra -Wpedantic -Werror)
endif()

# Enable assertions statements in the codebase.
option(ASSERT "Asserts are enabled when turned on." OFF)
if (ASSERT)
message("Enabling assertions in the code.")
add_compile_definitions(ASSERT=${ASSERT})
Expand All @@ -32,49 +26,65 @@ target_sources(jsonc INTERFACE
)
target_include_directories(jsonc INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)

# Test targets.

include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# Compile test targets, disabled by default.
option(COMPILE_TESTS "Compiled with tests when turned on." OFF)

if (COMPILE_TESTS)
message("Compiling tests.")

# Test targets.
include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings.
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(jsonc_tests
# Utils.
tests/integration/test_utils.h

# Unit tests.
# Testing types.
tests/unit/json_null_tests.cpp
tests/unit/json_boolean_tests.cpp
tests/unit/json_number_tests.cpp
tests/unit/json_string_tests.cpp
tests/unit/json_array_tests.cpp
tests/unit/json_object_tests.cpp
# Internal types tests.
tests/unit/json_token_reader_tests.cpp

# Integration tests.
tests/integration/json_dump_tests.cpp
tests/integration/json_malformed_json_tests.cpp
tests/integration/json_valid_json_tests.cpp
# Visitors.
tests/integration/json_beautifier_tests.cpp
tests/integration/json_minifier_tests.cpp
)

target_link_libraries(jsonc_tests PRIVATE jsonc)
# GMock is required to use EXPECT_THAT.
target_link_libraries(jsonc_tests PRIVATE GTest::gtest_main GTest::gmock_main)
target_include_directories(jsonc_tests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)

# Enable all possible warnings and treat them as errors.
# This check only affects tests builds, as if applied to
# an INTERFACE library, will affect clients of the library.
if(MSVC)
target_compile_options(jsonc_tests PRIVATE /W4 /WX)
else()
target_compile_options(jsonc_tests PRIVATE -Wall -Wextra -Wpedantic -Werror)
endif()

include(GoogleTest)
gtest_discover_tests(jsonc_tests)

# For Windows: Prevent overriding the parent project's compiler/linker settings.
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

enable_testing()

add_executable(jsonc_tests
# Utils.
tests/integration/test_utils.h

# Unit tests.
# Testing types.
tests/unit/json_null_tests.cpp
tests/unit/json_boolean_tests.cpp
tests/unit/json_number_tests.cpp
tests/unit/json_string_tests.cpp
tests/unit/json_array_tests.cpp
tests/unit/json_object_tests.cpp
# Internal types tests.
tests/unit/json_token_reader_tests.cpp

# Integration tests.
tests/integration/json_dump_tests.cpp
tests/integration/json_malformed_json_tests.cpp
tests/integration/json_valid_json_tests.cpp
# Visitors.
tests/integration/json_beautifier_tests.cpp
tests/integration/json_minifier_tests.cpp
)

target_link_libraries(jsonc_tests PRIVATE jsonc)
# GMock is required to use EXPECT_THAT.
target_link_libraries(jsonc_tests PRIVATE GTest::gtest_main GTest::gmock_main)
target_include_directories(jsonc_tests PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)

include(GoogleTest)
gtest_discover_tests(jsonc_tests)
endif()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Use these commands to build the project:

```bash
mkdir build
cmake .. -DASSERT=ON
cmake .. -DCOMPILE_TESTS=ON -DASSERT=ON
make
```

Expand Down

0 comments on commit d1d850b

Please sign in to comment.