using System.Text; using Anubis.Core; namespace Anubis.Tests; /// Italian formats of SPEC 13.3, with the values of the mockups (appendix F). [TestClass] public sealed class FormatTests { private const string Nbsp = " "; private static readonly TimeZoneInfo _rome = TimeZoneInfo.FindSystemTimeZoneById("Europe/Rome"); [TestMethod] [TestCategory(Category.Unit)] [DataRow(1299.84, "1.299,84" + Nbsp + "€")] [DataRow(10412.46, "10.412,46" + Nbsp + "€")] [DataRow(0.0, "0,00" + Nbsp + "€")] [DataRow(-26.0, "−26,00" + Nbsp + "€")] [DataRow(1234567.891, "1.234.567,89" + Nbsp + "€")] public void MoneyUsesCommaDotsAndSymbolAfterNonBreakingSpace(double value, string expected) => Assert.AreEqual(expected, Format.Money((decimal)value)); [TestMethod] [TestCategory(Category.Unit)] public void ResultsCarryPlusAndTypographicMinus() { Assert.AreEqual("+412,46" + Nbsp + "€", Format.Money(412.46m, withSign: true)); Assert.AreEqual("−9,53" + Nbsp + "€", Format.Money(-9.53m, withSign: true)); Assert.AreEqual("0,00" + Nbsp + "€", Format.Money(0m, withSign: true)); Assert.AreEqual("259,80" + Nbsp + "$", Format.Money(259.8m, "USD")); Assert.IsFalse(Format.Money(-1m).Contains('-', StringComparison.Ordinal), "il meno deve essere tipografico"); } [TestMethod] [TestCategory(Category.Unit)] public void RoundingIsHalfAwayFromZero() { Assert.AreEqual("0,13" + Nbsp + "€", Format.Money(0.125m)); Assert.AreEqual("−0,13" + Nbsp + "€", Format.Money(-0.125m)); Assert.AreEqual("0,00" + Nbsp + "€", Format.Money(-0.004m), "un valore che arrotondato è zero non ha segno"); } [TestMethod] [TestCategory(Category.Unit)] public void PercentagesAndNumbers() { Assert.AreEqual("+4,12%", Format.Percent(4.12m, 2, withSign: true)); Assert.AreEqual("−0,26%", Format.Percent(-0.26m, 2, withSign: true)); Assert.AreEqual("64,1%", Format.Percent(64.1m)); Assert.AreEqual("10.412", Format.Number(10412.46m)); Assert.AreEqual("0,0182", Format.Number(0.0182m, 4)); } [TestMethod] [TestCategory(Category.Unit)] public void SharesAlwaysAddUpToOneHundred() { // Appendix F.1: open positions by strategy (F.4) and cash, out of 10.412,46 €. var shares = Format.Shares([2326.50m, 3899.31m, 445.32m, 3741.33m]); Assert.AreEqual(100m, shares.Sum()); CollectionAssert.AreEqual(new[] { 22.3m, 37.5m, 4.3m, 35.9m }, shares.ToArray()); Assert.AreEqual(100m, Format.Shares([1m, 1m, 1m]).Sum(), "tre terzi fanno 100"); CollectionAssert.AreEqual(new[] { 33.4m, 33.3m, 33.3m }, Format.Shares([1m, 1m, 1m]).ToArray()); CollectionAssert.AreEqual(new[] { 0m, 0m }, Format.Shares([0m, -5m]).ToArray()); } [TestMethod] [TestCategory(Category.Unit)] public void SharesHoldForAnyWeights() => Property.ForAll( g => Enumerable.Range(0, g.Int(1, 12)).Select(_ => g.Decimal(0m, 10_000m, 2)).ToArray(), weights => { var shares = Format.Shares(weights, 1); if (weights.Any(w => w > 0)) { Assert.AreEqual(100m, shares.Sum()); } Assert.IsTrue(shares.All(s => s >= 0)); }); [TestMethod] [TestCategory(Category.Unit)] public void SpokenDatesInItalianTime() { var now = new DateTimeOffset(2026, 10, 9, 6, 21, 0, TimeSpan.Zero); // 08:21 in Rome Assert.AreEqual("Oggi 06:10", Format.ShortDate(new DateTimeOffset(2026, 10, 9, 4, 10, 0, TimeSpan.Zero), now, _rome)); Assert.AreEqual("Ieri 21:58", Format.ShortDate(new DateTimeOffset(2026, 10, 8, 19, 58, 0, TimeSpan.Zero), now, _rome)); Assert.AreEqual("mar 6 ott", Format.ShortDate(new DateTimeOffset(2026, 10, 6, 13, 41, 0, TimeSpan.Zero), now, _rome)); Assert.AreEqual("mer 31 dic 2025", Format.ShortDate(new DateTimeOffset(2025, 12, 31, 10, 0, 0, TimeSpan.Zero), now, _rome)); Assert.AreEqual("Venerdì 9 ottobre 2026, ore 08:21", Format.LongDateTime(now, _rome)); Assert.AreEqual("12 agosto 2026", Format.LongDate(new DateOnly(2026, 8, 12))); Assert.AreEqual("4 secondi fa", Format.Ago(TimeSpan.FromSeconds(4))); Assert.AreEqual("1 minuto fa", Format.Ago(TimeSpan.FromSeconds(65))); } [TestMethod] [TestCategory(Category.Unit)] public void CsvHasBomSemicolonsAndDecimalComma() { var bytes = Format.Csv(["Strumento", "Valore €", "Nota"], [["BTC/EUR", 1299.84m, "stop; mobile"], ["AAPL", -2.57m, null]]); CollectionAssert.AreEqual(Encoding.UTF8.GetPreamble(), bytes.Take(3).ToArray()); var text = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3); Assert.AreEqual("Strumento;Valore €;Nota\r\nBTC/EUR;1299,84;\"stop; mobile\"\r\nAAPL;-2,57;\r\n", text); } }