Skip to content

Commit

Permalink
Add automated testing using doctest (microsoft#55)
Browse files Browse the repository at this point in the history
* Add automated testing using doctest

This change adds basic unit testing for some simple scenarios in the IFC.

* Added [`doctest`](https://github.com/doctest/doctest) as a dependency for unit testing the C++ SDK.
* Added preset value for building the testing executables.
* Added unit tests for MSVC.
* Improved documentation for building in developer mode.
  • Loading branch information
cdacamar authored Jan 3, 2024
1 parent 18a4320 commit 489d177
Show file tree
Hide file tree
Showing 9 changed files with 946 additions and 3 deletions.
32 changes: 32 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,38 @@ cmake --build build --config Release

If you are using vcpkg as your C++ package manager, you might need to add `-DCMAKE_TOOLCHAIN_FILE=<path-to-your-vcpkd-root>/scripts/buildsystems/vcpkg.cmake` to the CMake command in the configuration step.

### Unit Testing

For configuring with testing enabled (only MSVC is enabled for now):

```sh
cmake -B build --preset=test-msvc
cmake --build build/test
cd build
ctest -C debug test
```

### Using vcpkg (for testing)

This project depends on [`doctest`](https://github.com/doctest/doctest) for validating the SDK. We recommend using [`vcpkg`](https://vcpkg.io) for managing this dependency. This project does not provide a [`builtin-baseline`](https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-json#builtin-baseline) in the `vcpkg.json` intentionally so that system dependencies can be relied on. If you are not using `vcpkg` in [classic mode](https://learn.microsoft.com/en-us/vcpkg/users/classic-mode) then you must introduce your own baseline (either through [`vcpkg x-update-baseline`](https://learn.microsoft.com/en-us/vcpkg/commands/update-baseline)) or add a custom [`vcpkg-configuration.json`](https://learn.microsoft.com/en-us/vcpkg/reference/vcpkg-configuration-json). Here's an example of using the `x-update-baseline` method:

```sh
vcpkg x-update-baseline --add-initial-baseline
cmake -B build -DCMAKE_TOOLCHAIN_FILE=<path-to>/vcpkg.cmake --preset=test-msvc
```

Note: Using this method will locally modify `/vcpkg.json` so be sure to revert it or exclude it from your commit before submitting any change for PR.

On Linux platforms please consult your package manager for alternative distributions of the C++ `doctest` framework. Some common alternatives:

- arch: `doctest`
- debian: `doctest-dev`
- fedora: `doctest`
- gentoo: `dev-cpp/doctest`
- ubuntu: `doctest-dev`

When using an alternative source for packages be sure to consult your distribution documentation on how to make that package available for CMake.

## Install

Here is the command for installing the release mode artifacts with a
Expand Down
4 changes: 4 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
{
"name": "ci-windows",
"inherits": ["ci-build", "ci-win64", "dev-mode", "vcpkg", "vcpkg-win64-static"]
},
{
"name": "test-msvc",
"inherits": ["ci-build", "ci-win64", "dev-mode", "vcpkg", "vcpkg-win64-static"]
}
]
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ This will create an IFC file named test.cpp.ifc. This can be dumped with ifc-pri
ifc-printer.exe --color test.cpp.ifc
```

See [BUILDING.md](BUILDING.md) for more information.

# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to grant us the right to use your contribution. For details, visit https://cla.opensource.microsoft.com.

Expand Down Expand Up @@ -56,4 +58,4 @@ Resources:

Copyright (c) Microsoft Corporation.

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 changes: 1 addition & 1 deletion cmake/find/FindMicrosoft.GSL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# This module is an opt-in find module _only_ for developers of the SDK who
# wish not to use vcpkg. To tell CMake about this find module, pass this file's
# cannot use vcpkg. To tell CMake about this find module, pass this file's
# directory via the CMAKE_MODULE_PATH command line option. This will satisfy a
# simple find_package(Microsoft.GSL REQUIRED) call by default.
# Do not use for release builds, because the GSL targets will also be added to
Expand Down
20 changes: 20 additions & 0 deletions cmake/find/Finddoctest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# This module is an opt-in find module _only_ for developers of the SDK who
# cannot use vcpkg. To tell CMake about this find module, pass this file's
# directory via the CMAKE_MODULE_PATH command line option. This will satisfy a
# simple find_package(doctest REQUIRED) call by default.

include(FetchContent)

FetchContent_Declare(
doctest
GIT_REPOSITORY https://github.com/doctest/doctest.git
GIT_TAG v2.4.11
GIT_SHALLOW YES
)

FetchContent_MakeAvailable(doctest)

set(doctest_FOUND 1)
35 changes: 35 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,44 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
find_package(Microsoft.IFC REQUIRED)
endif()

find_package(doctest CONFIG REQUIRED)

add_executable(ifc-test main.cxx)

target_compile_features(ifc-test PRIVATE cxx_std_23)

# Libs for ifc-test
target_link_libraries(ifc-test PRIVATE Microsoft.IFC::SDK)

if (WIN32)
# Only enabled for MSVC for now.
add_executable(ifc-basic basic.cxx)
# MSVC-specific testing
# Test IFCs
add_custom_target(test-ifcs
COMMAND
${CMAKE_CXX_COMPILER}
/nologo
/std:c++20
/ifcOutput${CMAKE_BINARY_DIR}/m.ifc
/c
${CMAKE_CURRENT_SOURCE_DIR}/m.ixx)

add_dependencies(ifc-basic test-ifcs)

target_compile_features(ifc-basic PRIVATE cxx_std_23)

target_compile_definitions(ifc-basic PRIVATE IFC_FILE="${CMAKE_BINARY_DIR}/m.ifc")

# Libs for ifc-basic
target_link_libraries(ifc-basic PRIVATE Microsoft.IFC::SDK)
target_link_libraries(ifc-basic PRIVATE doctest::doctest)
endif()

enable_testing()

add_test(NAME ifc-test COMMAND ifc-test)

if (WIN32)
add_test(NAME ifc-basic COMMAND ifc-basic)
endif()
Loading

0 comments on commit 489d177

Please sign in to comment.