Nostro/tests/ShouldWrapText/main.cpp

152 lines
11 KiB
C++
Raw Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Nostro.Ui's geometry and text: boxes and colours, the size classes, UTF-8 by code point, widths, the tap that
// finds a byte, the ellipsis and the word wrap. The measure is a fixed one, every code point ten pixels wide whatever
// its size, so every offset and width below is exact.
import std;
import Nostro.Ui;
#include "../Support/Check.inc"
using namespace Nostro::Ui;
using Test::Check;
using Test::Equal;
namespace {
TextMeasure Fixed() {
TextMeasure measure;
measure.advance = [](FontFace, std::uint32_t, float) { return 10.0f; };
measure.ascent = [](FontFace, float px) { return px * 0.8f; };
measure.lineHeight = [](FontFace, float px) { return px * 1.25f; };
return measure;
}
// Lines as text, to compare and to print: each as its bytes and its width.
std::string Show(const std::vector<WrappedLine>& lines) {
std::string shown;
for(const WrappedLine& line : lines) {
shown += std::format("[{},{}){} ", line.begin, line.end, line.width);
}
return shown;
}
void Lines(const std::vector<WrappedLine>& actual, const std::vector<WrappedLine>& expected, std::string_view what, std::source_location where = std::source_location::current()) {
Equal(Show(actual), Show(expected), what, where);
}
void Geometry() {
const Box box{10, 20, 30, 40};
Check(box.Right() == 40 && box.Bottom() == 60 && box.Center() == Point{25, 40}, "a box's edges and its middle");
Check(box.Contains({10, 20}) && box.Contains({39, 59}) && !box.Contains({40, 20}) && !box.Contains({10, 60}) && !box.Contains({9, 30}) && !box.Contains({20, 19}), "the left and top edges are inside, the right and bottom ones are not");
Check(!box.Empty() && Box{0, 0, 0, 5}.Empty() && Box{0, 0, 5, 0}.Empty(), "a box without a width or a height is empty");
Equal(box.Inset(Insets::All(5)) == Box{15, 25, 20, 30}, true, "insets take from every side");
Equal(box.Inset(Insets::Symmetric(5, 10)) == Box{15, 30, 20, 20}, true, "or from the sides and from top and bottom");
Equal(box.Inset(Insets::All(50)) == Box{60, 70, 0, 0}, true, "insets that leave nothing leave an empty box, not one inside out");
Equal(box.Intersect({30, 50, 100, 100}) == Box{30, 50, 10, 10}, true, "what two boxes share");
Check(box.Intersect({40, 20, 10, 10}).Empty() && box.Intersect({10, 60, 10, 10}).Empty(), "boxes that share an edge share nothing");
Check(box.Overlaps({39, 59, 5, 5}) && !box.Overlaps({100, 100, 5, 5}), "whether two boxes overlap");
const Color red{1, 0, 0};
Check(red.WithAlpha(0.5f) == Color{1, 0, 0, 0.5f}, "a colour at another opacity");
Check(red.Mixed({0, 0, 1}, 0.5f) == Color{0.5f, 0, 0.5f, 1}, "a colour mixed half with another");
Check(Color{1, 1, 1}.Luminance() > 0.99f && Color{0, 0, 0}.Luminance() == 0 && Color{0, 1, 0}.Luminance() > Color{0, 0, 1}.Luminance(), "green is brighter to the eye than blue");
Check(Metrics{1}.Dp(16) == 16 && Metrics{1.75f}.Dp(16) == 28 && Metrics{2.75f}.Dp(1) == 3, "a size of the design in whole pixels at the window's scale");
Check(SizeClassFor(360, 1) == SizeClass::Compact && SizeClassFor(599, 1) == SizeClass::Compact, "a phone held upright is compact");
Check(SizeClassFor(600, 1) == SizeClass::Medium && SizeClassFor(899, 1) == SizeClass::Medium, "a narrow window is medium");
Check(SizeClassFor(900, 1) == SizeClass::Expanded, "a desktop window is expanded");
Check(SizeClassFor(1080, 3) == SizeClass::Compact && SizeClassFor(1920, 1.75f) == SizeClass::Expanded, "the class goes by the width at the window's scale");
Check(SizeClassFor(700, 0) == SizeClass::Medium, "a scale of nothing is a scale of one");
}
void Utf8() {
// a, é, € and 😀: one, two, three and four bytes; boundaries 0 1 3 6 10.
const std::string mixed = "aé€\U0001F600";
Equal(mixed.size(), std::size_t{10}, "the sample is ten bytes");
std::size_t i = 0;
Check(DecodeUtf8(mixed, i) == 'a' && i == 1 && DecodeUtf8(mixed, i) == 0xE9 && i == 3 && DecodeUtf8(mixed, i) == 0x20AC && i == 6 && DecodeUtf8(mixed, i) == 0x1F600 && i == 10, "one to four bytes decode to one code point each");
Check(DecodeUtf8(mixed, i) == 0 && i == 10, "nothing is left at the end");
i = 0;
Check(DecodeUtf8("\xff", i) == 0xFFFD && i == 1, "a stray byte is consumed as a replacement");
i = 0;
Check(DecodeUtf8("\xe2\x82", i) == 0xFFFD && i == 2, "a sequence the text's end cuts short");
i = 0;
Check(DecodeUtf8("\xe2x", i) == 0xFFFD && i == 1 && DecodeUtf8("\xe2x", i) == 'x', "a sequence another character cuts short leaves that character");
Check(NextCodepoint(mixed, 0) == 1 && NextCodepoint(mixed, 1) == 3 && NextCodepoint(mixed, 3) == 6 && NextCodepoint(mixed, 6) == 10 && NextCodepoint(mixed, 10) == 10, "stepping forward over one to four bytes, and staying at the end");
Check(PreviousCodepoint(mixed, 10) == 6 && PreviousCodepoint(mixed, 6) == 3 && PreviousCodepoint(mixed, 3) == 1 && PreviousCodepoint(mixed, 1) == 0 && PreviousCodepoint(mixed, 0) == 0, "stepping back, and staying at the start");
Equal(PreviousCodepoint(mixed, 99), std::size_t{6}, "from past the end is from the end");
Check(CountCodepoints(mixed) == 4 && CountCodepoints("") == 0, "code points counted");
}
void Measuring() {
const TextMeasure measure = Fixed();
Check(TextWidth(measure, "héllo", FontFace::Regular, 16) == 50 && TextWidth(measure, "", FontFace::Regular, 16) == 0, "a width counts code points, not bytes");
Equal(TextWidth(measure, "a\nb", FontFace::Regular, 16), 20.0f, "a newline has no width");
Equal(NearestByte(measure, "héllo", FontFace::Regular, 16, -5), std::size_t{0}, "a tap before the line is its start");
Equal(NearestByte(measure, "héllo", FontFace::Regular, 16, 4), std::size_t{0}, "nearer the start of a character than its end");
Equal(NearestByte(measure, "héllo", FontFace::Regular, 16, 6), std::size_t{1}, "nearer its end");
Equal(NearestByte(measure, "héllo", FontFace::Regular, 16, 19), std::size_t{3}, "past a two-byte character, by its bytes");
Equal(NearestByte(measure, "héllo", FontFace::Regular, 16, 500), std::size_t{6}, "a tap past the line is its end");
Equal(CenteredBaseline(measure, {0, 100, 50, 40}, FontFace::Regular, 16), 100 + (40 - 20) / 2.0f + 12.8f, "the baseline that centres a line in a box");
Equal(Ellipsize(measure, "hello world", FontFace::Regular, 16, 200), std::string("hello world"), "an ellipsis: it fits");
Equal(Ellipsize(measure, "hello world", FontFace::Regular, 16, 110), std::string("hello world"), "it fits exactly");
Equal(Ellipsize(measure, "hello world", FontFace::Regular, 16, 60), std::string("hello…"), "cut with room for the mark");
Equal(Ellipsize(measure, "x", FontFace::Regular, 16, 5), std::string("…"), "nothing fits");
Equal(Ellipsize(measure, "line one\nline two", FontFace::Regular, 16, 200), std::string("line one…"), "a line break cuts too");
}
void Wrapping() {
WordWrapper wrap(Fixed());
const auto lines = [&wrap](std::string_view text, float width) { return wrap.Wrap(text, FontFace::Regular, 10, width); };
Lines(lines("hello world", 200), {{0, 11, 110}}, "fits on one line");
Lines(lines("hello world", 60), {{0, 5, 50}, {6, 11, 50}}, "breaks at the space, the space in neither line");
Lines(lines("hello world", 60), {{0, 5, 50}, {7, 12, 50}}, "a run of spaces is skipped at the break");
Lines(lines("ab cdef", 50), {{0, 2, 20}, {4, 8, 40}}, "a run of spaces before the overflow goes to neither line");
Lines(lines("abcdef ghi", 60), {{0, 6, 60}, {7, 10, 30}}, "a split forced at a space starts the next line after it");
Lines(lines("abcdefghij", 35), {{0, 3, 30}, {3, 6, 30}, {6, 9, 30}, {9, 10, 10}}, "a long word splits between code points");
Lines(lines("a\nb", 100), {{0, 1, 10}, {2, 3, 10}}, "a newline is a hard break");
Lines(lines("a\n\nb", 100), {{0, 1, 10}, {2, 2, 0}, {3, 4, 10}}, "an empty line between two newlines");
Lines(lines("ab\n", 50), {{0, 2, 20}, {3, 3, 0}}, "a text ending in a newline has an empty last line");
Lines(lines("", 100), {{0, 0, 0}}, "empty text is one empty line");
Lines(lines("well-known word", 60), {{0, 5, 50}, {5, 10, 50}, {11, 15, 40}}, "a hyphen is a break that stays on its line");
Lines(lines("héllo wörld", 60), {{0, 6, 50}, {7, 13, 50}}, "multi-byte code points count once");
Lines(lines("x", 5), {{0, 1, 10}}, "a single code point wider than the line is kept");
Lines(lines("ab cd ef", 50), {{0, 5, 50}, {6, 8, 20}}, "a space that overflows ends the line after the words that fit");
Lines(lines("abcde ", 50), {{0, 5, 50}}, "a trailing space that overflows opens no empty line");
Lines(lines("abcd ", 50), {{0, 4, 40}}, "nor does a run of them");
Lines(lines(" \nabc", 30), {{0, 3, 30}, {6, 9, 30}}, "a run of spaces wider than the line, before a newline: the split forced at a space opens no blank line either");
Lines(lines("x\n ", 30), {{0, 1, 10}, {2, 5, 30}}, "nor at the text's end");
Lines(lines("abcde \nx", 50), {{0, 5, 50}, {7, 8, 10}}, "a space that overflows before a newline leaves no blank line between the two");
Lines(lines("abcde \n", 50), {{0, 5, 50}, {7, 7, 0}}, "a text ending in a newline keeps its empty last line");
Lines(lines(" abcdef", 30), {{0, 3, 30}, {3, 6, 30}, {6, 7, 10}}, "a leading space is no break: no empty first line, the space stays on its line");
Lines(lines("x\n abcd", 30), {{0, 1, 10}, {2, 5, 30}, {5, 7, 20}}, "nor is a space right after a newline");
Lines(lines(" abcdef", 30), {{0, 3, 30}, {3, 6, 30}, {6, 8, 20}}, "nor is a run of them: the line opens with its first word, the spaces before it on that line");
Lines(lines("x\n abcd", 30), {{0, 1, 10}, {2, 5, 30}, {5, 8, 30}}, "the same after a newline");
Lines(lines("abcd ef", 50), {{0, 4, 40}, {6, 8, 20}}, "a space that overflows right behind another: the line ends before the run, and the next begins after it");
const std::vector<WrappedLine>& first = wrap.Wrap("cached text", FontFace::Regular, 10, 60);
const std::vector<WrappedLine>& again = wrap.Wrap("cached text", FontFace::Regular, 10, 60);
Check(&first == &again, "the same text, font, size and width come from the cache");
const std::size_t before = wrap.CacheSize();
wrap.Wrap("cached text", FontFace::Regular, 10, 70);
wrap.Wrap("cached text", FontFace::Medium, 10, 60);
wrap.Wrap("cached text", FontFace::Regular, 12, 60);
Equal(wrap.CacheSize(), before + 3, "another width, font or size is another entry");
for(std::size_t i = 0; i < WordWrapper::cacheLimit + 1; i++) {
wrap.Wrap(std::format("text {}", i), FontFace::Regular, 10, 60);
}
Check(wrap.CacheSize() <= WordWrapper::cacheLimit, "a full cache is emptied, not grown");
Check(wrap.Measure().advance(FontFace::Regular, 'x', 10) == 10, "the wrapper gives out the measure it wraps with");
}
}
int main() {
Geometry();
Utf8();
Measuring();
Wrapping();
return Test::Result();
}