using System.ComponentModel.DataAnnotations; using Bit.Core.Utilities; using Xunit; namespace Bit.Core.Test.Utilities; public class ValidateSequenceAttributeTests { [Fact] public void IsValid_WithEmptyCollection_ReturnsSuccess() { var result = Validate([]); Assert.Equal(ValidationResult.Success, result); } [Fact] public void IsValid_WithNullCollection_ReturnsSuccess() { var result = Validate(null); Assert.Equal(ValidationResult.Success, result); } [Fact] public void IsValid_WithSomeInvalidItems_ReturnsErrorMessage() { var result = Validate(["bad", "also-bad", "ok"]); Assert.NotNull(result); Assert.Contains("'bad'", result!.ErrorMessage); Assert.Contains("'also-bad'", result.ErrorMessage); Assert.DoesNotContain("ok", result.ErrorMessage); } /// /// Invokes directly so edge cases /// (null/empty collection) can be tested in isolation, independent of any model's /// [Required] attribute which would intercept those cases first. /// private static ValidationResult? Validate(IEnumerable? value) { var attr = new ValidateSequenceAttribute(); return attr.GetValidationResult(value, new ValidationContext(new object())); } /// /// Accepts only the literal string "ok" so tests are not coupled to /// regex rules. /// private class OnlyAcceptsOkValidator : ValidationAttribute { public override bool IsValid(object? value) => value?.ToString() == "ok"; } }