init commit

This commit is contained in:
cantyonion committed 2026-11-24 12:43:25 +08:00
commit 1497f114e6
47 files changed
+8358

No files matched your search

+466
View File
@@ -0,0 +1,466 @@
#include "LineEditor.h"
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
#include <gtest/gtest.h>
namespace
{
using Bilicraft::LineEditor;
constexpr std::string_view Up = "\x1b[A";
constexpr std::string_view Down = "\x1b[B";
constexpr std::string_view Left = "\x1b[D";
constexpr std::string_view Right = "\x1b[C";
constexpr std::string_view Home = "\x1b[H";
constexpr std::string_view End = "\x1b[F";
constexpr std::string_view Delete = "\x1b[3~";
const std::string Chinese = "\xe4\xb8\xad\xe6\x96\x87";
const std::string Emoji = "\xf0\x9f\x8c\xb1";
void Type(LineEditor &editor, std::string_view bytes)
{
for (const char byte : bytes)
EXPECT_FALSE(editor.Feed(byte));
}
void Submit(LineEditor &editor, std::string_view text)
{
Type(editor, text);
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>(text));
EXPECT_TRUE(editor.Text().empty());
EXPECT_EQ(editor.Cursor(), 0U);
}
TEST(LineEditorTest, EmptyEditorAndEmptySubmission)
{
LineEditor editor;
EXPECT_TRUE(editor.Text().empty());
EXPECT_EQ(editor.Cursor(), 0U);
EXPECT_FALSE(editor.IsEof());
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>(""));
Type(editor, Up);
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, SubmitsOneLineAtATimeAndClearsText)
{
LineEditor editor;
std::vector<std::string> lines;
for (const char byte : std::string("one\ntwo\rthree\r\nfour\n"))
if (auto line = editor.Feed(byte))
lines.push_back(*line);
EXPECT_EQ(lines, (std::vector<std::string>{"one", "two", "three", "four"}));
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, CrLfSuppressionPersistsAcrossRequestBoundaries)
{
LineEditor editor;
Type(editor, "one");
EXPECT_EQ(editor.Feed('\r'), std::optional<std::string>("one"));
EXPECT_TRUE(editor.Text().empty());
// No further input is fed until the next request is allowed.
EXPECT_FALSE(editor.Feed('\n'));
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>(""));
EXPECT_EQ(editor.Feed('\r'), std::optional<std::string>(""));
Type(editor, "x");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("x"));
EXPECT_EQ(editor.Feed('\r'), std::optional<std::string>(""));
EXPECT_EQ(editor.Feed('\r'), std::optional<std::string>(""));
}
TEST(LineEditorTest, HistoryBrowsesBothDirectionsAndClampsAtEnds)
{
LineEditor editor;
Submit(editor, "one");
Submit(editor, "two");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "two");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "one");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "one");
Type(editor, Down);
EXPECT_EQ(editor.Text(), "two");
Type(editor, Down);
EXPECT_TRUE(editor.Text().empty());
Type(editor, Down);
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, HistoryRestoresDraftAndItsUtf8Cursor)
{
LineEditor editor;
Submit(editor, "old");
Type(editor, "a" + Chinese + "z");
Type(editor, Left);
Type(editor, Left);
const auto expectedText = editor.Text();
const auto expectedCursor = editor.Cursor();
ASSERT_EQ(expectedCursor, 4U);
Type(editor, Up);
EXPECT_EQ(editor.Text(), "old");
Type(editor, Down);
EXPECT_EQ(editor.Text(), expectedText);
EXPECT_EQ(editor.Cursor(), expectedCursor);
Type(editor, "!");
Type(editor, Up);
Type(editor, Down);
EXPECT_EQ(editor.Text(), "a" + Chinese.substr(0, 3) + "!" + Chinese.substr(3) + "z");
EXPECT_EQ(editor.Cursor(), expectedCursor + 1);
}
TEST(LineEditorTest, EditingRecalledLineDoesNotMutateHistory)
{
LineEditor editor;
Submit(editor, "first");
Submit(editor, "second");
Type(editor, Up);
Type(editor, " changed");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "first");
Type(editor, Down);
EXPECT_EQ(editor.Text(), "second");
Type(editor, Down);
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, SubmittingEditedHistoryAddsNewEntryAndResetsDraft)
{
LineEditor editor;
Submit(editor, "original");
Type(editor, "draft");
Type(editor, Up);
Type(editor, "!");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("original!"));
Type(editor, Up);
EXPECT_EQ(editor.Text(), "original!");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "original");
Type(editor, Down);
Type(editor, Down);
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, IgnoresWhitespaceHistoryAndOnlyDeduplicatesConsecutiveEntries)
{
LineEditor editor;
Submit(editor, "alpha");
Submit(editor, "alpha");
Submit(editor, " \t ");
Submit(editor, "");
Submit(editor, "alpha");
Submit(editor, "beta");
Submit(editor, "alpha");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "alpha");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "beta");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "alpha");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "alpha");
}
TEST(LineEditorTest, HistoryPreservesNonblankWhitespaceExactly)
{
LineEditor editor;
Submit(editor, " command\t ");
Type(editor, Up);
EXPECT_EQ(editor.Text(), " command\t ");
}
TEST(LineEditorTest, HistoryRetainsOnlyLatestTwoHundredEntries)
{
LineEditor editor;
for (std::size_t index = 0; index < LineEditor::MaxHistoryEntries + 7; ++index)
Submit(editor, "entry" + std::to_string(index));
for (std::size_t index = 0; index < LineEditor::MaxHistoryEntries + 10; ++index)
Type(editor, Up);
EXPECT_EQ(editor.Text(), "entry7");
Type(editor, Down);
EXPECT_EQ(editor.Text(), "entry8");
for (std::size_t index = 0; index < LineEditor::MaxHistoryEntries; ++index)
Type(editor, Down);
EXPECT_TRUE(editor.Text().empty());
Submit(editor, "new");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "new");
}
TEST(LineEditorTest, HistoryIsLocalToEachEditor)
{
LineEditor first;
Submit(first, "private");
LineEditor second;
Type(second, Up);
EXPECT_TRUE(second.Text().empty());
}
TEST(LineEditorTest, FragmentedCsiAndSs3ArrowsAreNotInserted)
{
LineEditor editor;
Submit(editor, "first");
Submit(editor, "second");
EXPECT_FALSE(editor.Feed('\x1b'));
EXPECT_TRUE(editor.Text().empty());
EXPECT_FALSE(editor.Feed('['));
EXPECT_TRUE(editor.Text().empty());
EXPECT_FALSE(editor.Feed('A'));
EXPECT_EQ(editor.Text(), "second");
EXPECT_FALSE(editor.Feed('\x1b'));
EXPECT_FALSE(editor.Feed('O'));
EXPECT_EQ(editor.Text(), "second");
EXPECT_FALSE(editor.Feed('A'));
EXPECT_EQ(editor.Text(), "first");
Type(editor, "\x1bOB");
EXPECT_EQ(editor.Text(), "second");
Type(editor, "\x1bOB");
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, InsertDeleteAndBackspaceAtAsciiCursor)
{
LineEditor editor;
Type(editor, "ac");
Type(editor, Left);
Type(editor, "b");
EXPECT_EQ(editor.Text(), "abc");
EXPECT_EQ(editor.Cursor(), 2U);
Type(editor, Delete);
EXPECT_EQ(editor.Text(), "ab");
Type(editor, "\x7f");
EXPECT_EQ(editor.Text(), "a");
Type(editor, "\x08");
EXPECT_TRUE(editor.Text().empty());
Type(editor, "\x08\x7f");
Type(editor, Delete);
Type(editor, Left);
Type(editor, Right);
EXPECT_EQ(editor.Cursor(), 0U);
}
TEST(LineEditorTest, Utf8CodepointsAreInsertedAtomicallyAndEditedAtByteBoundaries)
{
LineEditor editor;
EXPECT_FALSE(editor.Feed(Chinese[0]));
EXPECT_FALSE(editor.Feed(Chinese[1]));
EXPECT_TRUE(editor.Text().empty());
EXPECT_EQ(editor.Cursor(), 0U);
EXPECT_FALSE(editor.Feed(Chinese[2]));
EXPECT_EQ(editor.Text(), Chinese.substr(0, 3));
Type(editor, Chinese.substr(3) + Emoji);
EXPECT_EQ(editor.Cursor(), 10U);
Type(editor, Left);
EXPECT_EQ(editor.Cursor(), 6U);
Type(editor, Left);
EXPECT_EQ(editor.Cursor(), 3U);
Type(editor, Delete);
EXPECT_EQ(editor.Text(), Chinese.substr(0, 3) + Emoji);
Type(editor, "\x7f");
EXPECT_EQ(editor.Text(), Emoji);
EXPECT_EQ(editor.Cursor(), 0U);
Type(editor, Right);
EXPECT_EQ(editor.Cursor(), 4U);
Type(editor, "\x08");
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, MovementClampsAndSupportsAllCommonHomeEndForms)
{
for (const auto home : {Home, std::string_view("\x1bOH"), std::string_view("\x1b[1~"), std::string_view("\x1b[7~")})
{
for (const auto end :
{End, std::string_view("\x1bOF"), std::string_view("\x1b[4~"), std::string_view("\x1b[8~")})
{
LineEditor editor;
Type(editor, Chinese);
Type(editor, home);
EXPECT_EQ(editor.Cursor(), 0U);
Type(editor, Left);
EXPECT_EQ(editor.Cursor(), 0U);
Type(editor, end);
EXPECT_EQ(editor.Cursor(), Chinese.size());
Type(editor, Right);
EXPECT_EQ(editor.Cursor(), Chinese.size());
}
}
}
TEST(LineEditorTest, Ss3LeftRightMoveByCodepoint)
{
LineEditor editor;
Type(editor, Chinese);
Type(editor, "\x1bOD");
EXPECT_EQ(editor.Cursor(), 3U);
Type(editor, "\x1bOC");
EXPECT_EQ(editor.Cursor(), 6U);
}
TEST(LineEditorTest, CtrlAEndKillAndClearRespectCursor)
{
LineEditor editor;
Type(editor, "a" + Chinese + "z");
Type(editor, "\x01");
EXPECT_EQ(editor.Cursor(), 0U);
Type(editor, Right);
Type(editor, Right);
Type(editor, "\x15");
EXPECT_EQ(editor.Text(), Chinese.substr(3) + "z");
EXPECT_EQ(editor.Cursor(), 0U);
Type(editor, Right);
Type(editor, "\x0b");
EXPECT_EQ(editor.Text(), Chinese.substr(3));
Type(editor, "\x05");
EXPECT_EQ(editor.Cursor(), 3U);
Type(editor, "\x15");
EXPECT_TRUE(editor.Text().empty());
}
TEST(LineEditorTest, CtrlDEmptyLatchesEofAndFutureInputIsIgnored)
{
LineEditor editor;
EXPECT_FALSE(editor.Feed('\x04'));
EXPECT_TRUE(editor.IsEof());
Type(editor, "ignored\n\r\x1b[A");
EXPECT_TRUE(editor.Text().empty());
EXPECT_EQ(editor.Cursor(), 0U);
}
TEST(LineEditorTest, CtrlDOnNonemptyLineDeletesCodepointButDoesNotSetEof)
{
LineEditor editor;
Type(editor, Chinese);
Type(editor, "\x04");
EXPECT_EQ(editor.Text(), Chinese);
EXPECT_FALSE(editor.IsEof());
Type(editor, Home);
Type(editor, "\x04");
EXPECT_EQ(editor.Text(), Chinese.substr(3));
Type(editor, "\x04");
EXPECT_TRUE(editor.Text().empty());
EXPECT_FALSE(editor.IsEof());
Type(editor, "\x04");
EXPECT_TRUE(editor.IsEof());
}
TEST(LineEditorTest, UnknownEscapesAreSwallowedAsWholeSequences)
{
LineEditor editor;
Type(editor, "safe");
Type(editor, "\x1b[99~\x1b[?25l\x1b[1;5D\x1bOP\x1b(0\x1bz");
EXPECT_EQ(editor.Text(), "safe");
EXPECT_EQ(editor.Cursor(), 4U);
Type(editor, "!");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("safe!"));
}
TEST(LineEditorTest, OscAndControlStringsAreSwallowedThroughTheirTerminator)
{
LineEditor editor;
Type(editor, "a\x1b]0;window title\x07");
Type(editor, "b\x1bPignored [A payload\x1b\\");
Type(editor, "c\x1b]title\x1b\\");
EXPECT_EQ(editor.Text(), "abc");
}
TEST(LineEditorTest, OverlongEscapeHasBoundedStorageAndDoesNotLeakTail)
{
LineEditor editor;
Type(editor, "safe\x1b[");
Type(editor, std::string(10000, '1'));
Type(editor, "~done");
EXPECT_EQ(editor.Text(), "safedone");
Type(editor, "\x1b]" + std::string(10000, 'x') + "\x07");
Type(editor, "!");
EXPECT_EQ(editor.Text(), "safedone!");
}
TEST(LineEditorTest, NewlineAndNewEscapeRecoverFromTruncatedSequences)
{
LineEditor editor;
Type(editor, "ok\x1b[123");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("ok"));
Type(editor, "draft\x1b[12");
Type(editor, Up);
EXPECT_EQ(editor.Text(), "ok");
Type(editor, Down);
EXPECT_EQ(editor.Text(), "draft");
Type(editor, "\x1b]unterminated");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("draft"));
}
TEST(LineEditorTest, MalformedUtf8IsDiscardedAndAsciiInputRecovers)
{
LineEditor editor;
Type(editor, "\x80\xbf\xc0\xaf\xf5\xff\xe0\x80\x80\xed\xa0\x80\xf4\x90\x80\x80");
EXPECT_TRUE(editor.Text().empty());
Type(editor, "\xe4");
Type(editor, "valid");
EXPECT_EQ(editor.Text(), "valid");
Type(editor, "\xe4\xb8");
Type(editor, Left);
Type(editor, "!");
EXPECT_EQ(editor.Text(), "vali!d");
EXPECT_EQ(editor.Cursor(), 5U);
Type(editor, "\xe4\xb8");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>("vali!d"));
Type(editor, "next");
EXPECT_EQ(editor.Text(), "next");
}
TEST(LineEditorTest, InvalidDecoderPrefixCanBeFollowedByNewValidCodepoint)
{
LineEditor editor;
Type(editor, "\xe4" + Emoji + "\xe4\xb8" + Chinese);
EXPECT_EQ(editor.Text(), Emoji + Chinese);
EXPECT_EQ(editor.Cursor(), Emoji.size() + Chinese.size());
}
TEST(LineEditorTest, EmbeddedNulAndUnsupportedControlsAreNotStored)
{
LineEditor editor;
Type(editor, "a");
EXPECT_FALSE(editor.Feed('\0'));
Type(editor, "\x02\x03\x06\x0c");
Type(editor, "b");
EXPECT_EQ(editor.Text(), "ab");
}
TEST(LineEditorTest, ExactLineLimitAcceptedAndOverflowCanRecoverByEditing)
{
LineEditor editor;
const std::string full(LineEditor::MaxLineBytes, 'x');
Type(editor, full);
EXPECT_EQ(editor.Text(), full);
EXPECT_THROW(editor.Feed('y'), std::length_error);
EXPECT_EQ(editor.Text(), full);
EXPECT_EQ(editor.Cursor(), full.size());
EXPECT_FALSE(editor.IsEof());
Type(editor, "\x7f");
Type(editor, "y");
EXPECT_EQ(editor.Feed('\n'), std::optional<std::string>(full.substr(1) + "y"));
Submit(editor, "next");
}
TEST(LineEditorTest, Utf8OverflowIsAtomicAndDecoderRecovers)
{
LineEditor editor;
const std::string prefix(LineEditor::MaxLineBytes - 2, 'x');
Type(editor, prefix);
EXPECT_FALSE(editor.Feed(Chinese[0]));
EXPECT_FALSE(editor.Feed(Chinese[1]));
EXPECT_THROW(editor.Feed(Chinese[2]), std::length_error);
EXPECT_EQ(editor.Text(), prefix);
EXPECT_EQ(editor.Cursor(), prefix.size());
Type(editor, "\x7f");
Type(editor, Chinese.substr(0, 3));
EXPECT_EQ(editor.Text().size(), LineEditor::MaxLineBytes);
EXPECT_EQ(editor.Cursor(), LineEditor::MaxLineBytes);
Type(editor, "\x7f");
EXPECT_EQ(editor.Text(), prefix.substr(1));
}
} // namespace