aboutsummaryrefslogtreecommitdiff
path: root/Timeline.Tests/UsernameValidatorUnitTest.cs
diff options
context:
space:
mode:
author杨宇千 <crupest@outlook.com>2019-08-12 16:14:57 +0800
committer杨宇千 <crupest@outlook.com>2019-08-12 16:14:57 +0800
commit4367f283f196d8b6f195855b4592bf65aa10859e (patch)
treefb988189269b240d7bc82e6e6fa3d41e65313652 /Timeline.Tests/UsernameValidatorUnitTest.cs
parent285fe070388e48d82f008c3de5b0d7675f55ebfa (diff)
downloadtimeline-4367f283f196d8b6f195855b4592bf65aa10859e.tar.gz
timeline-4367f283f196d8b6f195855b4592bf65aa10859e.tar.bz2
timeline-4367f283f196d8b6f195855b4592bf65aa10859e.zip
Add username validator.
Diffstat (limited to 'Timeline.Tests/UsernameValidatorUnitTest.cs')
-rw-r--r--Timeline.Tests/UsernameValidatorUnitTest.cs86
1 files changed, 86 insertions, 0 deletions
diff --git a/Timeline.Tests/UsernameValidatorUnitTest.cs b/Timeline.Tests/UsernameValidatorUnitTest.cs
new file mode 100644
index 00000000..97d56195
--- /dev/null
+++ b/Timeline.Tests/UsernameValidatorUnitTest.cs
@@ -0,0 +1,86 @@
+using FluentAssertions;
+using System;
+using Timeline.Services;
+using Xunit;
+
+namespace Timeline.Tests
+{
+ public class UsernameValidatorUnitTest : IClassFixture<UsernameValidator>
+ {
+ private readonly UsernameValidator _validator;
+
+ public UsernameValidatorUnitTest(UsernameValidator validator)
+ {
+ _validator = validator;
+ }
+
+ [Fact]
+ public void NullShouldThrow()
+ {
+ _validator.Invoking(v => v.Validate(null, out string message)).Should().Throw<ArgumentNullException>();
+ }
+
+
+ private string FailAndMessage(string username)
+ {
+ var result = _validator.Validate(username, out var message);
+ result.Should().BeFalse();
+ return message;
+ }
+
+ private void Succeed(string username)
+ {
+ _validator.Validate(username, out var message).Should().BeTrue();
+ message.Should().BeNull();
+ }
+
+ [Fact]
+ public void Empty()
+ {
+ FailAndMessage("").Should().ContainEquivalentOf("empty");
+ }
+
+ [Fact]
+ public void WhiteSpace()
+ {
+ FailAndMessage(" ").Should().ContainEquivalentOf("whitespace");
+ FailAndMessage("\t").Should().ContainEquivalentOf("whitespace");
+ FailAndMessage("\n").Should().ContainEquivalentOf("whitespace");
+
+ FailAndMessage("a b").Should().ContainEquivalentOf("whitespace");
+ FailAndMessage("a\tb").Should().ContainEquivalentOf("whitespace");
+ FailAndMessage("a\nb").Should().ContainEquivalentOf("whitespace");
+ }
+
+ [Fact]
+ public void BadCharactor()
+ {
+ FailAndMessage("!").Should().ContainEquivalentOf("regex");
+ FailAndMessage("!abc").Should().ContainEquivalentOf("regex");
+ FailAndMessage("ab!c").Should().ContainEquivalentOf("regex");
+ }
+
+ [Fact]
+ public void BadBegin()
+ {
+ FailAndMessage("-").Should().ContainEquivalentOf("regex");
+ FailAndMessage("-abc").Should().ContainEquivalentOf("regex");
+ }
+
+ [Fact]
+ public void TooLong()
+ {
+ FailAndMessage(new string('a', 40)).Should().ContainEquivalentOf("long");
+ }
+
+ [Fact]
+ public void Success()
+ {
+ Succeed("abc");
+ Succeed("_abc");
+ Succeed("a-bc");
+ Succeed("a-b-c");
+ Succeed("a-b_c");
+ }
+ }
+}