// 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 "namespaces.h" #include "generators/qchar_generator.h" #include "generators/qstring_generator.h" #include #include using namespace QDOC_CATCH_GENERATORS_ROOT_NAMESPACE; #include 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(std::numeric_limits::min()), static_cast(std::numeric_limits::max()) ))); auto upper_character_bound = GENERATE_COPY(take(10, random(lower_character_bound, static_cast(std::numeric_limits::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}); }