Skip to content

Commit

Permalink
simplify/doc maud_add_test
Browse files Browse the repository at this point in the history
  • Loading branch information
bkietz committed Nov 12, 2024
1 parent 63ed182 commit 5da9db8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ scans of C++ sources.
- If an interface unit of ``module test_:main`` is found then it will be linked
with each test executable, otherwise ``gtest_main`` will be linked.

- If the command ``maud_add_test(source_file_path partition out_target_name)``
- If the command ``maud_add_test(source_file_path out_target_name)``
is defined it will be invoked on each test source as it is scanned, allowing
you to override what a unit test is for your project.

Expand Down
8 changes: 5 additions & 3 deletions cmake_modules/Maud.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,9 @@ function(_maud_scan source_file)
return()
endif()
if(COMMAND "maud_add_test")
maud_add_test("${source_file}" "${module}" "${partition}" target_name)
maud_add_test("${source_file}" target_name)
else()
_maud_add_test("${source_file}" "${module}" "${partition}" target_name)
_maud_add_test("${source_file}" target_name)
endif()
endif()

Expand Down Expand Up @@ -506,7 +506,9 @@ function(_maud_scan source_file)
endfunction()


function(_maud_add_test source_file module partition out_target_name)
function(_maud_add_test source_file out_target_name)
get_source_file_property(partition "${source_file}" MAUD_PARTITION)

if(partition STREQUAL "main")
if(_MAUD_TEST_MAIN)
message(
Expand Down
4 changes: 4 additions & 0 deletions cmake_modules/trike/trike/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ def get_documentable_declaration(
# However this doesn't seem critical to support, particularly since
# if these constructions are necessary it should be sufficient to
# override the automatic declaration string.
#
# FIXME at least we should track depth of []{}() and only terminate
# when depth == 0. Otherwise we'll lose default arguments which
# include an initializer list.
break

if t.spelling in {
Expand Down
25 changes: 9 additions & 16 deletions project.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,9 @@ glob benchmark:
unit testing:
- write: basics.cxx
contents: |
module;
#include <coroutine>
#include <string>
module test_;
import test_;
SUITE_ { std::string yo = "yo"; };
TEST_(DISABLED_empty) {}
Expand Down Expand Up @@ -243,7 +242,7 @@ unit testing:
disabling unit testing:
- write: inline_python.test.cxx
contents: |
module test_;
import test_;
TEST_(inline_python) {
Expand All @@ -268,7 +267,7 @@ unit testing main:
}
- write: foo_check.cxx
contents: |
module test_;
import test_;
TEST_(check_foo_is_999) {
EXPECT_(foo == 999);
}
Expand All @@ -285,11 +284,11 @@ unit testing main:
custom unit testing:
- write: one_equals_three.test.cxx
contents: |
module test_;
import test_;
int main() {
expect_eq(1, 3);
}
- write: test_.cxx
- write: .test_.cxx
contents: |
module;
#include <iostream>
Expand All @@ -299,24 +298,18 @@ custom unit testing:
}
- write: test.cmake
contents: |
set(
MAUD_CXX_SOURCE_EXCLUSION_PATTERN
"test_[.]cxx"
)
function(maud_add_test source_file module partition out_target_name)
function(maud_add_test source_file out_target_name)
cmake_path(GET source_file STEM name)
set(${out_target_name} "test_.${name}" PARENT_SCOPE)
if(NOT TARGET "test_.${name}")
add_executable(test_.${name})
endif()
add_executable(test_.${name})
add_test(NAME test_.${name} COMMAND $<TARGET_FILE:test_.${name}>)
target_sources(
test_.${name}
PRIVATE FILE_SET module_providers TYPE CXX_MODULES
BASE_DIRS ${CMAKE_SOURCE_DIR} FILES test_.cxx
BASE_DIRS ${CMAKE_SOURCE_DIR} FILES .test_.cxx
)
set_target_properties(test_.${name} PROPERTIES MAUD_INTERFACE test_.cxx)
endfunction()
- maud --log-level=VERBOSE

Expand Down
3 changes: 2 additions & 1 deletion sphinx_configuration/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@
extensions += ["sphinx.ext.extlinks"]
extlinks_detect_hardcoded_links = True
extlinks = {
"cxx20": ("https://timsong-cpp.github.io/cppwp/n4868/%s", "CXX(20:%s)"),
"cxx20": ("https://timsong-cpp.github.io/cppwp/n4868/%s", "c++20:%s"),
# TODO this should be intersphinx instead
"cmake": ("https://cmake.org/cmake/help/latest/%s", None),
"mastering-cmake": ("https://cmake.org/cmake/help/book/mastering-cmake/chapter/%s", None),
"gtest": ("https://google.github.io/googletest/%s", None),
"sphinx": ("https://www.sphinx-doc.org/en/master/usage/%s", None),
}
Expand Down
57 changes: 50 additions & 7 deletions testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,56 @@ instead of ``GTest``), write an interface unit with

.. code-block:: cmake
maud_add_test(source_file partition out_target_name)
If defined, each source file which declares ``module test_``
or a partition of it will be passed to this function and
added to the target it names. (See project test
``custom unit testing`` for an example.)

maud_add_test(source_file out_target_name)
If defined, this function will be invoked on each source file which
declares ``import test_``, ``module test_``, or any partition of it.
If ``out_target_name`` is set to a
:mastering-cmake:`target name <Key Concepts.html#targets>`,
the source file will be attached to it and imports automatically
processed as with other ``Maud`` targets. For example, if you would
prefer to unit test with a minimal custom framework you could define
your own ``module test_``:

``.test_.cxx``
.. code-block:: c++

module;
#include "my_test_framework.hxx"
export module test_;
export using my_test_framework::expect_equals;
// ...

Then inject this into unit tests by defining ``maud_add_test``:

``test_.cmake``
.. code-block:: cmake
function(maud_add_test source_file out_target_name)
cmake_path(GET source_file STEM name)
set(${out_target_name} "test_.${name}" PARENT_SCOPE)
# Create a test executable and register it
add_executable(test_.${name})
add_test(NAME test_.${name} COMMAND $<TARGET_FILE:test_.${name}>)
# Link the unit test with test_.cxx:
target_sources(
test_.${name}
PRIVATE FILE_SET module_providers TYPE CXX_MODULES
BASE_DIRS ${CMAKE_SOURCE_DIR} FILES .test_.cxx
)
endfunction()
Then this could be used in a unit test:

``math.test.cxx``
.. code-block:: c++

import test_;
int main() {
expect_equals(1 + 2, 3);
}

Formatting test
~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 5da9db8

Please sign in to comment.