From 1a998040268282086549e67ae81f8e059ee885a9 Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Wed, 21 Aug 2019 23:55:42 +0800 Subject: Add validators. --- Timeline.Tests/UserDetailValidatorTest.cs | 97 +++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Timeline.Tests/UserDetailValidatorTest.cs (limited to 'Timeline.Tests/UserDetailValidatorTest.cs') diff --git a/Timeline.Tests/UserDetailValidatorTest.cs b/Timeline.Tests/UserDetailValidatorTest.cs new file mode 100644 index 00000000..9b112946 --- /dev/null +++ b/Timeline.Tests/UserDetailValidatorTest.cs @@ -0,0 +1,97 @@ +using FluentAssertions; +using System.Collections.Generic; +using Timeline.Models.Validation; +using Xunit; + +namespace Timeline.Tests +{ + public static class UserDetailValidatorsTest + { + private static void SucceedWith(object value) where TValidator : class, IValidator, new() + { + var result = new TValidator().Validate(value, out var message); + result.Should().BeTrue(); + message.Should().Equals(ValidationConstants.SuccessMessage); + } + + private static void FailWith(object value, params string[] messageContains) where TValidator : class, IValidator, new() + { + var result = new TValidator().Validate(value, out var message); + result.Should().BeFalse(); + + foreach (var m in messageContains) + { + message.Should().ContainEquivalentOf(m); + } + } + + public class QQ + { + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("12345678")] + public void Success(object qq) + { + SucceedWith(qq); + } + + [Theory] + [InlineData(123, "type")] + [InlineData("123", "short")] + [InlineData("111111111111111111111111111111111111", "long")] + [InlineData("aaaaaaaa", "digit")] + public void Fail(object qq, string messageContains) + { + FailWith(qq, messageContains); + } + } + + public class EMail + { + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("aaa@aaa.net")] + public void Success(object email) + { + SucceedWith(email); + } + + public static IEnumerable FailTestData() + { + yield return new object[] { 123, "type" }; + yield return new object[] { new string('a', 100), "long" }; + yield return new object[] { "aaaaaaaa", "format" }; + } + + [Theory] + [MemberData(nameof(FailTestData))] + public void Fail(object email, string messageContains) + { + FailWith(email, messageContains); + } + } + + public class PhoneNumber + { + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("12345678910")] + public void Success(object phoneNumber) + { + SucceedWith(phoneNumber); + } + + [Theory] + [InlineData(123, "type")] + [InlineData("111111111111111111111111111111111111", "long")] + [InlineData("aaaaaaaa", "digit")] + public void Fail(object phoneNumber, string messageContains) + { + FailWith(phoneNumber, messageContains); + } + } + } +} -- cgit v1.2.3