Skip to content

Commit dddfc67

Browse files
committed
add: Assertion extension to use IEqualityComparer
1 parent 7ca3cd5 commit dddfc67

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Moq;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Cosmos.DataTransfer.Common.UnitTests;
6+
7+
/// <summary>
8+
/// Method for suppling Assert.AreEqual with a custom equality comparer
9+
/// (IEqualityComparer).
10+
/// </summary>
11+
public static class AssertExtension {
12+
public static void AreEqual<T>(this Assert that, T expected, T actual, IEqualityComparer<T> comparer) {
13+
if (!comparer.Equals(expected, actual))
14+
throw new AssertFailedException(
15+
$"Assert.AreEqual failed. Expected:<{expected!}>. Actual:<{actual!}>.");
16+
}
17+
}
18+
19+
[TestClass]
20+
public class AssertExtensionTests {
21+
[TestMethod]
22+
public void AreEqual_ThrowsArgumentNullException() {
23+
var bad_comparer = new Mock<IEqualityComparer<int>>();
24+
bad_comparer.Setup((x) => x.Equals(It.IsAny<int>(), It.IsAny<int>()))
25+
.Returns(false);
26+
var e = Assert.ThrowsException<AssertFailedException>(
27+
() => Assert.That.AreEqual(1, 1, bad_comparer.Object)
28+
);
29+
Assert.AreEqual("Assert.AreEqual failed. Expected:<1>. Actual:<1>.", e.Message);
30+
31+
var good_comparer = new Mock<IEqualityComparer<int>>();
32+
good_comparer.Setup((x) => x.Equals(It.IsAny<int>(), It.IsAny<int>()))
33+
.Returns(true);
34+
Assert.That.AreEqual(1, 1, good_comparer.Object); // No Exceptions thrown
35+
}
36+
}
37+

Interfaces/Cosmos.DataTransfer.Common.UnitTests/Cosmos.DataTransfer.Common.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
1717
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1818
<PackageReference Include="coverlet.collector" Version="3.1.2" />
19+
<PackageReference Include="Moq" Version="4.18.4" />
1920
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
2021
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2122
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)