mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-29 16:39:34 +08:00
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:
@@ -1,3 +1,4 @@
|
||||
#include <iostream>
|
||||
//#include "ftxui/component/event.hpp"
|
||||
//#include "ftxui/component/receiver.hpp"
|
||||
#include <vector>
|
||||
@@ -17,15 +18,21 @@ bool GeneratorBool(const char*& data, size_t& size) {
|
||||
return out;
|
||||
}
|
||||
|
||||
std::wstring GeneratorString(const char*& data, size_t& size) {
|
||||
std::string GeneratorString(const char*& data, size_t& size) {
|
||||
int index = 0;
|
||||
while (index < size && data[index])
|
||||
++index;
|
||||
|
||||
auto out = std::wstring(data, data + index);
|
||||
data += index;
|
||||
size -= index;
|
||||
return std::move(out);
|
||||
try {
|
||||
auto out = std::string(data, data + index);
|
||||
auto w_out = to_wstring(out);
|
||||
data += index;
|
||||
size -= index;
|
||||
return std::move(out);
|
||||
} catch (...) {
|
||||
// The input component do not support invalid UTF8 yet.
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
|
||||
int GeneratorInt(const char* data, size_t size) {
|
||||
@@ -39,7 +46,7 @@ int GeneratorInt(const char* data, size_t size) {
|
||||
|
||||
bool g_bool;
|
||||
int g_int;
|
||||
std::vector<std::wstring> g_list;
|
||||
std::vector<std::string> g_list;
|
||||
|
||||
Components GeneratorComponents(const char*& data, size_t& size, int depth);
|
||||
|
||||
@@ -114,7 +121,7 @@ extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
|
||||
g_bool = GeneratorBool(data, size);
|
||||
g_int = GeneratorInt(data, size);
|
||||
g_list = {
|
||||
L"test_1", L"test_2", L"test_3", L"test_4", L"test_5",
|
||||
"test_1", "test_2", "test_3", "test_4", "test_5",
|
||||
};
|
||||
|
||||
int depth = 10;
|
||||
@@ -123,6 +130,12 @@ extern "C" int LLVMFuzzerTestOneInput(const char* data, size_t size) {
|
||||
int width = GeneratorInt(data, size);
|
||||
int height = GeneratorInt(data, size);
|
||||
|
||||
width %= 500;
|
||||
width += 500;
|
||||
|
||||
height %= 500;
|
||||
height += 500;
|
||||
|
||||
auto screen =
|
||||
Screen::Create(Dimension::Fixed(width), Dimension::Fixed(height));
|
||||
|
||||
|
@@ -11,7 +11,7 @@
|
||||
using namespace ftxui;
|
||||
|
||||
Component Focusable() {
|
||||
return Button(L"", [] {});
|
||||
return Button("", [] {});
|
||||
}
|
||||
Component NonFocusable() {
|
||||
return Container::Horizontal({});
|
||||
|
@@ -200,7 +200,7 @@ class InputBase : public ComponentBase {
|
||||
bool OnEvent(Event event) override {
|
||||
wrapped_content_ = to_wstring(*content_);
|
||||
if (wrapped_input_.OnEvent(event)) {
|
||||
content_ = to_string(wrapped_content_);
|
||||
*content_ = to_string(wrapped_content_);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
@@ -1,12 +1,12 @@
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
|
||||
#include <string> // for wstring
|
||||
#include <string> // for string
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
#include "ftxui/component/component.hpp" // for Input
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase, Component
|
||||
#include "ftxui/component/component_options.hpp" // for InputOption
|
||||
#include "ftxui/component/deprecated.hpp" // for Input
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
|
||||
#include "ftxui/dom/elements.hpp" // for Fit
|
||||
#include "ftxui/dom/node.hpp" // for Render
|
||||
@@ -17,8 +17,8 @@
|
||||
using namespace ftxui;
|
||||
|
||||
TEST(InputTest, Init) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
Component input = Input(&content, &placeholder, &option);
|
||||
|
||||
@@ -26,17 +26,17 @@ TEST(InputTest, Init) {
|
||||
}
|
||||
|
||||
TEST(InputTest, Type) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
Component input = Input(&content, &placeholder, &option);
|
||||
|
||||
input->OnEvent(Event::Character('a'));
|
||||
EXPECT_EQ(content, L"a");
|
||||
input->OnEvent(Event::Character("a"));
|
||||
EXPECT_EQ(content, "a");
|
||||
EXPECT_EQ(option.cursor_position(), 1u);
|
||||
|
||||
input->OnEvent(Event::Character('b'));
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(content, "ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
auto document = input->Render();
|
||||
@@ -47,18 +47,18 @@ TEST(InputTest, Type) {
|
||||
}
|
||||
|
||||
TEST(InputTest, TypePassword) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
option.password = true;
|
||||
Component input = Input(&content, &placeholder, &option);
|
||||
|
||||
input->OnEvent(Event::Character('a'));
|
||||
EXPECT_EQ(content, L"a");
|
||||
EXPECT_EQ(content, "a");
|
||||
EXPECT_EQ(option.cursor_position(), 1u);
|
||||
|
||||
input->OnEvent(Event::Character('b'));
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(content, "ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
auto document = input->Render();
|
||||
@@ -69,8 +69,8 @@ TEST(InputTest, TypePassword) {
|
||||
}
|
||||
|
||||
TEST(InputTest, Arrow) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
auto input = Input(&content, &placeholder, &option);
|
||||
|
||||
@@ -106,53 +106,53 @@ TEST(InputTest, Arrow) {
|
||||
}
|
||||
|
||||
TEST(InputTest, Insert) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
Component input = Input(&content, &placeholder);
|
||||
|
||||
input->OnEvent(Event::Character('a'));
|
||||
input->OnEvent(Event::Character('b'));
|
||||
input->OnEvent(Event::Character('c'));
|
||||
EXPECT_EQ(content, L"abc");
|
||||
EXPECT_EQ(content, "abc");
|
||||
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(content, L"a-bc");
|
||||
EXPECT_EQ(content, "a-bc");
|
||||
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(content, L"a--bc");
|
||||
EXPECT_EQ(content, "a--bc");
|
||||
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
input->OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(content, L"-a--bc");
|
||||
EXPECT_EQ(content, "-a--bc");
|
||||
}
|
||||
|
||||
TEST(InputTest, Home) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
auto input = Input(&content, &placeholder, &option);
|
||||
|
||||
input->OnEvent(Event::Character('a'));
|
||||
input->OnEvent(Event::Character('b'));
|
||||
input->OnEvent(Event::Character('c'));
|
||||
EXPECT_EQ(content, L"abc");
|
||||
EXPECT_EQ(content, "abc");
|
||||
|
||||
EXPECT_EQ(option.cursor_position(), 3u);
|
||||
input->OnEvent(Event::Home);
|
||||
EXPECT_EQ(option.cursor_position(), 0u);
|
||||
|
||||
input->OnEvent(Event::Character('-'));
|
||||
EXPECT_EQ(content, L"-abc");
|
||||
EXPECT_EQ(content, "-abc");
|
||||
}
|
||||
|
||||
TEST(InputTest, End) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
auto input = Input(&content, &placeholder, &option);
|
||||
|
||||
@@ -169,8 +169,8 @@ TEST(InputTest, End) {
|
||||
}
|
||||
|
||||
TEST(InputTest, Delete) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
auto input = Input(&content, &placeholder, &option);
|
||||
|
||||
@@ -179,21 +179,21 @@ TEST(InputTest, Delete) {
|
||||
input->OnEvent(Event::Character('c'));
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
|
||||
EXPECT_EQ(content, L"abc");
|
||||
EXPECT_EQ(content, "abc");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
input->OnEvent(Event::Delete);
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(content, "ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
input->OnEvent(Event::Delete);
|
||||
EXPECT_EQ(content, L"ab");
|
||||
EXPECT_EQ(content, "ab");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
}
|
||||
|
||||
TEST(InputTest, Backspace) {
|
||||
std::wstring content;
|
||||
std::wstring placeholder;
|
||||
std::string content;
|
||||
std::string placeholder;
|
||||
auto option = InputOption();
|
||||
auto input = Input(&content, &placeholder, &option);
|
||||
|
||||
@@ -202,19 +202,19 @@ TEST(InputTest, Backspace) {
|
||||
input->OnEvent(Event::Character('c'));
|
||||
input->OnEvent(Event::ArrowLeft);
|
||||
|
||||
EXPECT_EQ(content, L"abc");
|
||||
EXPECT_EQ(content, "abc");
|
||||
EXPECT_EQ(option.cursor_position(), 2u);
|
||||
|
||||
input->OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(content, L"ac");
|
||||
EXPECT_EQ(content, "ac");
|
||||
EXPECT_EQ(option.cursor_position(), 1u);
|
||||
|
||||
input->OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(content, L"c");
|
||||
EXPECT_EQ(content, "c");
|
||||
EXPECT_EQ(option.cursor_position(), 0u);
|
||||
|
||||
input->OnEvent(Event::Backspace);
|
||||
EXPECT_EQ(content, L"c");
|
||||
EXPECT_EQ(content, "c");
|
||||
EXPECT_EQ(option.cursor_position(), 0u);
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
|
||||
#include <string> // for wstring, basic_string
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
@@ -14,7 +14,7 @@ using namespace ftxui;
|
||||
|
||||
TEST(RadioboxTest, Navigation) {
|
||||
int selected = 0;
|
||||
std::vector<std::wstring> entries = {L"1", L"2", L"3"};
|
||||
std::vector<std::string> entries = {"1", "2", "3"};
|
||||
auto radiobox = Radiobox(&entries, &selected);
|
||||
|
||||
// With arrow key.
|
||||
@@ -60,7 +60,7 @@ TEST(RadioboxTest, Navigation) {
|
||||
EXPECT_EQ(selected, 0);
|
||||
|
||||
// With more entries
|
||||
entries = {L"1", L"2", L"3"};
|
||||
entries = {"1", "2", "3"};
|
||||
EXPECT_EQ(selected, 0);
|
||||
radiobox->OnEvent(Event::ArrowDown);
|
||||
radiobox->OnEvent(Event::Return);
|
||||
|
@@ -4,9 +4,8 @@
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Renderer
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/dom/deprecated.hpp" // for text
|
||||
#include "ftxui/dom/elements.hpp" // for Element
|
||||
#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ
|
||||
#include "ftxui/dom/elements.hpp" // for text, Element
|
||||
#include "gtest/gtest_pred_impl.h" // for Test, TEST, EXPECT_EQ
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
@@ -18,7 +17,7 @@ bool TestSignal(int signal) {
|
||||
called++;
|
||||
std::raise(signal);
|
||||
called++;
|
||||
return text(L"");
|
||||
return text("");
|
||||
});
|
||||
|
||||
auto screen = ScreenInteractive::FitComponent();
|
||||
|
@@ -2,7 +2,7 @@
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include <functional> // for function
|
||||
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
|
||||
#include <string> // for wstring, basic_string
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp" // for ftxui
|
||||
@@ -15,7 +15,7 @@
|
||||
using namespace ftxui;
|
||||
|
||||
TEST(ToggleTest, leftRightArrow) {
|
||||
std::vector<std::wstring> entries = {L"On", L"Off"};
|
||||
std::vector<std::string> entries = {"On", "Off"};
|
||||
int selected = 0;
|
||||
auto toggle = Toggle(&entries, &selected);
|
||||
|
||||
@@ -42,7 +42,7 @@ TEST(ToggleTest, leftRightArrow) {
|
||||
EXPECT_EQ(selected, 0);
|
||||
|
||||
// With more entries
|
||||
entries = {L"1", L"2", L"3"};
|
||||
entries = {"1", "2", "3"};
|
||||
EXPECT_EQ(selected, 0);
|
||||
toggle->OnEvent(Event::ArrowRight);
|
||||
EXPECT_EQ(selected, 1);
|
||||
@@ -59,7 +59,7 @@ TEST(ToggleTest, leftRightArrow) {
|
||||
}
|
||||
|
||||
TEST(ToggleTest, Tab) {
|
||||
std::vector<std::wstring> entries = {L"1", L"2", L"3"};
|
||||
std::vector<std::string> entries = {"1", "2", "3"};
|
||||
int selected = 0;
|
||||
auto toggle = Toggle(&entries, &selected);
|
||||
|
||||
@@ -86,7 +86,7 @@ TEST(ToggleTest, Tab) {
|
||||
}
|
||||
|
||||
TEST(ToggleTest, OnChange) {
|
||||
std::vector<std::wstring> entries = {L"1", L"2", L"3"};
|
||||
std::vector<std::string> entries = {"1", "2", "3"};
|
||||
int selected = 0;
|
||||
int counter = 0;
|
||||
auto option = ToggleOption();
|
||||
@@ -115,7 +115,7 @@ TEST(ToggleTest, OnChange) {
|
||||
}
|
||||
|
||||
TEST(ToggleTest, OnEnter) {
|
||||
std::vector<std::wstring> entries = {L"1", L"2", L"3"};
|
||||
std::vector<std::string> entries = {"1", "2", "3"};
|
||||
int selected = 0;
|
||||
int counter = 0;
|
||||
|
||||
|
Reference in New Issue
Block a user