-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collection overloads for asserting on ImmutableArrays (#2043)
* Collection overloads for asserting on ImmutableArrays * chore: add System.Collections.Immutable package reference and clean up project files * Fixes * Compilation fixes
- Loading branch information
Showing
14 changed files
with
276 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Collections.Immutable; | ||
using TUnit.Assertions.Enums; | ||
|
||
namespace TUnit.Assertions.Tests.Bugs; | ||
|
||
public class Tests1917 | ||
{ | ||
[Test] | ||
public async Task Immutable_Array_Has_Enumerable_Methods() | ||
{ | ||
var array = ImmutableArray<string>.Empty; | ||
var list = ImmutableList<string>.Empty; | ||
|
||
await Assert.That(array).IsEmpty(); | ||
await Assert.That(list).IsEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
.../Assertions/Collections/Conditions/EnumerableDistinctItemsExpectedValueAssertCondition.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
TUnit.Assertions/Assertions/Collections/HasExtensions_ImmutableArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#nullable disable | ||
|
||
using System.Collections.Immutable; | ||
using TUnit.Assertions.AssertConditions.Collections; | ||
using TUnit.Assertions.AssertConditions.Interfaces; | ||
using TUnit.Assertions.AssertionBuilders; | ||
using TUnit.Assertions.AssertionBuilders.Wrappers; | ||
|
||
namespace TUnit.Assertions.Extensions; | ||
|
||
public static partial class HasExtensions | ||
{ | ||
public static SingleItemAssertionBuilderWrapper<ImmutableArray<TInner>, TInner> HasSingleItem<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
var invokableValueAssertionBuilder = valueSource.RegisterAssertion(new EnumerableCountEqualToExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(1), []); | ||
|
||
return new SingleItemAssertionBuilderWrapper<ImmutableArray<TInner>, TInner>(invokableValueAssertionBuilder); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> HasDistinctItems<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
return HasDistinctItems(valueSource, EqualityComparer<TInner>.Default); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> HasDistinctItems<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, IEqualityComparer<TInner> equalityComparer) | ||
{ | ||
return valueSource.RegisterAssertion(new EnumerableDistinctItemsExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(equalityComparer), []); | ||
} | ||
|
||
public static EnumerableCount<ImmutableArray<TInner>, TInner> HasCount<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
valueSource.AppendExpression("HasCount()"); | ||
return new EnumerableCount<ImmutableArray<TInner>, TInner>(valueSource); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
TUnit.Assertions/Assertions/Collections/ImmutableArrayIsExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#nullable disable | ||
|
||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using TUnit.Assertions.AssertConditions.Collections; | ||
using TUnit.Assertions.AssertConditions.Interfaces; | ||
using TUnit.Assertions.AssertionBuilders; | ||
using TUnit.Assertions.Enums; | ||
using TUnit.Assertions.Equality; | ||
|
||
namespace TUnit.Assertions.Extensions; | ||
|
||
[SuppressMessage("Usage", "TUnitAssertions0003:Compiler argument populated")] | ||
public static class ImmutableArrayIsExtensions | ||
{ | ||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsEquivalentTo< | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] | ||
TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, ImmutableArray<TInner> expected, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null) | ||
{ | ||
return IsEquivalentTo(valueSource, expected, new CollectionEquivalentToEqualityComparer<TInner>(), doNotPopulateThisValue); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsEquivalentTo< | ||
TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, | ||
ImmutableArray<TInner> expected, IEqualityComparer<TInner> comparer, | ||
[CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null) | ||
{ | ||
return IsEquivalentTo(valueSource, expected, comparer, CollectionOrdering.Matching, doNotPopulateThisValue); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsEquivalentTo< | ||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields | DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] | ||
TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, ImmutableArray<TInner> expected, CollectionOrdering collectionOrdering, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null) | ||
{ | ||
return IsEquivalentTo(valueSource, expected, new CollectionEquivalentToEqualityComparer<TInner>(), collectionOrdering, doNotPopulateThisValue); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsEquivalentTo<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, ImmutableArray<TInner> expected, IEqualityComparer<TInner> comparer, CollectionOrdering collectionOrdering, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null) | ||
{ | ||
return valueSource.RegisterAssertion( | ||
new EnumerableEquivalentToExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(expected, | ||
comparer, collectionOrdering), [doNotPopulateThisValue]); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsInOrder<TInner>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
return IsOrderedBy(valueSource, x => x, Comparer<TInner>.Default, null); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsInDescendingOrder<TInner>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
return IsOrderedByDescending(valueSource, x => x, Comparer<TInner>.Default, null); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsInOrder<TInner>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
IComparer<TInner> comparer) | ||
{ | ||
return IsOrderedBy(valueSource, x => x, comparer, null); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsInDescendingOrder<TInner>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
IComparer<TInner> comparer) | ||
{ | ||
return IsOrderedByDescending(valueSource, x => x, comparer, null); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsOrderedBy<TInner, TComparisonItem>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
Func<TInner, TComparisonItem> comparisonItemSelector, | ||
[CallerArgumentExpression(nameof(comparisonItemSelector))] string doNotPopulateThisValue = null) | ||
{ | ||
return IsOrderedBy(valueSource, comparisonItemSelector, Comparer<TComparisonItem>.Default, doNotPopulateThisValue); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsOrderedByDescending<TInner, TComparisonItem>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
Func<TInner, TComparisonItem> comparisonItemSelector, | ||
[CallerArgumentExpression(nameof(comparisonItemSelector))] string doNotPopulateThisValue = null) | ||
{ | ||
return IsOrderedByDescending(valueSource, comparisonItemSelector, Comparer<TComparisonItem>.Default, doNotPopulateThisValue); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsOrderedBy<TInner, TComparisonItem>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
Func<TInner, TComparisonItem> comparisonItemSelector, | ||
IComparer<TComparisonItem> comparer, | ||
[CallerArgumentExpression(nameof(comparisonItemSelector))] string doNotPopulateThisValue = null, | ||
[CallerArgumentExpression(nameof(comparer))] string doNotPopulateThisValue2 = null) | ||
{ | ||
return valueSource.RegisterAssertion( | ||
new EnumerableOrderedByAssertCondition<ImmutableArray<TInner>, TInner, TComparisonItem>(comparer, comparisonItemSelector, Order.Ascending), [doNotPopulateThisValue, doNotPopulateThisValue2]); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsOrderedByDescending<TInner, TComparisonItem>( | ||
this IValueSource<ImmutableArray<TInner>> valueSource, | ||
Func<TInner, TComparisonItem> comparisonItemSelector, | ||
IComparer<TComparisonItem> comparer, | ||
[CallerArgumentExpression(nameof(comparisonItemSelector))] string doNotPopulateThisValue = null, | ||
[CallerArgumentExpression(nameof(comparer))] string doNotPopulateThisValue2 = null) | ||
{ | ||
return valueSource.RegisterAssertion( | ||
new EnumerableOrderedByAssertCondition<ImmutableArray<TInner>, TInner, TComparisonItem>(comparer, comparisonItemSelector, Order.Descending), [doNotPopulateThisValue, doNotPopulateThisValue2]); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsEmpty<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
return valueSource.RegisterAssertion(new EnumerableCountEqualToExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(0), []); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
TUnit.Assertions/Assertions/Collections/ImmutableArrayIsNotExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#nullable disable | ||
|
||
using System.Collections.Immutable; | ||
using System.Runtime.CompilerServices; | ||
using TUnit.Assertions.AssertConditions.Collections; | ||
using TUnit.Assertions.AssertConditions.Interfaces; | ||
using TUnit.Assertions.AssertionBuilders; | ||
|
||
namespace TUnit.Assertions.Extensions; | ||
|
||
public static class ImmutableArrayIsNotExtensions | ||
{ | ||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsNotEquivalentTo<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource, IEnumerable<TInner> expected, IEqualityComparer<TInner> equalityComparer = null, [CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue = null) | ||
{ | ||
return valueSource.RegisterAssertion(new EnumerableNotEquivalentToExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(expected, equalityComparer) | ||
, [doNotPopulateThisValue]); | ||
} | ||
|
||
public static InvokableValueAssertionBuilder<ImmutableArray<TInner>> IsNotEmpty<TInner>(this IValueSource<ImmutableArray<TInner>> valueSource) | ||
{ | ||
return valueSource.RegisterAssertion(new EnumerableCountNotEqualToExpectedValueAssertCondition<ImmutableArray<TInner>, TInner>(0) | ||
, []); | ||
} | ||
} |
Oops, something went wrong.