diff options
author | Luca Di Sera <luca.disera@qt.io> | 2023-05-12 15:12:11 +0200 |
---|---|---|
committer | Luca Di Sera <luca.disera@qt.io> | 2023-05-15 13:02:08 +0200 |
commit | ac01635f461bd9f211d89f6eea833d9d928fd113 (patch) | |
tree | 4efbfd1fc2f551435605b1c72d93e372f2dbb90b /src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp | |
parent | 7057d01fbb9f8f37c707b33e3b92c10a78919ddc (diff) | |
download | qttools-ac01635f461bd9f211d89f6eea833d9d928fd113.tar.gz |
QDoc: Move catch_generators under src
QDoc employs a support library, "catch_generators", to
provide custom support for the data-generation that certain tests use or
might want to use when using the Catch2 testing framework, which QDoc
uses for some of its tests.
"catch_generators" was currently kept under "tests/auto/qdoc" and its
headers were included and used directly by related tests by relative
paths.
Due to a certain restructuring that is happening in QDoc, with one of
the goals, among others, being to increase the locality of QDoc-related
code under "src/qdoc", the "catch_generators" support library is now
moved under "src/qdoc".
To allow code that depended on it to keep their usages without requiring
the addition of some relative paths from the "tests" directory to the
"src" directory, a library target, "Qt::QDocCatchGeneratorsPrivate" was
created for it.
The target can be linked-to to gain access to the
previously-directly-used headers, with include path
"catch_generators/.*".
To allow for this specific include path to work, the internal directory
structure for "catch_generators" was slightly modified with the
addition of some intermediate directories in between its root and the
library headers.
The root "CMakeLists.txt" file for the QDoc project was modified to add
the moved "catch_generators" as a subdirectory, to include it in the
build process.
"catch_generators" contains its own test code under the subdirectory
"tests".
Previously the test target in "tests" included the relevant headers from
the library by use of relative paths to its sources.
Now that the library is exported as a target, the "CMakeLists.txt" file
under "tests" was modified to rely on linking to the library itself to
obtain access to the required headers.
Similarly, targets under "tests/auto/qdoc" that used the dependency now
link to it instead. Hence, their "CMakeLists.txt" files were modified to
avoid using the dependency as an include directory and instead use
"Qt::QDocCatcGeneratorsPrivate" as a library.
The inclusions of the "catch_generators" headers in
relevant sources was modified to respect the new "catch_generators/.*"
path.
Additionally, "catch_generators" exposed a top level header,
"qdoc_catch_generators.h", including certain headers in the library that
were expected to be commonly used by tests, so that the consumers did
not have to include multiple headers when using various common generators.
The header was removed in favor of including the library headers
themselves now that they have a well-defined root provided by the
exposed target.
Sources that included the header were modified to include only their
relevant headers from "catch_generators", respecting the additional
granularity.
Due to the Qt Project CI performing discovery of tests by configuring
only the "tests" directory of the module; the moved "catch_generators",
which contained its own testing code, will now escape the automated
testing in CI for its own tests, as the relevant target does not reside
under the "tests" directory of the repository.
To avoid this issue, the "CMakeLists.txt" file under "tests/auto/qdoc",
was modified to `include` the "CMakeLists.txt" file that describes
"catch_generators"' test target, so as to "mirror" the test under the
"tests" directory.
To support working when included, the "CMakeLists.txt" file that
describes "catch_generators"' test target was modified to refer to any
relevant relative path by prefixing the value of
`CMAKE_CURRENT_LIST_DIR`, so that the relative paths would not be
incorrectly resolved when using `include`.
Due to this "mirroring", the relevant test target would be configured
twice when the whole repository was configured, resulting in an error.
To ensure that this is not the case, the `include` directive was hidden
behind `QT_BUILD_STANADALONE_TESTS`, which is not set during a general
configuration of the repository but is set when CI performs
test-discovery on the "tests" directory.
Similarly, the "tests" directory of a repository is configured only when
`QT_BUILD_TESTS` is `ON`, which further ensures that the required
dependencies, such as `Qt::Test`, will be found before the directory is
configured.
By having moved the test declaration outside of the `tests` directory,
the test will always be configured, failing to find its dependencies
when this happens.
To avoid the issue, the `add_subdirectory(tests)` directive in
`src/qdoc/catch_generators/CMakeLists.txt` was conditioned over `QT_BUILD_TESTS`.
Certain files that were missing an license header were enhanced with it,
as the lack of an header will now be an issue outside the "tests" directory.
Change-Id: I0bca477b7da75c121c24b8528fc9defa26a6123e
Reviewed-by: Topi Reiniƶ <topi.reinio@qt.io>
Diffstat (limited to 'src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp')
-rw-r--r-- | src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp b/src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp new file mode 100644 index 000000000..0e92f6900 --- /dev/null +++ b/src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp @@ -0,0 +1,89 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#include <catch_generators/namespaces.h> +#include <catch_generators/generators/qchar_generator.h> +#include <catch_generators/generators/qstring_generator.h> + +#include <catch_conversions/qt_catch_conversions.h> + +#include <catch/catch.hpp> + +using namespace QDOC_CATCH_GENERATORS_ROOT_NAMESPACE; + +#include <algorithm> + +SCENARIO("Binding a QString to a length range", "[QString][Bounds]") { + GIVEN("A minimum length") { + auto minimum_length = GENERATE(take(100, random(0, 100))); + + AND_GIVEN("A maximum length that is greater or equal than the minimum length") { + auto maximum_length = GENERATE_COPY(take(100, random(minimum_length, 100))); + + WHEN("A QString is generated from those bounds") { + QString generated_string = GENERATE_COPY(take(1, string(character(), minimum_length, maximum_length))); + + THEN("The generated string's length is in the range [minimum_length, maximum_length]") { + REQUIRE(generated_string.size() >= minimum_length); + REQUIRE(generated_string.size() <= maximum_length); + } + } + } + } +} + +TEST_CASE("When the maximum length and the minimum length are zero all generated strings are the empty string", "[QString][Bounds][SpecialCase][BoundingValue]") { + QString generated_string = GENERATE(take(100, string(character(), 0, 0))); + + REQUIRE(generated_string.isEmpty()); +} + +TEST_CASE("When the maximum length and the minimum length are equal, all generated strings have the same length equal to the given length", "[QString][Bounds][SpecialCase]") { + auto length = GENERATE(take(100, random(0, 100))); + auto generated_string = GENERATE_COPY(take(100, string(character(), length, length))); + + REQUIRE(generated_string.size() == length); +} + +SCENARIO("Limiting the characters that can compose a QString", "[QString][Contents]") { + GIVEN("A list of characters candidates") { + auto lower_character_bound = GENERATE(take(10, random( + static_cast<unsigned int>(std::numeric_limits<char16_t>::min()), + static_cast<unsigned int>(std::numeric_limits<char16_t>::max()) + ))); + auto upper_character_bound = GENERATE_COPY(take(10, random(lower_character_bound, static_cast<unsigned int>(std::numeric_limits<char16_t>::max())))); + + auto character_candidates = character(lower_character_bound, upper_character_bound); + + WHEN("A QString is generated from that list") { + QString generated_string = GENERATE_REF(take(100, string(std::move(character_candidates), 1, 50))); + + THEN("The string is composed only of characters that are in the list of characters") { + REQUIRE( + std::all_of( + generated_string.cbegin(), generated_string.cend(), + [lower_character_bound, upper_character_bound](QChar element){ return element.unicode() >= lower_character_bound && element.unicode() <= upper_character_bound; } + ) + ); + } + } + } +} + +TEST_CASE("The strings generated by a generator of empty string are all empty", "[QString][Contents]") { + QString generated_string = GENERATE(take(100, empty_string())); + + REQUIRE(generated_string.isEmpty()); +} + + +TEST_CASE("The first element of the passsed in generator is not lost", "[QString][GeneratorFirstElement][SpecialCase]") { + QChar first_value{'a'}; + + // REMARK: We use two values to avoid having the generator throw + // an exception if the first element is actually lost. + auto character_generator{Catch::Generators::values({first_value, QChar{'b'}})}; + auto generated_string = GENERATE_REF(take(1, string(std::move(character_generator), 1, 1))); + + REQUIRE(generated_string == QString{first_value}); +} |