~aleteoryx/muditaos

muditaos/module-gui/test/test-catch-text/test-gui-TextDocument.cpp -rw-r--r-- 2.1 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <widgets/text/parsers/TextParse.hpp>
#include <widgets/text/core/TextDocument.hpp>

#include <mock/multi-line-string.hpp>
#include <mock/buildTextDocument.hpp>

#include <catch2/catch.hpp>

TEST_CASE("TextDocument ctor")
{
    using namespace gui;

    SECTION("empty")
    {
        /// textToTextBlocks have it's own tests
        auto document = TextDocument(textToTextBlocks("", nullptr));
        REQUIRE(document.getText() == UTF8());
    }

    SECTION("one block (one paragraph)")
    {
        const auto no_lines = 3;
        std::string text    = mockup::multiLineString(no_lines);
        auto document       = TextDocument(textToTextBlocks(text, nullptr));
        REQUIRE(!(document.getText() == ""));
        REQUIRE(document.getText() == text);
    }

    SECTION("many blocks")
    {
        const auto no_lines = 3;
        std::string text    = mockup::multiLineString(no_lines);
        auto blocks         = textToTextBlocks(text, nullptr);

        auto document = TextDocument(blocks);
        document.append(std::move(blocks));

        REQUIRE(!(document.getText() == ""));
        REQUIRE(document.getText() == std::string(text + text));
    }
}

TEST_CASE("TextDocument getBlockCursor")
{
    using namespace gui;

    SECTION("empty")
    {
        auto document = TextDocument({});
        auto cursor   = document.getBlockCursor(100);
        REQUIRE(cursor.getBlockNumber() == text::npos);
        auto text = document.getText(cursor);
        REQUIRE(text == "");
    }

    auto [document, testfont] = mockup::buildTestDocument();
    REQUIRE(testfont != nullptr);

    SECTION("middle")
    {
        unsigned int pos = 10;
        REQUIRE(document.getText().length() > pos);
        auto cursor = document.getBlockCursor(pos);
        REQUIRE(cursor.getPosition() != text::npos);
        REQUIRE(cursor.getBlockNumber() != text::npos);
        auto text = document.getText(cursor);
        REQUIRE(!(text == ""));
    }
}