-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added simple unit test sample for demonstration #4154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ec83745
cmake: Remove unneeded include directories
cryptomilk db9cadd
cmake: Remove uneeded properties from darktable-test-variables
cryptomilk 9e615b9
src:tests: Added simple unit test exsample for demonstration
burrima b978d23
src:tests: Added function mocking
burrima d0ac74f
ci: Update ci-script
cryptomilk 95bddc0
ci: Allow to execute tests on tmpfs
cryptomilk f230751
ci: Enable unit testing
cryptomilk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # MIT License | ||
| # | ||
| # Copyright (c) 2018 Kamil Lorenc | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| ## Add unit test with mocking support | ||
| # \param name unit test name (excluding extension and 'test_' prefix) | ||
| # \param SOURCES optional list of source files to include in test executable | ||
| # (beside test_${name}.c) | ||
| # \param MOCKS optional list of functions to be mocked in executable | ||
| # \param COMPILE_OPTIONS optional list of options for the compiler | ||
| # \param LINK_LIBRARIES optional list of libraries to link (used as | ||
| # -l${LINK_LIBRARIES}) | ||
| # \param LINK_OPTIONS optional list of options to be passed to linker | ||
| function(add_cmocka_mock_test name) | ||
| # parse arguments passed to the function | ||
| set(options ) | ||
| set(oneValueArgs ) | ||
| set(multiValueArgs SOURCES MOCKS COMPILE_OPTIONS LINK_LIBRARIES LINK_OPTIONS) | ||
| cmake_parse_arguments(ADD_MOCKED_TEST "${options}" "${oneValueArgs}" | ||
| "${multiValueArgs}" ${ARGN} ) | ||
|
|
||
| # create link flags for mocks | ||
| set(link_flags "") | ||
| foreach (mock ${ADD_MOCKED_TEST_MOCKS}) | ||
| set(link_flags "${link_flags} -Wl,--wrap=${mock}") | ||
| endforeach(mock) | ||
|
|
||
| # define test | ||
| add_cmocka_test(${name} | ||
| SOURCES ${ADD_MOCKED_TEST_SOURCES} | ||
| COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} | ||
| ${ADD_MOCKED_TEST_COMPILE_OPTIONS} | ||
| LINK_LIBRARIES ${ADD_MOCKED_TEST_LINK_LIBRARIES} | ||
| LINK_OPTIONS ${link_flags} ${ADD_MOCKED_TEST_LINK_OPTIONS}) | ||
|
|
||
| # allow using includes from src/ directory | ||
| target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/src) | ||
| endfunction(add_cmocka_mock_test) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # | ||
| # Copyright (c) 2007 Daniel Gollub <[email protected]> | ||
| # Copyright (c) 2007-2018 Andreas Schneider <[email protected]> | ||
| # Copyright (c) 2018 Anderson Toshiyuki Sasaki <[email protected]> | ||
| # | ||
| # Redistribution and use is allowed according to the terms of the BSD license. | ||
| # For details see the accompanying COPYING-CMAKE-SCRIPTS file. | ||
|
|
||
| #.rst: | ||
| # AddCMockaTest | ||
| # ------------- | ||
| # | ||
| # This file provides a function to add a test | ||
| # | ||
| # Functions provided | ||
| # ------------------ | ||
| # | ||
| # :: | ||
| # | ||
| # add_cmocka_test(target_name | ||
| # SOURCES src1 src2 ... srcN | ||
| # [COMPILE_OPTIONS opt1 opt2 ... optN] | ||
| # [LINK_LIBRARIES lib1 lib2 ... libN] | ||
| # [LINK_OPTIONS lopt1 lop2 .. loptN] | ||
| # ) | ||
| # | ||
| # ``target_name``: | ||
| # Required, expects the name of the test which will be used to define a target | ||
| # | ||
| # ``SOURCES``: | ||
| # Required, expects one or more source files names | ||
| # | ||
| # ``COMPILE_OPTIONS``: | ||
| # Optional, expects one or more options to be passed to the compiler | ||
| # | ||
| # ``LINK_LIBRARIES``: | ||
| # Optional, expects one or more libraries to be linked with the test | ||
| # executable. | ||
| # | ||
| # ``LINK_OPTIONS``: | ||
| # Optional, expects one or more options to be passed to the linker | ||
| # | ||
| # | ||
| # Example: | ||
| # | ||
| # .. code-block:: cmake | ||
| # | ||
| # add_cmocka_test(my_test | ||
| # SOURCES my_test.c other_source.c | ||
| # COMPILE_OPTIONS -g -Wall | ||
| # LINK_LIBRARIES mylib | ||
| # LINK_OPTIONS -Wl,--enable-syscall-fixup | ||
| # ) | ||
| # | ||
| # Where ``my_test`` is the name of the test, ``my_test.c`` and | ||
| # ``other_source.c`` are sources for the binary, ``-g -Wall`` are compiler | ||
| # options to be used, ``mylib`` is a target of a library to be linked, and | ||
| # ``-Wl,--enable-syscall-fixup`` is an option passed to the linker. | ||
| # | ||
|
|
||
| enable_testing() | ||
| include(CTest) | ||
|
|
||
| if (CMAKE_CROSSCOMPILING) | ||
| if (WIN32) | ||
| find_program(WINE_EXECUTABLE | ||
| NAMES wine) | ||
| set(TARGET_SYSTEM_EMULATOR ${WINE_EXECUTABLE} CACHE INTERNAL "") | ||
| endif() | ||
| endif() | ||
|
|
||
| function(ADD_CMOCKA_TEST _TARGET_NAME) | ||
|
|
||
| set(one_value_arguments | ||
| ) | ||
|
|
||
| set(multi_value_arguments | ||
| SOURCES | ||
| COMPILE_OPTIONS | ||
| LINK_LIBRARIES | ||
| LINK_OPTIONS | ||
| ) | ||
|
|
||
| cmake_parse_arguments(_add_cmocka_test | ||
| "" | ||
| "${one_value_arguments}" | ||
| "${multi_value_arguments}" | ||
| ${ARGN} | ||
| ) | ||
|
|
||
| if (NOT DEFINED _add_cmocka_test_SOURCES) | ||
| message(FATAL_ERROR "No sources provided for target ${_TARGET_NAME}") | ||
| endif() | ||
|
|
||
| add_executable(${_TARGET_NAME} ${_add_cmocka_test_SOURCES}) | ||
|
|
||
| if (DEFINED _add_cmocka_test_COMPILE_OPTIONS) | ||
| target_compile_options(${_TARGET_NAME} | ||
| PRIVATE ${_add_cmocka_test_COMPILE_OPTIONS} | ||
| ) | ||
| endif() | ||
|
|
||
| if (DEFINED _add_cmocka_test_LINK_LIBRARIES) | ||
| target_link_libraries(${_TARGET_NAME} | ||
| PRIVATE ${_add_cmocka_test_LINK_LIBRARIES} | ||
| ) | ||
| endif() | ||
|
|
||
| if (DEFINED _add_cmocka_test_LINK_OPTIONS) | ||
| set_target_properties(${_TARGET_NAME} | ||
| PROPERTIES LINK_FLAGS | ||
| ${_add_cmocka_test_LINK_OPTIONS} | ||
| ) | ||
| endif() | ||
|
|
||
| add_test(NAME ${_TARGET_NAME} | ||
| COMMAND ${TARGET_SYSTEM_EMULATOR} ${_TARGET_NAME}) | ||
|
|
||
| endfunction (ADD_CMOCKA_TEST) | ||
|
|
||
| function(ADD_CMOCKA_TEST_ENVIRONMENT _TARGET_NAME) | ||
| if (WIN32 OR CYGWIN OR MINGW OR MSVC) | ||
| file(TO_NATIVE_PATH "${cmocka-library_BINARY_DIR}" CMOCKA_DLL_PATH) | ||
|
|
||
| if (TARGET_SYSTEM_EMULATOR) | ||
| set(DLL_PATH_ENV "WINEPATH=${CMOCKA_DLL_PATH};$ENV{WINEPATH}") | ||
| else() | ||
| set(DLL_PATH_ENV "PATH=${CMOCKA_DLL_PATH};$ENV{PATH}") | ||
| endif() | ||
| # | ||
| # IMPORTANT NOTE: The set_tests_properties(), below, internally | ||
| # stores its name/value pairs with a semicolon delimiter. | ||
| # because of this we must protect the semicolons in the path | ||
| # | ||
| string(REPLACE ";" "\\;" DLL_PATH_ENV "${DLL_PATH_ENV}") | ||
|
|
||
| set_tests_properties(${_TARGET_NAME} | ||
| PROPERTIES | ||
| ENVIRONMENT | ||
| "${DLL_PATH_ENV}") | ||
| endif() | ||
| endfunction() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,4 @@ | ||
| include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) | ||
| include_directories(${CMAKE_CURRENT_BINARY_DIR}/..) | ||
|
|
||
| add_executable(darktable-test-variables variables.c) | ||
|
|
||
| set_target_properties(darktable-test-variables PROPERTIES INSTALL_RPATH "$ORIGIN/../") | ||
| set_target_properties(darktable-test-variables PROPERTIES LINKER_LANGUAGE C) | ||
| target_link_libraries(darktable-test-variables lib_darktable) | ||
|
|
||
| add_subdirectory(unittests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| add_cmocka_test(test_sample | ||
| SOURCES test_sample.c | ||
| LINK_LIBRARIES cmocka) | ||
|
|
||
| add_cmocka_mock_test(test_filmicrgb | ||
| SOURCES test_filmicrgb.c | ||
| LINK_LIBRARIES lib_darktable cmocka | ||
| MOCKS dt_iop_color_picker_reset) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.