// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // A text field's editing by code point: one to four bytes of UTF-8, the selection, what each kind of field takes, // the input method's composing text and its deletions, and a secret that is wiped when it goes. import std; import Nostro.Ui; #include "../Support/Check.inc" using namespace Nostro::Ui; using Test::Check; using Test::Equal; namespace { // a, é, € and 😀: one, two, three and four bytes; boundaries 0 1 3 6 10. const std::string mixed = "aé€\U0001F600"; void Put(EditState& field, std::string_view value, std::size_t cursor) { field.Set(value); field.MoveTo(cursor); } void Keys() { EditState field; Check(field.Empty() && field.kind == FieldKind::Text, "a new field is empty, and takes prose"); Put(field, mixed, 10); Check(field.Apply(EditKey::Backspace) && field.cursor == 6 && field.value == "aé€", "Backspace takes the four-byte emoji"); Put(field, mixed, 1); Check(field.Apply(EditKey::Delete) && field.cursor == 1 && field.value == "a€\U0001F600", "Delete takes the two-byte é"); Put(field, "abc", 0); Check(!field.Apply(EditKey::Backspace) && field.value == "abc" && field.cursor == 0, "Backspace at the start deletes nothing and says so"); Put(field, "abc", 3); Check(!field.Apply(EditKey::Delete) && field.value == "abc" && field.cursor == 3, "Delete at the end deletes nothing and says so"); Put(field, mixed, 6); Check(field.Apply(EditKey::Left) && field.cursor == 3, "Left moves back over the three-byte €"); Check(field.Apply(EditKey::Right) && field.cursor == 6, "Right moves over it again"); Put(field, "ab", 0); Check(field.Apply(EditKey::Left) && field.cursor == 0, "Left at the start stays, and is still a move"); Put(field, "ab", 2); Check(field.Apply(EditKey::Right) && field.cursor == 2, "Right at the end stays, and is still a move"); Put(field, "hello", 2); Check(field.Apply(EditKey::Home) && field.cursor == 0 && field.Apply(EditKey::End) && field.cursor == 5, "Home and End go to the line's ends"); Put(field, mixed, 2); Equal(field.cursor, std::size_t{1}, "a cursor asked for inside a character goes to its start"); Put(field, mixed, 99); Equal(field.cursor, std::size_t{10}, "and one past the end to the end"); } void Selecting() { EditState field; Put(field, "hello world", 5); Check(!field.HasSelection() && field.Selected().empty(), "a cursor alone selects nothing"); field.Apply(EditKey::Right, true); field.Apply(EditKey::End, true); Check(field.HasSelection() && field.Selected() == " world" && field.SelectionBegin() == 5 && field.SelectionEnd() == 11, "Shift with a move stretches the selection from where it began"); field.Apply(EditKey::Home, true); Check(field.Selected() == "hello" && field.cursor == 0 && field.anchor == 5, "back past where it began, it selects the other way"); field.Apply(EditKey::Left); Check(!field.HasSelection() && field.cursor == 0, "Left without Shift ends a selection at its start"); field.SelectAll(); Check(field.Selected() == "hello world" && field.cursor == 11, "everything selected, the cursor at the end"); field.Apply(EditKey::Right); Check(!field.HasSelection() && field.cursor == 11, "Right ends a selection at its end"); field.SelectAll(); field.Apply(EditKey::Left); Equal(field.cursor, std::size_t{0}, "and Left at its start, wherever the cursor was"); field.SelectAll(); field.Apply(EditKey::Home); Check(!field.HasSelection() && field.cursor == 0, "Home ends it at the start of the line"); Put(field, "hello world", 5); field.Apply(EditKey::End, true); Check(field.Apply(EditKey::Backspace) && field.value == "hello" && field.cursor == 5 && !field.HasSelection(), "Backspace deletes what is selected, and only that"); field.SelectAll(); Check(field.Apply(EditKey::Delete) && field.Empty(), "so does Delete"); Check(!field.DeleteSelection(), "with nothing selected there is nothing to delete"); Put(field, "hello world", 0); field.Apply(EditKey::End, true); field.Commit("x"); Check(field.value == "x" && field.cursor == 1, "what is typed takes the place of the selection"); field.SelectAll(); field.MoveTo(0); Check(!field.HasSelection(), "a tap ends a selection"); } void Kinds() { EditState prose; prose.Commit("a\tb\nc d\x01\x7f"); Equal(prose.value, std::string("a b c d"), "a line of prose: a tab or a line break is a space, a control character nothing"); prose.Set("\xff" "ok\xe2\x82"); Equal(prose.value, std::string("ok"), "malformed UTF-8 is dropped"); prose.Set("a\xEF\xBF\xBD" "b"); Equal(prose.value, std::string("a\xEF\xBF\xBD" "b"), "a replacement character that is one is kept"); EditState plain(FieldKind::Plain); plain.Paste("NL23 BUNQ 2025\n"); Equal(plain.value, std::string("NL23 BUNQ 2025"), "a line taken as typed keeps its spaces and drops a line break around it"); EditState decimal(FieldKind::Decimal); decimal.Paste("€ 1.234,50 EUR"); Equal(decimal.value, std::string("1.234,50"), "an amount takes digits and separators"); EditState digits(FieldKind::Digits); digits.Commit("12,5 x"); Equal(digits.value, std::string("125"), "a whole number takes digits"); EditState secret(FieldKind::Secret); secret.Paste(" sandbox_abc 123\n"); Equal(secret.value, std::string("sandbox_abc123"), "a key has no space in it"); EditState full; full.Set(std::string(EditState::limit - 1, 'a')); full.Commit("é!"); Check(full.value.size() == EditState::limit - 1 && full.cursor == full.value.size(), "a field takes no more than it holds, and no half character"); full.Commit("b"); Equal(full.value.size(), EditState::limit, "what still fits is taken"); full.Commit("c"); Equal(full.value.size(), EditState::limit, "and nothing after that"); } void Composing() { EditState field; Put(field, "hello", 2); field.preedit = "XY"; Check(field.Shown() == "heXYllo" && field.ShownCursor() == 4 && !field.Empty(), "the composing text shows at the cursor"); field.Commit("Z"); Check(field.value == "heZllo" && field.preedit.empty() && field.cursor == 3, "what the input method commits takes the place of what it composed"); field.preedit = "W"; field.Paste("!"); Check(field.value == "heZW!llo" && field.preedit.empty(), "what is pasted follows the composing word, which is kept"); // The input method's deletions: byte counts around the cursor, rounded out to code points. Put(field, mixed, 6); field.preedit = "z"; field.DeleteAround(1, 1); Check(field.cursor == 3 && field.preedit == "z" && field.value == "aé", "one byte each side takes the € before and the 😀 after; the composing text stays"); Put(field, "abc", 1); field.DeleteAround(5, 0); Check(field.cursor == 0 && field.value == "bc", "a deletion past the start stops at the start"); Put(field, "abc", 2); field.DeleteAround(0, 9); Check(field.cursor == 2 && field.value == "ab", "and one past the end at the end"); field.DeleteAround(0, 0); Check(field.cursor == 2 && field.value == "ab", "a deletion of nothing leaves the text"); Put(field, "hello wor", 9); field.preedit = "ld"; field.CommitPreedit(); Check(field.cursor == 11 && field.preedit.empty() && field.value == "hello world", "the composing text taken into the value, the cursor past it"); field.CommitPreedit(); Equal(field.value, std::string("hello world"), "with no composing text nothing changes"); EditState empty; empty.preedit = "x"; Check(!empty.Empty(), "a field with only a composing text is not empty"); empty.Clear(); Check(empty.Empty() && empty.cursor == 0 && empty.anchor == 0, "cleared, nothing is left"); } void Secrets() { EditState secret(FieldKind::Secret); Check(secret.value.capacity() >= EditState::secretLimit, "a secret's buffer is its full size from the start"); // libc++'s string keeps its buffer when it is cleared, so its address can still be read: the bytes of the key // must be gone from it. const std::string_view buffer(secret.value.data(), EditState::secretLimit); secret.Commit("sandbox_0123456789abcdef"); Check(secret.value.data() == buffer.data(), "typing into it moves nothing"); secret.Apply(EditKey::Backspace); secret.preedit = "composing"; secret.CommitPreedit(); Check(secret.value == "sandbox_0123456789abcdecomposing" && secret.value.data() == buffer.data(), "nor does an edit"); secret.Commit(std::string(600, 'k')); Check(secret.value.size() == EditState::secretLimit && secret.value.data() == buffer.data(), "it takes no more than its buffer holds"); secret.Clear(); Check(secret.Empty() && buffer.find_first_not_of('\0') == std::string_view::npos, "cleared, no byte of it is left in memory"); { EditState gone(FieldKind::Secret); gone.Commit("another-key"); } Check(true, "a secret that goes out of scope is cleared first"); } } int main() { Keys(); Selecting(); Kinds(); Composing(); Secrets(); return Test::Result(); }