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
60 changes: 59 additions & 1 deletion TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Immutable;
using TUnit.Assertions.Extensions;
using System.Diagnostics.CodeAnalysis;

namespace TUnit.Assertions.UnitTests;

Expand Down Expand Up @@ -27,6 +28,25 @@
await TUnitAssert.That(dictionary).DoesNotContainKey("blah");
}

[Test]
public async Task String_ReadOnlyDictionary_Contains_Key()
{
var dictionary = new ReadDictionary();

await TUnitAssert.That(dictionary).ContainsKey("Blah");
}

[Test]
public async Task String_ReadOnlyDictionary_Does_Not_Contain_Key()
{
IReadOnlyDictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>
{
["Blah"] = []
}.AsReadOnly();

await TUnitAssert.That(dictionary).DoesNotContainKey("blah");
}

[Test]
public async Task String_Dictionary_Contains_Key_IgnoreCase()
{
Expand All @@ -46,4 +66,42 @@
.IsEquivalentTo(ImmutableDictionary<string, int>.Empty.Add("Hello2", 1))
);
}

public class ReadDictionary : IReadOnlyDictionary<string, string>
{
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
yield break;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public int Count
{
get;
}
public bool ContainsKey(string key)
{
return true;
}

public bool TryGetValue(string key, [MaybeNullWhen(false)] out string value)

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (windows-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (windows-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (macos-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (macos-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (ubuntu-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).

Check warning on line 91 in TUnit.Assertions.Tests/Old/DictionaryAssertionTests.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (ubuntu-latest)

Nullability of reference types in type of parameter 'value' of 'bool ReadDictionary.TryGetValue(string key, out string value)' doesn't match implicitly implemented member 'bool IReadOnlyDictionary<string, string>.TryGetValue(string key, out string value)' (possibly because of nullability attributes).
{
value = "Value";
return true;
}

public string this[string key] => "Value";
public IEnumerable<string> Keys
{
get;
} = ["Blah"];
public IEnumerable<string> Values
{
get;
} = ["Value"];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public static InvokableValueAssertionBuilder<TDictionary> ContainsKey<TDictionar
, [doNotPopulateThisValue]);
}

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)
{
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TKey>(expected,
(actual, _, _) =>
{
Verify.ArgNotNull(actual);
return actual.Keys.Contains(expected, equalityComparer);
},
(_, _, _) => $"The key \"{expected}\" was not found in the dictionary",
$"contain the key '{expected}'")
, [doNotPopulateThisValue]);
}

public static InvokableValueAssertionBuilder<TDictionary> ContainsValue<TDictionary, TValue>(this IValueSource<TDictionary> valueSource, TValue expected, IEqualityComparer<TValue> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null)
where TDictionary : IDictionary
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,30 @@ public static InvokableValueAssertionBuilder<TDictionary> DoesNotContainValue<TD
$"not contain the value '{expected}'")
, [doNotPopulateThisValue]);
}

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)
{
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TKey>(expected,
(actual, _, _) =>
{
Verify.ArgNotNull(actual);
return !actual.Keys.Contains(expected, equalityComparer);
},
(_, _, _) => $"The key \"{expected}\" was found in the dictionary",
$"not contain the key '{expected}'")
, [doNotPopulateThisValue]);
}

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)
{
return valueSource.RegisterAssertion(new FuncValueAssertCondition<IReadOnlyDictionary<TKey, TValue>, TValue>(expected,
(actual, _, _) =>
{
Verify.ArgNotNull(actual);
return !actual.Values.Contains(expected, equalityComparer);
},
(_, _, _) => $"The value \"{expected}\" was found in the dictionary",
$"not contain the value '{expected}'")
, [doNotPopulateThisValue]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<ItemGroup>
<ProjectReference Include="..\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.csproj" />
<ProjectReference Include="..\TUnit.Core\TUnit.Core.csproj" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\TUnit.TestProject.Library\TUnit.TestProject.Library.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.SourceGenerators.Testing" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ namespace TUnit.Assertions.Extensions
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) { }
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)
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
Expand All @@ -1267,8 +1268,10 @@ namespace TUnit.Assertions.Extensions
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) { }
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)
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,7 @@ namespace TUnit.Assertions.Extensions
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) { }
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)
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
Expand All @@ -1302,8 +1303,10 @@ namespace TUnit.Assertions.Extensions
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) { }
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)
where TActual : System.Collections.Generic.IEnumerable<TInner> { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
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)
where TDictionary : System.Collections.IDictionary { }
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) { }
Expand Down
Loading
Loading