Adds the building blocks every later screen uses, so modules are built from one set of components instead of ad hoc markup: - Form components (KhForm, KhTextBox, KhNumberBox, KhDateBox, KhSelect, KhCheck, KhSwitch, KhHotlink) with Italian number and date input, inline calculation (=10*3), field messages plus a summary that focuses the field, and service errors added by field name. - Display and window components (panels, tiles, badges, chips, money, tabs with Alt+1..9, steps, dialog, side drawer and DrawerService). - KhIconToolbar that keeps one line and moves less used commands into the "..." menu, KhDocHead, and the RecordPage/DocumentPage/MasterDataPage bases with standard commands, shortcuts and save/cancel/delete flow (#27, #29). - Development-only component catalog and sample document page. - Profile: personal data, password change that ends other sessions, TOTP enable/disable and recovery codes through ProfileService (#31). Refs #27 #29 #31 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
6.2 KiB
C#
187 lines
6.2 KiB
C#
using System.Globalization;
|
|
using Khaemhat.Web.Shell;
|
|
using Khaemhat.Web.Ui;
|
|
|
|
namespace Khaemhat.Web.Tests.Ui;
|
|
|
|
/// <summary>Numbers, inline calculations and dates typed the Italian way (SPEC §10.7, issue #27).</summary>
|
|
[TestClass]
|
|
public sealed class ItalianInputTests
|
|
{
|
|
private static readonly DateOnly Today = new(2026, 11, 11);
|
|
|
|
[TestMethod]
|
|
[DataRow("1234,5", "1234.5")]
|
|
[DataRow("1.234,56", "1234.56")]
|
|
[DataRow("1.234.567", "1234567")]
|
|
[DataRow("-12,5", "-12.5")]
|
|
[DataRow("12.5", "12.5")]
|
|
[DataRow("1.234", "1234")]
|
|
[DataRow(" 42 ", "42")]
|
|
[DataRow("=10*3", "30")]
|
|
[DataRow("=100/3", "33.333333333333333333333333333")]
|
|
[DataRow("=(2+3)x4", "20")]
|
|
[DataRow("=12:4", "3")]
|
|
[DataRow("=10,5+0,5", "11")]
|
|
[DataRow("=-2*-3", "6")]
|
|
public void Number_ReadsItalianFormsAndCalculations(string text, string expected)
|
|
{
|
|
Assert.IsTrue(ItalianNumber.TryParse(text, out var value), text);
|
|
Assert.AreEqual(decimal.Parse(expected, CultureInfo.InvariantCulture), value, text);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("abc")]
|
|
[DataRow("12,3,4")]
|
|
[DataRow("=10/0")]
|
|
[DataRow("=(1+2")]
|
|
[DataRow("=1+")]
|
|
[DataRow("=")]
|
|
public void Number_RefusesWhatIsNotANumber(string text) => Assert.IsFalse(ItalianNumber.TryParse(text, out _), text);
|
|
|
|
[TestMethod]
|
|
public void Number_EmptyIsNull()
|
|
{
|
|
Assert.IsTrue(ItalianNumber.TryParse(" ", out var value));
|
|
Assert.IsNull(value);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Number_FormatsWithTheCulture()
|
|
{
|
|
var italian = CultureInfo.GetCultureInfo("it-IT");
|
|
Assert.AreEqual("1.234,50", ItalianNumber.Format(1234.5m, 2, italian));
|
|
Assert.AreEqual(string.Empty, ItalianNumber.Format(null, 2, italian));
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("T", 2026, 11, 11)]
|
|
[DataRow("o", 2026, 11, 11)]
|
|
[DataRow("F", 2026, 11, 30)]
|
|
[DataRow("I", 2026, 11, 1)]
|
|
[DataRow("+", 2026, 11, 12)]
|
|
[DataRow("-", 2026, 11, 10)]
|
|
[DataRow("+30", 2026, 12, 11)]
|
|
[DataRow("-11", 2026, 10, 31)]
|
|
[DataRow("5", 2026, 11, 5)]
|
|
[DataRow("5/3", 2026, 3, 5)]
|
|
[DataRow("05/03/2027", 2027, 3, 5)]
|
|
[DataRow("5-3-26", 2026, 3, 5)]
|
|
[DataRow("5.3.26", 2026, 3, 5)]
|
|
[DataRow("0503", 2026, 3, 5)]
|
|
[DataRow("050326", 2026, 3, 5)]
|
|
[DataRow("05032026", 2026, 3, 5)]
|
|
[DataRow("1/1/60", 1960, 1, 1)]
|
|
[DataRow("1/1/46", 2046, 1, 1)]
|
|
[DataRow("29/2/28", 2028, 2, 29)]
|
|
public void Date_ReadsShortFormsAndLetters(string text, int year, int month, int day)
|
|
{
|
|
Assert.IsTrue(ItalianDate.TryParse(text, Today, null, out var value), text);
|
|
Assert.AreEqual(new DateOnly(year, month, day), value, text);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Date_RelativeFormsStartFromTheCurrentValue()
|
|
{
|
|
var current = new DateOnly(2026, 2, 10);
|
|
Assert.IsTrue(ItalianDate.TryParse("F", Today, current, out var end));
|
|
Assert.AreEqual(new DateOnly(2026, 2, 28), end);
|
|
Assert.IsTrue(ItalianDate.TryParse("+7", Today, current, out var week));
|
|
Assert.AreEqual(new DateOnly(2026, 2, 17), week);
|
|
Assert.IsTrue(ItalianDate.TryParse("T", Today, current, out var today));
|
|
Assert.AreEqual(Today, today);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("31/11/2026")]
|
|
[DataRow("29/2/2027")]
|
|
[DataRow("0/1")]
|
|
[DataRow("1/13")]
|
|
[DataRow("123")]
|
|
[DataRow("ieri")]
|
|
[DataRow("1/2/3/4")]
|
|
public void Date_RefusesWhatIsNotADate(string text) => Assert.IsFalse(ItalianDate.TryParse(text, Today, null, out _), text);
|
|
|
|
[TestMethod]
|
|
public void Date_EmptyIsNull()
|
|
{
|
|
Assert.IsTrue(ItalianDate.TryParse(string.Empty, Today, Today, out var value));
|
|
Assert.IsNull(value);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void Validation_CollectsFieldMessagesAndRaisesChanged()
|
|
{
|
|
var validation = new KhValidation();
|
|
var changes = 0;
|
|
validation.Changed += () => changes++;
|
|
|
|
validation.Add("number", "taken");
|
|
validation.Add(null, "whole form");
|
|
Assert.IsFalse(validation.IsValid);
|
|
CollectionAssert.AreEqual(new[] { "taken" }, validation.For("number").ToArray());
|
|
|
|
validation.ClearField("number");
|
|
validation.ClearField("number");
|
|
Assert.HasCount(1, validation.Messages);
|
|
validation.Clear();
|
|
Assert.IsTrue(validation.IsValid);
|
|
Assert.AreEqual(4, changes);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void FormContext_ValidatesEveryRegisteredField()
|
|
{
|
|
var context = new KhFormContext(new KhValidation());
|
|
var empty = new FakeField("name", "Nome", "Campo obbligatorio.");
|
|
var good = new FakeField("code", "Codice");
|
|
context.Register(empty);
|
|
context.Register(good);
|
|
|
|
Assert.IsFalse(context.Validate());
|
|
var message = context.Validation.Messages.Single();
|
|
Assert.AreEqual("name", message.Field);
|
|
Assert.AreEqual("Nome", message.Label);
|
|
Assert.AreEqual("in-name", message.InputId);
|
|
Assert.AreSame(good, context.Find("code"));
|
|
|
|
context.Unregister(empty);
|
|
Assert.IsTrue(context.Validate());
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("page.tab.1", 1)]
|
|
[DataRow("page.tab.9", 9)]
|
|
public void PageTab_ReadsTheNumber(string command, int expected)
|
|
{
|
|
Assert.IsTrue(KeyboardShortcuts.TryGetTabNumber(command, out var number));
|
|
Assert.AreEqual(expected, number);
|
|
}
|
|
|
|
[TestMethod]
|
|
[DataRow("page.tab.0")]
|
|
[DataRow("page.tab.10")]
|
|
[DataRow("page.tab.")]
|
|
[DataRow("page.save")]
|
|
public void PageTab_RefusesOtherCommands(string command) => Assert.IsFalse(KeyboardShortcuts.TryGetTabNumber(command, out _));
|
|
|
|
[TestMethod]
|
|
public void Pretty_ShowsArrowsAsSymbols()
|
|
{
|
|
Assert.AreEqual("Ctrl+↑", KeyboardShortcuts.Pretty("Ctrl+ArrowUp"));
|
|
Assert.AreEqual("Esc", KeyboardShortcuts.Pretty("Escape"));
|
|
Assert.AreEqual(string.Empty, KeyboardShortcuts.Pretty(null));
|
|
}
|
|
|
|
private sealed class FakeField(string name, string label, params string[] messages) : IKhField
|
|
{
|
|
public string FieldName => name;
|
|
|
|
public string? Label => label;
|
|
|
|
public string InputId => "in-" + name;
|
|
|
|
public IEnumerable<string> Validate() => messages;
|
|
}
|
|
}
|