mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00

In the past, FTXUI switched from std::string to std::wstring to support fullwidth characters. The reasons was that fullwidth characters can be stored inside a single wchar_t. Then FTXUI added support for combining characters. A single glygh doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large. The usage of wstring doesn't really fit the new model and have several drawbacks: 1. It doesn't simplify the implementation of FTXUI, because of combining characters. 2. It reduces drawing performance by 2x. 3. It increase Screen's memory allocation by 2x. This patch converts FTXUI to use std::string internally. It now exposes std::string based API. The std::wstring API remains, but is now deprecated. Tests and examples haven't been update to show the breakage is limited. They will be updated in a second set of patches. Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153 Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
371 lines
9.2 KiB
C++
371 lines
9.2 KiB
C++
#include <gtest/gtest-message.h> // for Message
|
|
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl, TestPartResult
|
|
#include <algorithm> // for remove
|
|
#include <string> // for allocator, basic_string, string
|
|
#include <vector> // for vector
|
|
|
|
#include "ftxui/dom/deprecated.hpp" // for vtext
|
|
#include "ftxui/dom/elements.hpp" // for operator|, Element, flex_grow, flex_shrink, vbox
|
|
#include "ftxui/dom/node.hpp" // for Render
|
|
#include "ftxui/screen/box.hpp" // for ftxui
|
|
#include "ftxui/screen/screen.hpp" // for Screen
|
|
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
|
|
|
|
using namespace ftxui;
|
|
using namespace ftxui;
|
|
|
|
std::string rotate(std::string str) {
|
|
str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
|
|
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
|
|
return str;
|
|
}
|
|
|
|
TEST(VBoxText, NoFlex_NoFlex_NoFlex) {
|
|
auto root = vbox({
|
|
vtext(L"012"),
|
|
vtext(L"abc"),
|
|
vtext(L"ABC"),
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexGrow_NoFlex_NoFlex) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_grow,
|
|
vtext(L"abc"),
|
|
vtext(L"ABC"),
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012 abcABC", //
|
|
"012 abcABC", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, NoFlex_FlexGrow_NoFlex) {
|
|
auto root = vbox({
|
|
vtext(L"012"),
|
|
vtext(L"abc") | flex_grow,
|
|
vtext(L"ABC"),
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abc ABC", //
|
|
"012abc ABC", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, NoFlex_NoFlex_FlexGrow) {
|
|
auto root = vbox({
|
|
vtext(L"012"),
|
|
vtext(L"abc"),
|
|
vtext(L"ABC") | flex_grow,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexGrow_NoFlex_FlexGrow) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_grow,
|
|
vtext(L"abc"),
|
|
vtext(L"ABC") | flex_grow,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012 abcABC ", //
|
|
"012 abcABC ", //
|
|
"012 abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexGrow_FlexGrow_FlexGrow) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_grow,
|
|
vtext(L"abc") | flex_grow,
|
|
vtext(L"ABC") | flex_grow,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abc ABC ", //
|
|
"012 abc ABC ", //
|
|
"012 abc ABC ", //
|
|
"012 abc ABC ", //
|
|
"012 abc ABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
// ------
|
|
|
|
TEST(VBoxText, FlexShrink_NoFlex_NoFlex) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_shrink,
|
|
vtext(L"abc"),
|
|
vtext(L"ABC"),
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"a", //
|
|
"aA", //
|
|
"abA", //
|
|
"abAB", //
|
|
"abcAB", //
|
|
"abcABC", //
|
|
"0abcABC", //
|
|
"01abcABC", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, NoFlex_FlexShrink_NoFlex) {
|
|
auto root = vbox({
|
|
vtext(L"012"),
|
|
vtext(L"abc") | flex_shrink,
|
|
vtext(L"ABC"),
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0A", //
|
|
"01A", //
|
|
"01AB", //
|
|
"012AB", //
|
|
"012ABC", //
|
|
"012aABC", //
|
|
"012abABC", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, NoFlex_NoFlex_FlexShrink) {
|
|
auto root = vbox({
|
|
vtext(L"012"),
|
|
vtext(L"abc"),
|
|
vtext(L"ABC") | flex_shrink,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"01a", //
|
|
"01ab", //
|
|
"012ab", //
|
|
"012abc", //
|
|
"012abcA", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexShrink_NoFlex_FlexShrink) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_shrink,
|
|
vtext(L"abc"),
|
|
vtext(L"ABC") | flex_shrink,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"a", //
|
|
"ab", //
|
|
"abc", //
|
|
"0abc", //
|
|
"0abcA", //
|
|
"01abcA", //
|
|
"01abcAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexShrink_FlexShrink_FlexShrink) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_shrink,
|
|
vtext(L"abc") | flex_shrink,
|
|
vtext(L"ABC") | flex_shrink,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"0aA", //
|
|
"01aA", //
|
|
"01abA", //
|
|
"01abAB", //
|
|
"012abAB", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
"012abcABC ", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
TEST(VBoxText, FlexGrow_NoFlex_FlewShrink) {
|
|
auto root = vbox({
|
|
vtext(L"012") | flex_grow,
|
|
vtext(L"abc"),
|
|
vtext(L"ABC") | flex_shrink,
|
|
});
|
|
|
|
std::vector<std::string> expectations = {
|
|
"", //
|
|
"0", //
|
|
"0a", //
|
|
"01a", //
|
|
"01ab", //
|
|
"012ab", //
|
|
"012abc", //
|
|
"012abcA", //
|
|
"012abcAB", //
|
|
"012abcABC", //
|
|
"012 abcABC", //
|
|
"012 abcABC", //
|
|
"012 abcABC", //
|
|
};
|
|
for (int i = 0; i < expectations.size(); ++i) {
|
|
Screen screen(1, i);
|
|
Render(screen, root);
|
|
EXPECT_EQ(expectations[i], rotate(screen.ToString()));
|
|
}
|
|
}
|
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
// the LICENSE file.
|