|
| 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 | + |
0 commit comments