using System.Runtime.CompilerServices; using System.Text.RegularExpressions; using AIStudio.Components; namespace AIStudio.Tests.Components; /// /// Checks that every text field with a validation checks its text again while the user types. /// /// /// Without Immediate or a DebounceInterval, a MudTextField passes its text on only when the user leaves it, so an /// issue stays visible while the user types the text which resolves it. A field with a DebounceInterval binds its /// Value, not its Text: bound by its text, it would delay the texts its parent sets as well, e.g., by a reset, and /// check them without having been touched, see TextFieldDefaults. A read-only field takes no typing and needs neither. /// [TestFixture] public sealed partial class ValidatedTextFieldTests { private const string CHECKS_ON_LEAVING = "checks its text only when the user leaves it"; private const string DEBOUNCED_BY_TEXT = "binds its text, so it delays and checks the texts its parent sets as well"; [TestCase("""""", ExpectedResult = "")] [TestCase("""""", ExpectedResult = "")] [TestCase("""""", ExpectedResult = "")] [TestCase("""""", ExpectedResult = "")] [TestCase("""""", ExpectedResult = "1: " + CHECKS_ON_LEAVING)] [TestCase("""""", ExpectedResult = "1: " + CHECKS_ON_LEAVING)] [TestCase("""""", ExpectedResult = "1: " + DEBOUNCED_BY_TEXT)] [TestCase("""""", ExpectedResult = "1: " + DEBOUNCED_BY_TEXT)] [TestCase(""" b)")" Validation="@this.Validate"/>""", ExpectedResult = "1: " + CHECKS_ON_LEAVING)] [TestCase(""" """, ExpectedResult = "6: " + CHECKS_ON_LEAVING)] public string AValidatedTextFieldIsJudgedByHowItChecksWhileTheUserTypes(string razor) => string.Join(" | ", ValidatedTextFields(razor).SelectMany(field => IssuesOf(field).Select(issue => $"{field.Line}: {issue}"))); [Test] public void EveryValidatedTextFieldChecksWhileTheUserTypes() { var appDirectory = AppSourceDirectory(); var fields = 0; var issues = new List(); foreach (var file in RazorFiles(appDirectory)) { foreach (var field in ValidatedTextFields(File.ReadAllText(file))) { fields++; issues.AddRange(IssuesOf(field).Select(issue => $"{Path.GetRelativePath(appDirectory, file)}:{field.Line}: {issue}")); } } Assert.That(fields, Is.GreaterThan(50), "The test found too few text fields with a validation, so it probably misreads the Razor files."); Assert.That(issues, Is.Empty, $"Give these text fields DebounceInterval=\"@{nameof(TextFieldDefaults)}.{nameof(TextFieldDefaults.VALIDATION_DEBOUNCE_MILLISECONDS)}\" and bind their Value:\n{string.Join('\n', issues)}"); } /// /// A MudTextField of a Razor source, from its opening angle bracket to its closing one. /// /// The line on which the field starts, counted from 1. /// The markup of the field. private sealed record TextFieldTag(int Line, string Source); /// /// Finds every MudTextField with a validation which takes typing, i.e., which is not read-only. /// private static IEnumerable ValidatedTextFields(string razor) { foreach (Match start in TextFieldStartRegex().Matches(razor)) { var tag = razor[start.Index..(TagEnd(razor, start.Index + start.Length) + 1)]; if (!ValidationRegex().IsMatch(tag) || ReadOnlyRegex().IsMatch(tag)) continue; yield return new TextFieldTag(razor[..start.Index].Count(character => character == '\n') + 1, tag); } } private static IEnumerable IssuesOf(TextFieldTag field) { var isDebounced = DebounceRegex().IsMatch(field.Source); if (!isDebounced && !ImmediateRegex().IsMatch(field.Source)) yield return CHECKS_ON_LEAVING; if (isDebounced && TextBindingRegex().IsMatch(field.Source)) yield return DEBOUNCED_BY_TEXT; } /// /// Finds the angle bracket which closes a tag. /// /// /// An attribute value may hold an angle bracket of its own, e.g., in a lambda or a comparison, and the C# code /// inside its parentheses may hold strings with quotes, as @T("…") does. So the search skips attribute values, /// counts parentheses, and skips the strings inside them. /// private static int TagEnd(string razor, int position) { var depth = 0; var isInsideValue = false; for (; position < razor.Length; position++) { var character = razor[position]; if (depth > 0 && character == '"') { position = StringEnd(razor, position); continue; } switch (character) { case '(' or '[' or '{': depth++; break; case ')' or ']' or '}': depth--; break; case '"' when depth == 0: isInsideValue = !isInsideValue; break; case '>' when depth == 0 && !isInsideValue: return position; } } throw new InvalidOperationException("A MudTextField has no end."); } /// /// Finds the quote which closes a C# string, skipping escaped characters. /// private static int StringEnd(string razor, int openingQuote) { for (var position = openingQuote + 1; position < razor.Length; position++) { if (razor[position] == '\\') position++; else if (razor[position] == '"') return position; } throw new InvalidOperationException("A string inside a MudTextField has no end."); } private static IEnumerable RazorFiles(string appDirectory) => Directory.EnumerateFiles(appDirectory, "*.razor", SearchOption.AllDirectories) .Where(path => Path.GetRelativePath(appDirectory, path).Split(Path.DirectorySeparatorChar)[0] is not ("bin" or "obj" or "wwwroot")); [GeneratedRegex(@" Path.GetFullPath(Path.Combine(Path.GetDirectoryName(sourceFilePath)!, "..", "..", "MindWork AI Studio")); }