Skip to content

Commit 04e20ea

Browse files
committed
Added tests for Enumeration
1 parent 1941e55 commit 04e20ea

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace LinkDotNet.Blog.UnitTests.Domain
6+
{
7+
public class EnumerationTests
8+
{
9+
[Fact]
10+
public void GivenEnumeration_WhenCallingToString_ThenKeyIsReturned()
11+
{
12+
// Arrange
13+
var testEnum = TestEnumeration.One;
14+
15+
// Act
16+
var display = testEnum.ToString();
17+
18+
// Assert
19+
display.Should().Be(testEnum.Key);
20+
}
21+
22+
[Fact]
23+
public void GivenEnumerationWhenCallingAll_ThenAllPartsAreReturned()
24+
{
25+
// Act
26+
var all = TestEnumeration.All;
27+
28+
// Assert
29+
all.Should().Contain(TestEnumeration.One);
30+
all.Should().Contain(TestEnumeration.Two);
31+
}
32+
33+
[Fact]
34+
public void GivenEnumerationObject_WhenComparingEqualOnes_ThenEqual()
35+
{
36+
// Arrange
37+
var enum1 = TestEnumeration.One;
38+
var alsoEnum1 = TestEnumeration.Create("One");
39+
40+
// Act
41+
var isEqual = enum1 == alsoEnum1;
42+
43+
isEqual.Should().BeTrue();
44+
}
45+
46+
[Fact]
47+
public void GivenInvalidKey_WhenCreatingEnumeration_ThenErrorResult()
48+
{
49+
// Act
50+
Action result = () => TestEnumeration.Create("InvalidKey");
51+
52+
// Assert
53+
result.Should().Throw<InvalidOperationException>();
54+
}
55+
56+
[Theory]
57+
[InlineData(null)]
58+
[InlineData("")]
59+
[InlineData(" ")]
60+
public void GivenNullOrEmptyKey_WhenCreating_ThenException(string key)
61+
{
62+
Action result = () => new TestEnumeration(key);
63+
64+
// Assert
65+
result.Should().Throw<ArgumentException>();
66+
}
67+
68+
[Fact]
69+
public void NullEnumerationNeverEqual()
70+
{
71+
var isEqual = null == (TestEnumeration)null;
72+
73+
isEqual.Should().BeFalse();
74+
}
75+
76+
[Fact]
77+
public void ShouldDifferentEnumerations()
78+
{
79+
var isEqual = TestEnumeration.One == TestEnumeration.Two;
80+
81+
isEqual.Should().BeFalse();
82+
}
83+
}
84+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using LinkDotNet.Domain;
2+
3+
namespace LinkDotNet.Blog.UnitTests.Domain
4+
{
5+
public sealed class TestEnumeration : Enumeration<TestEnumeration>
6+
{
7+
public static readonly TestEnumeration One = new TestEnumeration(nameof(One));
8+
9+
public static readonly TestEnumeration Two = new TestEnumeration(nameof(Two));
10+
11+
public TestEnumeration(string key) : base(key)
12+
{
13+
}
14+
}
15+
}

LinkDotNet.Domain/Enumeration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static TEnumeration Create(string key)
4343
{
4444
var enumeration = All.SingleOrDefault(p => p.key == key);
4545

46-
if (enumeration == null)
46+
if (enumeration is null)
4747
{
4848
throw new InvalidOperationException($"{key} is not a valid value for {typeof(TEnumeration).Name}");
4949
}

0 commit comments

Comments
 (0)