From a7e2b485401efe1af2b9638cd24ea4353b1eef7b Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sat, 13 Aug 2022 13:57:35 +0200 Subject: [PATCH 1/4] Consistent use of ICollectionWrapper --- .../DataColumnCollectionAssertionExtensions.cs | 2 +- Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs | 4 ++-- .../DataTableCollectionAssertionExtensions.cs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs index 67c2af72db..68f6e57166 100644 --- a/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataColumnCollectionAssertionExtensions.cs @@ -73,7 +73,7 @@ public static AndConstraint> NotBeSameAs Guard.ThrowIfArgumentIsNull( unexpected, nameof(unexpected), "Cannot verify same reference against a collection (use NotBeNull instead?)."); - if (assertion.Subject is ReadOnlyNonGenericCollectionWrapper wrapper) + if (assertion.Subject is ICollectionWrapper wrapper) { var actualSubject = wrapper.UnderlyingCollection; diff --git a/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs index 094562a800..ef82cee119 100644 --- a/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataRowCollectionAssertionExtensions.cs @@ -28,7 +28,7 @@ public static AndConstraint> BeSameAs( this GenericCollectionAssertions assertion, DataRowCollection expected, string because = "", params object[] becauseArgs) { - if (assertion.Subject is ReadOnlyNonGenericCollectionWrapper wrapper) + if (assertion.Subject is ICollectionWrapper wrapper) { var actualSubject = wrapper.UnderlyingCollection; @@ -68,7 +68,7 @@ public static AndConstraint> NotBeSameAs( this GenericCollectionAssertions assertion, DataRowCollection unexpected, string because = "", params object[] becauseArgs) { - if (assertion.Subject is ReadOnlyNonGenericCollectionWrapper wrapper) + if (assertion.Subject is ICollectionWrapper wrapper) { var actualSubject = wrapper.UnderlyingCollection; diff --git a/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs b/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs index 4a01fb71dc..e5364a53d5 100644 --- a/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs +++ b/Src/FluentAssertions/DataTableCollectionAssertionExtensions.cs @@ -25,7 +25,7 @@ public static AndConstraint> BeSameAs( this GenericCollectionAssertions assertion, DataTableCollection expected, string because = "", params object[] becauseArgs) { - if (assertion.Subject is ReadOnlyNonGenericCollectionWrapper wrapper) + if (assertion.Subject is ICollectionWrapper wrapper) { var actualSubject = wrapper.UnderlyingCollection; @@ -65,7 +65,7 @@ public static AndConstraint> NotBeSameAs( this GenericCollectionAssertions assertion, DataTableCollection unexpected, string because = "", params object[] becauseArgs) { - if (assertion.Subject is ReadOnlyNonGenericCollectionWrapper wrapper) + if (assertion.Subject is ICollectionWrapper wrapper) { var actualSubject = wrapper.UnderlyingCollection; From 17556b5e4647c32c460ba9c31ccef5222c607eb4 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sat, 13 Aug 2022 14:02:33 +0200 Subject: [PATCH 2/4] Change unsupported mutating methods to explicit interface implementations --- .../ReadOnlyNonGenericCollectionWrapper.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs b/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs index f82dd73023..b829f8b68f 100644 --- a/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs +++ b/Src/FluentAssertions/Common/ReadOnlyNonGenericCollectionWrapper.cs @@ -47,7 +47,7 @@ public ReadOnlyNonGenericCollectionWrapper(TCollection collection) public int Count => UnderlyingCollection.Count; - public bool IsReadOnly => true; + bool ICollection.IsReadOnly => true; public IEnumerator GetEnumerator() => UnderlyingCollection.Cast().GetEnumerator(); @@ -57,18 +57,9 @@ public ReadOnlyNonGenericCollectionWrapper(TCollection collection) public void CopyTo(TItem[] array, int arrayIndex) => UnderlyingCollection.CopyTo(array, arrayIndex); - /* + void ICollection.Add(TItem item) => throw new NotSupportedException(); - Mutation is not supported, but these methods must be implemented to satisfy ICollection: - * Add - * Clear - * Remove + void ICollection.Clear() => throw new NotSupportedException(); - */ - - public void Add(TItem item) => throw new NotSupportedException(); - - public void Clear() => throw new NotSupportedException(); - - public bool Remove(TItem item) => throw new NotSupportedException(); + bool ICollection.Remove(TItem item) => throw new NotSupportedException(); } From e8026ad843d0b6b349856046cb829e243afb34ca Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sat, 13 Aug 2022 14:03:47 +0200 Subject: [PATCH 3/4] Use null-coalescing assignment --- Src/FluentAssertions/Equivalency/Field.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Src/FluentAssertions/Equivalency/Field.cs b/Src/FluentAssertions/Equivalency/Field.cs index d82f1e46bc..e44a523d20 100644 --- a/Src/FluentAssertions/Equivalency/Field.cs +++ b/Src/FluentAssertions/Equivalency/Field.cs @@ -46,16 +46,6 @@ public object GetValue(object obj) public CSharpAccessModifier SetterAccessibility => fieldInfo.GetCSharpAccessModifier(); - public bool IsBrowsable - { - get - { - if (isBrowsable == null) - { - isBrowsable = fieldInfo.GetCustomAttribute() is not { State: EditorBrowsableState.Never }; - } - - return isBrowsable.Value; - } - } + public bool IsBrowsable => + isBrowsable ??= fieldInfo.GetCustomAttribute() is not { State: EditorBrowsableState.Never }; } From b3ac174bccab9c6780f17da3bec6ac8123274b61 Mon Sep 17 00:00:00 2001 From: Jonas Nyrup Date: Sat, 13 Aug 2022 14:06:05 +0200 Subject: [PATCH 4/4] Use target-typed new expression --- Src/FluentAssertions/Events/EventMonitor.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Src/FluentAssertions/Events/EventMonitor.cs b/Src/FluentAssertions/Events/EventMonitor.cs index 60cdceb893..eb63581fcf 100644 --- a/Src/FluentAssertions/Events/EventMonitor.cs +++ b/Src/FluentAssertions/Events/EventMonitor.cs @@ -16,8 +16,7 @@ internal class EventMonitor : IMonitor { private readonly WeakReference subject; - private readonly ConcurrentDictionary recorderMap = - new ConcurrentDictionary(); + private readonly ConcurrentDictionary recorderMap = new(); public EventMonitor(object eventSource, Func utcNow) {