This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:35:23 +01:00
commit 1ea50c50bc
62 changed files with 9982 additions and 608 deletions

View file

@ -0,0 +1,106 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Currencies and amounts (Nostro:Currency) and the text helpers (Nostro:Utils): every parse with what it refuses,
// every format, and the local-time helpers with a fixed time zone so the expectations hold anywhere.
#include <stdlib.h>
#include <time.h>
import std;
import Nostro;
#include "../Support/Check.inc"
using namespace Nostro;
using Test::Check;
using Test::Equal;
using Test::Throws;
namespace {
void Currencies() {
Check(GetCurrency("EUR") == Currency::Euro, "EUR is the euro");
Check(GetCurrency("eur") == Currency::Euro, "eur is the euro");
Throws([] { GetCurrency("USD"); }, "unsupported currency 'USD'", "an unknown currency is refused");
Equal(GetCurrencyCode(Currency::Euro), std::string_view("EUR"), "code");
Equal(GetCurrencySymbol(Currency::Euro), std::string_view("€"), "symbol");
Equal(GetCurrencyDecimals(Currency::Euro), std::uint8_t{2}, "decimals");
Equal(std::format("{}", Currency::Euro), std::string("€"), "a currency formats as its symbol");
}
void Amounts() {
Equal(ParseAmount("12.50", Currency::Euro), std::int64_t{1250}, "a whole amount with cents");
Equal(ParseAmount("12", Currency::Euro), std::int64_t{1200}, "an amount without a fraction");
Equal(ParseAmount("12.5", Currency::Euro), std::int64_t{1250}, "a fraction shorter than the decimals");
Equal(ParseAmount("-0.05", Currency::Euro), std::int64_t{-5}, "a negative amount");
for(const std::string_view bad : {"", ".5", "-", "1.234", "1a.00", "1.0x", "+1"}) {
Throws([bad] { ParseAmount(bad, Currency::Euro); }, "malformed amount", std::format("'{}' is refused", bad));
}
Equal(FormatAmount(1250, Currency::Euro), std::string("12.50"), "cents format with a point");
Equal(FormatAmount(5, Currency::Euro), std::string("0.05"), "less than a unit gets its leading zeros");
Equal(FormatAmount(0, Currency::Euro), std::string("0.00"), "zero");
Equal(FormatAmount(-5, Currency::Euro), std::string("-0.05"), "a negative amount");
Equal(FormatAmount(std::numeric_limits<std::int64_t>::min(), Currency::Euro), std::string("-92233720368547758.08"), "the most negative amount");
Equal(FormatMoney(-102, Currency::Euro), std::string("-€1.02"), "money puts the sign before the symbol");
Equal(FormatMoney(102, Currency::Euro), std::string("€1.02"), "and no sign when positive");
}
void Escaping() {
Equal(EscB("a\"b\\c\nd\re\tf\x01g"), std::string("a\\\"b\\\\c\\nd\\re\\tf\\u0001g"), "JSON escaping of every kind of character");
}
void Timestamps() {
using namespace std::chrono;
const system_clock::time_point time = sys_days(year(2026) / 10 / 31) + hours(12) + minutes(34) + seconds(56) + microseconds(123456);
Check(ParseTimestamp("2026-10-31 12:34:56.123456") == time, "a timestamp with microseconds");
Check(ParseTimestamp("2026-10-31 12:34:56.1") == time - microseconds(23456), "a short fraction counts in tenths");
Check(ParseTimestamp("2026-10-31 12:34:56") == time - microseconds(123456), "a timestamp without a fraction");
for(const std::string_view bad : {"2026-10-31", "2026/10-31 12:34:56", "2026-10/31 12:34:56", "2026-10-31T12:34:56", "2026-10-31 12-34:56", "2026-10-31 12:34-56", "20x6-10-31 12:34:56", "2026-10-31 12:34:56.", "2026-10-31 12:34:56.1234567", "2026-10-31 12:34:56x1", "2026-02-30 12:34:56", "2026-10-31 12:34:5x"}) {
Throws([bad] { ParseTimestamp(bad); }, "malformed timestamp", std::format("'{}' is refused", bad));
}
Equal(FormatTimestamp(time), std::string("2026-10-31 12:34:56.123456"), "FormatTimestamp is ParseTimestamp's inverse");
}
void LocalTime() {
// Amsterdam: the clocks went back on 2026-10-25, so that day lasted 25 hours.
setenv("TZ", "Europe/Amsterdam", 1);
tzset();
using namespace std::chrono;
Check(ParseLocalDate("2026-10-31") == sys_days(year(2026) / 10 / 30) + hours(23), "a date is local midnight");
Check(ParseLocalDate("2026-10-31 09:30") == sys_days(year(2026) / 10 / 31) + hours(8) + minutes(30), "a date and time is local");
Check(ParseLocalDate("2026-10-25", 1) - ParseLocalDate("2026-10-25") == hours(25), "a day of the clock change counts by the calendar");
Check(ParseLocalDate("2026-10-31", 1) == ParseLocalDate("2026-11-01"), "a day on crosses the month");
for(const std::string_view bad : {"2026-10-3", "2026/10/31", "2026-10-31T09:30", "2026-10-31 09-30", "2026-1x-31", "2026-02-30", "2026-10-31 24:00", "2026-10-31 23:60"}) {
Throws([bad] { ParseLocalDate(bad); }, "is not a date", std::format("'{}' is refused", bad));
}
Equal(FormatLocalTime(sys_days(year(2026) / 10 / 31) + hours(8) + minutes(30)), std::string("2026-10-31 09:30"), "local time format");
}
void Urls() {
Equal(UrlEncode("a-b_c.d~e f/g?h=i&j"), std::string("a-b_c.d~e%20f%2Fg%3Fh%3Di%26j"), "everything but the unreserved characters is encoded");
Equal(UrlEncode("é"), std::string("%C3%A9"), "bytes above 0x7F are encoded one by one");
Equal(UrlDecode("a%20b+c%2Fd%2f"), std::string("a b c/d/"), "percent escapes and a plus decode");
Equal(UrlDecode("100%"), std::string("100%"), "a bare percent stays");
Equal(UrlDecode("%zz%2"), std::string("%zz%2"), "an escape without two hex digits stays");
Equal(UrlDecode("%Aa"), std::string("\xaa"), "hex digits of either case");
const std::string_view url = "https://example.com/cb?code=a%2Bb&state=x+y&empty=&flag#state=fragment";
Equal(QueryParameter(url, "code").value_or("none"), std::string("a+b"), "a parameter is decoded");
Equal(QueryParameter(url, "state").value_or("none"), std::string("x y"), "the fragment is not part of the query");
Equal(QueryParameter(url, "empty").value_or("none"), std::string(""), "an empty value");
Equal(QueryParameter(url, "flag").value_or("none"), std::string(""), "a name without a value");
Check(!QueryParameter(url, "missing"), "a parameter that is not there");
Check(!QueryParameter("https://example.com/cb", "code"), "a URL without a query");
}
}
int main() {
Currencies();
Amounts();
Escaping();
Timestamps();
LocalTime();
Urls();
return Test::Result();
}