Update examples to use std::string. (#182)

In examples and tests, use std::string.

In addtion:
1. Address follow-up from:
https://github.com/ArthurSonzogni/FTXUI/pull/179
2. Fix a bug when Input is used with std::string.
This commit is contained in:
Arthur Sonzogni
2021-08-09 00:27:37 +02:00
committed by GitHub
parent 3b4ab618a3
commit 9a54528bca
60 changed files with 817 additions and 836 deletions

View File

@@ -9,14 +9,17 @@
namespace ftxui {
static std::string charset[] =
static std::string charset[] = {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft's terminals often use fonts not handling the 8 unicode
// characters for representing the whole gauge. Fallback with less.
{" ", " ", " ", " ", "", "", "", "", "", ""};
" ", " ", " ", " ", "", "", "", "", "", "",
#else
{" ", " ", "", "", "", "", "", "", "", ""};
" ", " ", "", "", "", "", "", "", "", "",
#endif
// An extra character in case when the fuzzer manage to have:
// int(9 * (limit - limit_int) = 9
""};
class Gauge : public Node {
public:

View File

@@ -3,8 +3,7 @@
#include <string> // for allocator, basic_string, string
#include <vector> // for vector
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, Element, flex_grow, flex_shrink, hbox
#include "ftxui/dom/elements.hpp" // for text, operator|, Element, flex_grow, flex_shrink, hbox
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/screen.hpp" // for Screen
@@ -15,9 +14,9 @@ using namespace ftxui;
TEST(HBoxTest, NoFlex_NoFlex_NoFlex) {
auto root = hbox({
text(L"012"),
text(L"abc"),
text(L"ABC"),
text("012"),
text("abc"),
text("ABC"),
});
std::vector<std::string> expectations = {
@@ -43,9 +42,9 @@ TEST(HBoxTest, NoFlex_NoFlex_NoFlex) {
TEST(HBoxTest, FlexGrow_NoFlex_NoFlex) {
auto root = hbox({
text(L"012") | flex_grow,
text(L"abc"),
text(L"ABC"),
text("012") | flex_grow,
text("abc"),
text("ABC"),
});
std::vector<std::string> expectations = {
@@ -71,9 +70,9 @@ TEST(HBoxTest, FlexGrow_NoFlex_NoFlex) {
TEST(HBoxTest, NoFlex_FlexGrow_NoFlex) {
auto root = hbox({
text(L"012"),
text(L"abc") | flex_grow,
text(L"ABC"),
text("012"),
text("abc") | flex_grow,
text("ABC"),
});
std::vector<std::string> expectations = {
@@ -99,9 +98,9 @@ TEST(HBoxTest, NoFlex_FlexGrow_NoFlex) {
TEST(HBoxTest, NoFlex_NoFlex_FlexGrow) {
auto root = hbox({
text(L"012"),
text(L"abc"),
text(L"ABC") | flex_grow,
text("012"),
text("abc"),
text("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -127,9 +126,9 @@ TEST(HBoxTest, NoFlex_NoFlex_FlexGrow) {
TEST(HBoxTest, FlexGrow_NoFlex_FlexGrow) {
auto root = hbox({
text(L"012") | flex_grow,
text(L"abc"),
text(L"ABC") | flex_grow,
text("012") | flex_grow,
text("abc"),
text("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -157,9 +156,9 @@ TEST(HBoxTest, FlexGrow_NoFlex_FlexGrow) {
TEST(HBoxTest, FlexGrow_FlexGrow_FlexGrow) {
auto root = hbox({
text(L"012") | flex_grow,
text(L"abc") | flex_grow,
text(L"ABC") | flex_grow,
text("012") | flex_grow,
text("abc") | flex_grow,
text("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -191,9 +190,9 @@ TEST(HBoxTest, FlexGrow_FlexGrow_FlexGrow) {
TEST(HBoxTest, FlexShrink_NoFlex_NoFlex) {
auto root = hbox({
text(L"012") | flex_shrink,
text(L"abc"),
text(L"ABC"),
text("012") | flex_shrink,
text("abc"),
text("ABC"),
});
std::vector<std::string> expectations = {
@@ -219,9 +218,9 @@ TEST(HBoxTest, FlexShrink_NoFlex_NoFlex) {
TEST(HBoxTest, NoFlex_FlexShrink_NoFlex) {
auto root = hbox({
text(L"012"),
text(L"abc") | flex_shrink,
text(L"ABC"),
text("012"),
text("abc") | flex_shrink,
text("ABC"),
});
std::vector<std::string> expectations = {
@@ -247,9 +246,9 @@ TEST(HBoxTest, NoFlex_FlexShrink_NoFlex) {
TEST(HBoxTest, NoFlex_NoFlex_FlexShrink) {
auto root = hbox({
text(L"012"),
text(L"abc"),
text(L"ABC") | flex_shrink,
text("012"),
text("abc"),
text("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -275,9 +274,9 @@ TEST(HBoxTest, NoFlex_NoFlex_FlexShrink) {
TEST(HBoxTest, FlexShrink_NoFlex_FlexShrink) {
auto root = hbox({
text(L"012") | flex_shrink,
text(L"abc"),
text(L"ABC") | flex_shrink,
text("012") | flex_shrink,
text("abc"),
text("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -302,9 +301,9 @@ TEST(HBoxTest, FlexShrink_NoFlex_FlexShrink) {
TEST(HBoxTest, FlexShrink_FlexShrink_FlexShrink) {
auto root = hbox({
text(L"012") | flex_shrink,
text(L"abc") | flex_shrink,
text(L"ABC") | flex_shrink,
text("012") | flex_shrink,
text("abc") | flex_shrink,
text("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -331,9 +330,9 @@ TEST(HBoxTest, FlexShrink_FlexShrink_FlexShrink) {
TEST(HBoxTest, FlexGrow_NoFlex_FlewShrink) {
auto root = hbox({
text(L"012") | flex_grow,
text(L"abc"),
text(L"ABC") | flex_shrink,
text("012") | flex_grow,
text("abc"),
text("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {

View File

@@ -68,8 +68,8 @@ class HFlow : public Node {
///
/// ```cpp
/// hbox({
/// text(L"Left"),
/// text(L"Right"),
/// text("Left"),
/// text("Right"),
/// });
/// ```
Element hflow(Elements children) {

View File

@@ -1,6 +1,7 @@
#include <memory> // for make_shared
#include <string> // for string
#include <vector> // for vector
#include <algorithm> // for min
#include <memory> // for make_shared
#include <string> // for string, wstring
#include <vector> // for vector
#include "ftxui/dom/deprecated.hpp" // for text, vtext
#include "ftxui/dom/elements.hpp" // for Element, text, vtext
@@ -87,7 +88,7 @@ Element text(std::string text) {
return std::make_shared<Text>(text);
}
/// @brief Display a piece of UTF16 encoded unicode text.
/// @brief Display a piece of unicode text.
/// @ingroup dom
/// @see ftxui::to_wstring
///
@@ -136,7 +137,7 @@ Element vtext(std::string text) {
return std::make_shared<VText>(text);
}
/// @brief Display a piece of UTF16 encoded unicode text vertically.
/// @brief Display a piece unicode text vertically.
/// @ingroup dom
/// @see ftxui::to_wstring
///

View File

@@ -1,19 +1,17 @@
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl, TestPartResult
#include <string> // for allocator, wstring
#include <string> // for allocator, string
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, border, Element
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/box.hpp" // for ftxui
#include "ftxui/screen/screen.hpp" // for Screen
#include "ftxui/screen/string.hpp" // for to_string
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
#include "ftxui/dom/elements.hpp" // for text, operator|, border, Element
#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;
TEST(TextTest, ScreenHeightSmaller) {
auto element = text(L"test");
auto element = text("test");
Screen screen(2, 0);
Render(screen, element);
@@ -21,7 +19,7 @@ TEST(TextTest, ScreenHeightSmaller) {
}
TEST(TextTest, ScreenSmaller) {
auto element = text(L"test");
auto element = text("test");
Screen screen(2, 1);
Render(screen, element);
@@ -29,7 +27,7 @@ TEST(TextTest, ScreenSmaller) {
}
TEST(TextTest, ScreenFit) {
auto element = text(L"test");
auto element = text("test");
Screen screen(4, 1);
Render(screen, element);
@@ -37,7 +35,7 @@ TEST(TextTest, ScreenFit) {
}
TEST(TextTest, ScreenBigger) {
auto element = text(L"test");
auto element = text("test");
Screen screen(6, 1);
Render(screen, element);
@@ -45,7 +43,7 @@ TEST(TextTest, ScreenBigger) {
}
TEST(TextTest, ScreenBigger2) {
auto element = text(L"test");
auto element = text("test");
Screen screen(6, 2);
Render(screen, element);
@@ -54,7 +52,7 @@ TEST(TextTest, ScreenBigger2) {
// See https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-504871456
TEST(TextTest, CJK) {
auto element = text(L"测试") | border;
auto element = text("测试") | border;
Screen screen(6, 3);
Render(screen, element);
EXPECT_EQ(
@@ -66,7 +64,7 @@ TEST(TextTest, CJK) {
// See https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-504871456
TEST(TextTest, CJK_2) {
auto element = text(L"测试") | border;
auto element = text("测试") | border;
Screen screen(5, 3);
Render(screen, element);
EXPECT_EQ(
@@ -78,7 +76,7 @@ TEST(TextTest, CJK_2) {
// See https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-504871456
TEST(TextTest, CJK_3) {
auto element = text(L"测试") | border;
auto element = text("测试") | border;
Screen screen(4, 3);
Render(screen, element);
EXPECT_EQ(
@@ -89,20 +87,20 @@ TEST(TextTest, CJK_3) {
}
TEST(TextTest, CombiningCharacters) {
const std::wstring t =
const std::string t =
// Combining above:
L"ā à á â ã ā a̅ ă ȧ ä ả å a̋ ǎ a̍ a̎ ȁ a̐ ȃ a̒ a̔ a̕ a̚ a̛ a̽ a̾ a̿ à á a͂ a͆ a͊ a͋ a͌ a͐ "
L"a͑ a͒ a͗ a͘ a͛ a͝ a͞ a͠ a͡ aͣ aͤ aͥ aͦ aͧ aͨ aͩ aͪ aͫ aͬ aͭ aͮ aͯ a᷀ a᷁ a᷃ a᷄ a᷅ a᷆ a᷇ a᷈ a᷉ a᷾ a⃐ a⃑ a⃔ "
L"a⃕ a⃖ a⃗ a⃛ a⃜ a⃡ a⃩ a⃰ a︠ a︡ a︢ a︣"
"ā à á â ã ā a̅ ă ȧ ä ả å a̋ ǎ a̍ a̎ ȁ a̐ ȃ a̒ a̔ a̕ a̚ a̛ a̽ a̾ a̿ à á a͂ a͆ a͊ a͋ a͌ a͐ "
"a͑ a͒ a͗ a͘ a͛ a͝ a͞ a͠ a͡ aͣ aͤ aͥ aͦ aͧ aͨ aͩ aͪ aͫ aͬ aͭ aͮ aͯ a᷀ a᷁ a᷃ a᷄ a᷅ a᷆ a᷇ a᷈ a᷉ a᷾ a⃐ a⃑ a⃔ "
"a⃕ a⃖ a⃗ a⃛ a⃜ a⃡ a⃩ a⃰ a︠ a︡ a︢ a︣"
// Combining middle:
L"a̴ a̵ a̶ a̷ a̸ a⃒ a⃓ a⃘ a⃙ a⃚ a⃝ a⃞ a⃟ a⃥ a⃦"
"a̴ a̵ a̶ a̷ a̸ a⃒ a⃓ a⃘ a⃙ a⃚ a⃝ a⃞ a⃟ a⃥ a⃦"
// Combining below:
L"a̗ a̘ a̙ a̜ a̝ a̞ a̟ a̠ a̡ a̢ ạ ḁ a̦ a̧ ą a̩ a̪ a̫ a̬ a̭ a̮ a̯ a̰ a̱ a̲ a̳ a̹ a̺ a̻ a̼ aͅ a͇ a͈ a͉ a͍ "
L"a͎ a͓ a͔ a͕ a͖ a͙ a͚ a͜ a͟ a͢ a᷂ a᷊ a᷿ a⃨";
"a̗ a̘ a̙ a̜ a̝ a̞ a̟ a̠ a̡ a̢ ạ ḁ a̦ a̧ ą a̩ a̪ a̫ a̬ a̭ a̮ a̯ a̰ a̱ a̲ a̳ a̹ a̺ a̻ a̼ aͅ a͇ a͈ a͉ a͍ "
"a͎ a͓ a͔ a͕ a͖ a͙ a͚ a͜ a͟ a͢ a᷂ a᷊ a᷿ a⃨";
auto element = text(t);
Screen screen(290, 1);
Render(screen, element);
EXPECT_EQ(to_string(t), screen.ToString());
EXPECT_EQ(t, screen.ToString());
}
// Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@@ -4,8 +4,7 @@
#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/elements.hpp" // for vtext, 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
@@ -22,9 +21,9 @@ std::string rotate(std::string str) {
TEST(VBoxText, NoFlex_NoFlex_NoFlex) {
auto root = vbox({
vtext(L"012"),
vtext(L"abc"),
vtext(L"ABC"),
vtext("012"),
vtext("abc"),
vtext("ABC"),
});
std::vector<std::string> expectations = {
@@ -50,9 +49,9 @@ TEST(VBoxText, NoFlex_NoFlex_NoFlex) {
TEST(VBoxText, FlexGrow_NoFlex_NoFlex) {
auto root = vbox({
vtext(L"012") | flex_grow,
vtext(L"abc"),
vtext(L"ABC"),
vtext("012") | flex_grow,
vtext("abc"),
vtext("ABC"),
});
std::vector<std::string> expectations = {
@@ -78,9 +77,9 @@ TEST(VBoxText, FlexGrow_NoFlex_NoFlex) {
TEST(VBoxText, NoFlex_FlexGrow_NoFlex) {
auto root = vbox({
vtext(L"012"),
vtext(L"abc") | flex_grow,
vtext(L"ABC"),
vtext("012"),
vtext("abc") | flex_grow,
vtext("ABC"),
});
std::vector<std::string> expectations = {
@@ -106,9 +105,9 @@ TEST(VBoxText, NoFlex_FlexGrow_NoFlex) {
TEST(VBoxText, NoFlex_NoFlex_FlexGrow) {
auto root = vbox({
vtext(L"012"),
vtext(L"abc"),
vtext(L"ABC") | flex_grow,
vtext("012"),
vtext("abc"),
vtext("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -134,9 +133,9 @@ TEST(VBoxText, NoFlex_NoFlex_FlexGrow) {
TEST(VBoxText, FlexGrow_NoFlex_FlexGrow) {
auto root = vbox({
vtext(L"012") | flex_grow,
vtext(L"abc"),
vtext(L"ABC") | flex_grow,
vtext("012") | flex_grow,
vtext("abc"),
vtext("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -164,9 +163,9 @@ TEST(VBoxText, FlexGrow_NoFlex_FlexGrow) {
TEST(VBoxText, FlexGrow_FlexGrow_FlexGrow) {
auto root = vbox({
vtext(L"012") | flex_grow,
vtext(L"abc") | flex_grow,
vtext(L"ABC") | flex_grow,
vtext("012") | flex_grow,
vtext("abc") | flex_grow,
vtext("ABC") | flex_grow,
});
std::vector<std::string> expectations = {
@@ -198,9 +197,9 @@ TEST(VBoxText, FlexGrow_FlexGrow_FlexGrow) {
TEST(VBoxText, FlexShrink_NoFlex_NoFlex) {
auto root = vbox({
vtext(L"012") | flex_shrink,
vtext(L"abc"),
vtext(L"ABC"),
vtext("012") | flex_shrink,
vtext("abc"),
vtext("ABC"),
});
std::vector<std::string> expectations = {
@@ -226,9 +225,9 @@ TEST(VBoxText, FlexShrink_NoFlex_NoFlex) {
TEST(VBoxText, NoFlex_FlexShrink_NoFlex) {
auto root = vbox({
vtext(L"012"),
vtext(L"abc") | flex_shrink,
vtext(L"ABC"),
vtext("012"),
vtext("abc") | flex_shrink,
vtext("ABC"),
});
std::vector<std::string> expectations = {
@@ -254,9 +253,9 @@ TEST(VBoxText, NoFlex_FlexShrink_NoFlex) {
TEST(VBoxText, NoFlex_NoFlex_FlexShrink) {
auto root = vbox({
vtext(L"012"),
vtext(L"abc"),
vtext(L"ABC") | flex_shrink,
vtext("012"),
vtext("abc"),
vtext("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -282,9 +281,9 @@ TEST(VBoxText, NoFlex_NoFlex_FlexShrink) {
TEST(VBoxText, FlexShrink_NoFlex_FlexShrink) {
auto root = vbox({
vtext(L"012") | flex_shrink,
vtext(L"abc"),
vtext(L"ABC") | flex_shrink,
vtext("012") | flex_shrink,
vtext("abc"),
vtext("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -309,9 +308,9 @@ TEST(VBoxText, FlexShrink_NoFlex_FlexShrink) {
TEST(VBoxText, FlexShrink_FlexShrink_FlexShrink) {
auto root = vbox({
vtext(L"012") | flex_shrink,
vtext(L"abc") | flex_shrink,
vtext(L"ABC") | flex_shrink,
vtext("012") | flex_shrink,
vtext("abc") | flex_shrink,
vtext("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {
@@ -338,9 +337,9 @@ TEST(VBoxText, FlexShrink_FlexShrink_FlexShrink) {
TEST(VBoxText, FlexGrow_NoFlex_FlewShrink) {
auto root = vbox({
vtext(L"012") | flex_grow,
vtext(L"abc"),
vtext(L"ABC") | flex_shrink,
vtext("012") | flex_grow,
vtext("abc"),
vtext("ABC") | flex_shrink,
});
std::vector<std::string> expectations = {