summaryrefslogtreecommitdiff
path: root/src/qdoc/catch_generators/tests/generators/catch_qstring_generator.cpp
blob: 0e92f6900764dbadb413f292e8fea9ee22703bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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});
}