Skip to content

Commit e1cd24c

Browse files
authored
IReadOnlyDictionary assertions (#2399)
* IReadOnlyDictionary assertions * Update snaps
1 parent f31a692 commit e1cd24c

7 files changed

+107
-2
lines changed

TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
using System.Collections;
12
using System.Collections.Immutable;
2-
using TUnit.Assertions.Extensions;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace TUnit.Assertions.UnitTests;
56

@@ -27,6 +28,25 @@ public async Task String_Dictionary_Does_Not_Contain_Key()
2728
await TUnitAssert.That(dictionary).DoesNotContainKey("blah");
2829
}
2930

31+
[Test]
32+
public async Task String_ReadOnlyDictionary_Contains_Key()
33+
{
34+
var dictionary = new ReadDictionary();
35+
36+
await TUnitAssert.That(dictionary).ContainsKey("Blah");
37+
}
38+
39+
[Test]
40+
public async Task String_ReadOnlyDictionary_Does_Not_Contain_Key()
41+
{
42+
IReadOnlyDictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>
43+
{
44+
["Blah"] = []
45+
}.AsReadOnly();
46+
47+
await TUnitAssert.That(dictionary).DoesNotContainKey("blah");
48+
}
49+
3050
[Test]
3151
public async Task String_Dictionary_Contains_Key_IgnoreCase()
3252
{
@@ -46,4 +66,42 @@ await TUnitAssert.That(ImmutableDictionary<string, int>.Empty.Add("Hello", 1))
4666
.IsEquivalentTo(ImmutableDictionary<string, int>.Empty.Add("Hello2", 1))
4767
);
4868
}
69+
70+
public class ReadDictionary : IReadOnlyDictionary<string, string>
71+
{
72+
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
73+
{
74+
yield break;
75+
}
76+
77+
IEnumerator IEnumerable.GetEnumerator()
78+
{
79+
return GetEnumerator();
80+
}
81+
82+
public int Count
83+
{
84+
get;
85+
}
86+
public bool ContainsKey(string key)
87+
{
88+
return true;
89+
}
90+
91+
public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value)
92+
{
93+
value = "Value";
94+
return true;
95+
}
96+
97+
public string this[string key] => "Value";
98+
public IEnumerable<string> Keys
99+
{
100+
get;
101+
} = ["Blah"];
102+
public IEnumerable<string> Values
103+
{
104+
get;
105+
} = ["Value"];
106+
}
49107
}

TUnit.Assertions/Assertions/Collections/DoesExtensions_Dictionary.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public static InvokableValueAssertionBuilder<TDictionary> ContainsKey<TDictionar
2424
, [doNotPopulateThisValue]);
2525
}
2626

27+
public static InvokableValueAssertionBuilder<IReadOnlyDictionary<TKey, TValue>> ContainsKey<TKey, TValue>(this IValueSource<IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, IEqualityComparer<TKey> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null)
28+
{
29+
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TKey>(expected,
30+
(actual, _, _) =>
31+
{
32+
Verify.ArgNotNull(actual);
33+
return actual.Keys.Contains(expected, equalityComparer);
34+
},
35+
(_, _, _) => $"The key \"{expected}\" was not found in the dictionary",
36+
$"contain the key '{expected}'")
37+
, [doNotPopulateThisValue]);
38+
}
39+
2740
public static InvokableValueAssertionBuilder<TDictionary> ContainsValue<TDictionary, TValue>(this IValueSource<TDictionary> valueSource, TValue expected, IEqualityComparer<TValue> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null)
2841
where TDictionary : IDictionary
2942
{

TUnit.Assertions/Assertions/Collections/DoesNotExtensions_Dictionary.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,30 @@ public static InvokableValueAssertionBuilder<TDictionary> DoesNotContainValue<TD
3737
$"not contain the value '{expected}'")
3838
, [doNotPopulateThisValue]);
3939
}
40+
41+
public static InvokableValueAssertionBuilder<IReadOnlyDictionary<TKey, TValue>> DoesNotContainKey<TKey, TValue>(this IValueSource<IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, IEqualityComparer<TKey> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null)
42+
{
43+
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TKey>(expected,
44+
(actual, _, _) =>
45+
{
46+
Verify.ArgNotNull(actual);
47+
return !actual.Keys.Contains(expected, equalityComparer);
48+
},
49+
(_, _, _) => $"The key \"{expected}\" was found in the dictionary",
50+
$"not contain the key '{expected}'")
51+
, [doNotPopulateThisValue]);
52+
}
53+
54+
public static InvokableValueAssertionBuilder<IReadOnlyDictionary<TKey, TValue>> DoesNotContainValue<TKey, TValue>(this IValueSource<IReadOnlyDictionary<TKey, TValue>> valueSource, TValue expected, IEqualityComparer<TValue> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null)
55+
{
56+
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TValue>(expected,
57+
(actual, _, _) =>
58+
{
59+
Verify.ArgNotNull(actual);
60+
return !actual.Values.Contains(expected, equalityComparer);
61+
},
62+
(_, _, _) => $"The value \"{expected}\" was found in the dictionary",
63+
$"not contain the value '{expected}'")
64+
, [doNotPopulateThisValue]);
65+
}
4066
}

TUnit.Core.SourceGenerator.Tests/TUnit.Core.SourceGenerator.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<ItemGroup>
66
<ProjectReference Include="..\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.csproj" />
77
<ProjectReference Include="..\TUnit.Core\TUnit.Core.csproj" ReferenceOutputAssembly="false" />
8-
<ProjectReference Include="..\TUnit.TestProject.Library\TUnit.TestProject.Library.csproj" ReferenceOutputAssembly="false" />
98
</ItemGroup>
109
<ItemGroup>
1110
<PackageReference Include="Microsoft.CodeAnalysis.SourceGenerators.Testing" />

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet2_0.verified.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,7 @@ namespace TUnit.Assertions.Extensions
12481248
public static TUnit.Assertions.AssertionBuilders.MappableResultAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>, TUnit.Assertions.AssertConditions.Collections.EnumerableContainsExpectedFuncAssertCondition<System.Collections.Generic.IEnumerable<TInner>, TInner>, TInner> Contains<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string doNotPopulateThisValue = null) { }
12491249
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TActual> Contains<TActual, TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TActual> valueSource, TInner expected, System.Collections.Generic.IEqualityComparer<TInner> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12501250
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
1251+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> ContainsKey<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
12511252
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> ContainsKey<TDictionary, TKey>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12521253
where TDictionary : System.Collections.IDictionary { }
12531254
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>> ContainsOnly<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string doNotPopulateThisValue = null) { }
@@ -1267,8 +1268,10 @@ namespace TUnit.Assertions.Extensions
12671268
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>> DoesNotContain<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string? doNotPopulateThisValue = null) { }
12681269
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TActual> DoesNotContain<TActual, TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TActual> valueSource, TInner expected, System.Collections.Generic.IEqualityComparer<TInner?>? equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string? doNotPopulateThisValue = null)
12691270
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
1271+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> DoesNotContainKey<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
12701272
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> DoesNotContainKey<TDictionary, TKey>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12711273
where TDictionary : System.Collections.IDictionary { }
1274+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> DoesNotContainValue<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TValue expected, System.Collections.Generic.IEqualityComparer<TValue> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
12721275
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> DoesNotContainValue<TDictionary, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TValue expected, System.Collections.Generic.IEqualityComparer<TValue> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12731276
where TDictionary : System.Collections.IDictionary { }
12741277
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<string> DoesNotEndWith(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<string> valueSource, string expected, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }

TUnit.PublicAPI/Tests.Assertions_Library_Has_No_API_Changes.DotNet8_0.verified.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,7 @@ namespace TUnit.Assertions.Extensions
12831283
public static TUnit.Assertions.AssertionBuilders.MappableResultAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>, TUnit.Assertions.AssertConditions.Collections.EnumerableContainsExpectedFuncAssertCondition<System.Collections.Generic.IEnumerable<TInner>, TInner>, TInner> Contains<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string doNotPopulateThisValue = null) { }
12841284
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TActual> Contains<TActual, TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TActual> valueSource, TInner expected, System.Collections.Generic.IEqualityComparer<TInner> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12851285
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
1286+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> ContainsKey<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
12861287
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> ContainsKey<TDictionary, TKey>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
12871288
where TDictionary : System.Collections.IDictionary { }
12881289
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>> ContainsOnly<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string doNotPopulateThisValue = null) { }
@@ -1302,8 +1303,10 @@ namespace TUnit.Assertions.Extensions
13021303
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IEnumerable<TInner>> DoesNotContain<TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IEnumerable<TInner>> valueSource, System.Func<TInner, bool> matcher, [System.Runtime.CompilerServices.CallerArgumentExpression("matcher")] string? doNotPopulateThisValue = null) { }
13031304
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TActual> DoesNotContain<TActual, TInner>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TActual> valueSource, TInner expected, System.Collections.Generic.IEqualityComparer<TInner?>? equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string? doNotPopulateThisValue = null)
13041305
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
1306+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> DoesNotContainKey<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
13051307
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> DoesNotContainKey<TDictionary, TKey>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TKey expected, System.Collections.Generic.IEqualityComparer<TKey> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
13061308
where TDictionary : System.Collections.IDictionary { }
1309+
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> DoesNotContainValue<TKey, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<System.Collections.Generic.IReadOnlyDictionary<TKey, TValue>> valueSource, TValue expected, System.Collections.Generic.IEqualityComparer<TValue> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }
13071310
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<TDictionary> DoesNotContainValue<TDictionary, TValue>(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<TDictionary> valueSource, TValue expected, System.Collections.Generic.IEqualityComparer<TValue> equalityComparer = null, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null)
13081311
where TDictionary : System.Collections.IDictionary { }
13091312
public static TUnit.Assertions.AssertionBuilders.InvokableValueAssertionBuilder<string> DoesNotEndWith(this TUnit.Assertions.AssertConditions.Interfaces.IValueSource<string> valueSource, string expected, [System.Runtime.CompilerServices.CallerArgumentExpression("expected")] string doNotPopulateThisValue = null) { }

0 commit comments

Comments
 (0)