Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions TUnit.Assertions.UnitTests/DictionaryAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using TUnit.Assertions.Extensions;

namespace TUnit.Assertions.UnitTests;
Expand Down Expand Up @@ -36,4 +37,13 @@ public async Task String_Dictionary_Contains_Key_IgnoreCase()

await TUnitAssert.That(dictionary).ContainsKey("blah", StringComparer.InvariantCultureIgnoreCase);
}

[Test]
public async Task Immutable_Dictionary_Does_Not_Contain_Key()
{
await TUnitAssert.ThrowsAsync<TUnitAssertionException>(async () =>
await TUnitAssert.That(ImmutableDictionary<string, int>.Empty.Add("Hello", 1))
.IsEquivalentTo(ImmutableDictionary<string, int>.Empty.Add("Hello2", 1))
);
}
}
26 changes: 26 additions & 0 deletions TUnit.Assertions/Compare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,32 @@ private static IEnumerable<ComparisonFailure> CheckEquivalent<

foreach (var key in keys)
{
if (!actualDictionary.Contains(key))
{
yield return new ComparisonFailure
{
Type = MemberType.DictionaryItem,
Actual = $"No entry with key: {key}",
Expected = $"[{key}] = {expectedDictionary[key]}",
NestedMemberNames = [..memberNames, $"[{key}]"]
};

yield break;
}

if (!expectedDictionary.Contains(key))
{
yield return new ComparisonFailure
{
Type = MemberType.DictionaryItem,
Actual = $"[{key}] = {actualDictionary[key]}",
Expected = $"No entry with key: {key}",
NestedMemberNames = [..memberNames, $"[{key}]"]
};

yield break;
}

var actualObject = actualDictionary[key];
var expectedObject = expectedDictionary[key];

Expand Down
Loading