From 8d1b92426bcc8a6350131010439c47d1771ab999 Mon Sep 17 00:00:00 2001 From: Dennis Doomen Date: Mon, 26 Aug 2024 20:41:36 +0200 Subject: [PATCH] Various internal naming improvements. --- Src/FluentAssertions/AndConstraint.cs | 8 +++--- .../GenericCollectionAssertions.cs | 2 +- .../GenericDictionaryAssertions.cs | 2 +- .../Collections/StringCollectionAssertions.cs | 2 +- .../Equivalency/EquivalencyStep.cs | 2 +- .../Equivalency/Steps/AssertionResultSet.cs | 2 +- .../Steps/EnumerableEquivalencyValidator.cs | 2 +- .../Execution/AssertionScope.cs | 12 ++++---- ...tDataItems.cs => ContextDataDictionary.cs} | 28 +++++++------------ .../Execution/MessageBuilder.cs | 4 +-- .../Formatting/FormattedObjectGraph.cs | 14 +++------- .../Specialized/ExceptionAssertions.cs | 2 +- 12 files changed, 33 insertions(+), 47 deletions(-) rename Src/FluentAssertions/Execution/{ContextDataItems.cs => ContextDataDictionary.cs} (67%) diff --git a/Src/FluentAssertions/AndConstraint.cs b/Src/FluentAssertions/AndConstraint.cs index 00d94c8410..796336bd30 100644 --- a/Src/FluentAssertions/AndConstraint.cs +++ b/Src/FluentAssertions/AndConstraint.cs @@ -3,15 +3,15 @@ namespace FluentAssertions; [DebuggerNonUserCode] -public class AndConstraint +public class AndConstraint { - public T And { get; } + public TParent And { get; } /// /// Initializes a new instance of the class. /// - public AndConstraint(T parentConstraint) + public AndConstraint(TParent parent) { - And = parentConstraint; + And = parent; } } diff --git a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs index fe91b27c0f..3c33ae9a28 100644 --- a/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericCollectionAssertions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; diff --git a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs index c31e6f4af8..765a90f2ad 100644 --- a/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs +++ b/Src/FluentAssertions/Collections/GenericDictionaryAssertions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; diff --git a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs index 2779fdf042..82e4498f3c 100644 --- a/Src/FluentAssertions/Collections/StringCollectionAssertions.cs +++ b/Src/FluentAssertions/Collections/StringCollectionAssertions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; diff --git a/Src/FluentAssertions/Equivalency/EquivalencyStep.cs b/Src/FluentAssertions/Equivalency/EquivalencyStep.cs index 52d5666eea..d50c77023a 100644 --- a/Src/FluentAssertions/Equivalency/EquivalencyStep.cs +++ b/Src/FluentAssertions/Equivalency/EquivalencyStep.cs @@ -1,4 +1,4 @@ -namespace FluentAssertions.Equivalency; +namespace FluentAssertions.Equivalency; /// /// Convenient implementation of that will only invoke diff --git a/Src/FluentAssertions/Equivalency/Steps/AssertionResultSet.cs b/Src/FluentAssertions/Equivalency/Steps/AssertionResultSet.cs index c6ea007df9..b1b9b7cfe2 100644 --- a/Src/FluentAssertions/Equivalency/Steps/AssertionResultSet.cs +++ b/Src/FluentAssertions/Equivalency/Steps/AssertionResultSet.cs @@ -29,7 +29,7 @@ public void AddSet(object key, string[] failures) /// The closest match is the set that contains the least amount of failures, or no failures at all, and preferably /// the set that is identified by the . /// - public string[] SelectClosestMatchFor(object key = null) + public string[] GetTheFailuresForTheSetWithTheFewestFailures(object key = null) { if (ContainsSuccessfulSet()) { diff --git a/Src/FluentAssertions/Equivalency/Steps/EnumerableEquivalencyValidator.cs b/Src/FluentAssertions/Equivalency/Steps/EnumerableEquivalencyValidator.cs index 71de9e6657..3a58151a4c 100644 --- a/Src/FluentAssertions/Equivalency/Steps/EnumerableEquivalencyValidator.cs +++ b/Src/FluentAssertions/Equivalency/Steps/EnumerableEquivalencyValidator.cs @@ -183,7 +183,7 @@ private bool LooselyMatchAgainst(IList subjects, T expectation, int e unmatchedSubjectIndexes.RemoveAt(indexToBeRemoved); } - foreach (string failure in results.SelectClosestMatchFor(expectationIndex)) + foreach (string failure in results.GetTheFailuresForTheSetWithTheFewestFailures(expectationIndex)) { AssertionScope.Current.AddPreFormattedFailure(failure); } diff --git a/Src/FluentAssertions/Execution/AssertionScope.cs b/Src/FluentAssertions/Execution/AssertionScope.cs index d18ed54d38..2b70d559c7 100644 --- a/Src/FluentAssertions/Execution/AssertionScope.cs +++ b/Src/FluentAssertions/Execution/AssertionScope.cs @@ -22,7 +22,7 @@ public sealed class AssertionScope : IAssertionScope #region Private Definitions private readonly IAssertionStrategy assertionStrategy; - private readonly ContextDataItems contextData = new(); + private readonly ContextDataDictionary contextData = new(); private readonly StringBuilder tracing = new(); private Func reason; @@ -219,8 +219,8 @@ public AssertionScope WithExpectation(string message, params object[] args) internal void TrackComparands(object subject, object expectation) { - contextData.Add(new ContextDataItems.DataItem("subject", subject, reportable: false, requiresFormatting: true)); - contextData.Add(new ContextDataItems.DataItem("expectation", expectation, reportable: false, requiresFormatting: true)); + contextData.Add(new ContextDataDictionary.DataItem("subject", subject, reportable: false, requiresFormatting: true)); + contextData.Add(new ContextDataDictionary.DataItem("expectation", expectation, reportable: false, requiresFormatting: true)); } /// @@ -359,7 +359,7 @@ public void AppendTracing(string tracingBlock) /// public void AddNonReportable(string key, object value) { - contextData.Add(new ContextDataItems.DataItem(key, value, reportable: false, requiresFormatting: false)); + contextData.Add(new ContextDataDictionary.DataItem(key, value, reportable: false, requiresFormatting: false)); } /// @@ -368,7 +368,7 @@ public void AddNonReportable(string key, object value) /// public void AddReportable(string key, string value) { - contextData.Add(new ContextDataItems.DataItem(key, value, reportable: true, requiresFormatting: false)); + contextData.Add(new ContextDataDictionary.DataItem(key, value, reportable: true, requiresFormatting: false)); } /// @@ -377,7 +377,7 @@ public void AddReportable(string key, string value) /// public void AddReportable(string key, Func valueFunc) { - contextData.Add(new ContextDataItems.DataItem(key, new DeferredReportable(valueFunc), reportable: true, + contextData.Add(new ContextDataDictionary.DataItem(key, new DeferredReportable(valueFunc), reportable: true, requiresFormatting: false)); } diff --git a/Src/FluentAssertions/Execution/ContextDataItems.cs b/Src/FluentAssertions/Execution/ContextDataDictionary.cs similarity index 67% rename from Src/FluentAssertions/Execution/ContextDataItems.cs rename to Src/FluentAssertions/Execution/ContextDataDictionary.cs index 6ddf42eba4..8a079f6240 100644 --- a/Src/FluentAssertions/Execution/ContextDataItems.cs +++ b/Src/FluentAssertions/Execution/ContextDataDictionary.cs @@ -7,7 +7,7 @@ namespace FluentAssertions.Execution; /// /// Represents a collection of data items that are associated with an . /// -internal class ContextDataItems +internal class ContextDataDictionary { private readonly List items = []; @@ -33,9 +33,9 @@ public string AsStringOrDefault(string key) return null; } - public void Add(ContextDataItems contextDataItems) + public void Add(ContextDataDictionary contextDataDictionary) { - foreach (DataItem item in contextDataItems.items) + foreach (DataItem item in contextDataDictionary.items) { Add(item.Clone()); } @@ -61,28 +61,20 @@ public T Get(string key) return (T)(item?.Value ?? default(T)); } - internal class DataItem + internal class DataItem(string key, object value, bool reportable, bool requiresFormatting) { - public DataItem(string key, object value, bool reportable, bool requiresFormatting) - { - Key = key; - Value = value; - Reportable = reportable; - RequiresFormatting = requiresFormatting; - } - - public string Key { get; } + public string Key { get; } = key; - public object Value { get; } + public object Value { get; } = value; - public bool Reportable { get; } + public bool Reportable { get; } = reportable; - public bool RequiresFormatting { get; } + public bool RequiresFormatting { get; } = requiresFormatting; public DataItem Clone() { - object value = Value is ICloneable2 cloneable ? cloneable.Clone() : Value; - return new DataItem(Key, value, Reportable, RequiresFormatting); + object clone = Value is ICloneable2 cloneable ? cloneable.Clone() : Value; + return new DataItem(Key, clone, Reportable, RequiresFormatting); } } } diff --git a/Src/FluentAssertions/Execution/MessageBuilder.cs b/Src/FluentAssertions/Execution/MessageBuilder.cs index faecdcb59b..65460111f5 100644 --- a/Src/FluentAssertions/Execution/MessageBuilder.cs +++ b/Src/FluentAssertions/Execution/MessageBuilder.cs @@ -30,7 +30,7 @@ public MessageBuilder(FormattingOptions formattingOptions) } // SMELL: Too many parameters. - public string Build(string message, object[] messageArgs, string reason, ContextDataItems contextData, string identifier, + public string Build(string message, object[] messageArgs, string reason, ContextDataDictionary contextData, string identifier, string fallbackIdentifier) { message = message.Replace("{reason}", SanitizeReason(reason), StringComparison.Ordinal); @@ -75,7 +75,7 @@ private static string SubstituteIdentifier(string message, string identifier, st return message.TrimStart(); } - private static string SubstituteContextualTags(string message, ContextDataItems contextData) + private static string SubstituteContextualTags(string message, ContextDataDictionary contextData) { const string pattern = @"(?[a-z|A-Z]+)(?:\:(?[a-z|A-Z|\s]+))?\}(?!\})"; diff --git a/Src/FluentAssertions/Formatting/FormattedObjectGraph.cs b/Src/FluentAssertions/Formatting/FormattedObjectGraph.cs index 750646ce27..3f5ba3550e 100644 --- a/Src/FluentAssertions/Formatting/FormattedObjectGraph.cs +++ b/Src/FluentAssertions/Formatting/FormattedObjectGraph.cs @@ -15,19 +15,13 @@ namespace FluentAssertions.Formatting; /// to the maximum number of lines provided through its constructor. It will throw /// a if the number of lines exceeds the maximum. /// -public class FormattedObjectGraph +public class FormattedObjectGraph(int maxLines) { - private readonly int maxLines; private readonly List lines = []; private readonly StringBuilder lineBuilder = new(); private int indentation; private string lineBuilderWhitespace = string.Empty; - public FormattedObjectGraph(int maxLines) - { - this.maxLines = maxLines; - } - /// /// The number of spaces that should be used by every indentation level. /// @@ -57,7 +51,7 @@ public void AddLine(string line) { FlushCurrentLine(); - AppendSafely(Whitespace + line); + AppendWithoutExceedingMaximumLines(Whitespace + line); } /// @@ -101,14 +95,14 @@ private void FlushCurrentLine() { if (lineBuilder.Length > 0) { - AppendSafely($"{lineBuilderWhitespace}{lineBuilder}"); + AppendWithoutExceedingMaximumLines($"{lineBuilderWhitespace}{lineBuilder}"); lineBuilder.Clear(); lineBuilderWhitespace = Whitespace; } } - private void AppendSafely(string line) + private void AppendWithoutExceedingMaximumLines(string line) { if (lines.Count == maxLines) { diff --git a/Src/FluentAssertions/Specialized/ExceptionAssertions.cs b/Src/FluentAssertions/Specialized/ExceptionAssertions.cs index c929bc330a..3cec526e9a 100644 --- a/Src/FluentAssertions/Specialized/ExceptionAssertions.cs +++ b/Src/FluentAssertions/Specialized/ExceptionAssertions.cs @@ -285,7 +285,7 @@ public static void Execute(IEnumerable messages, string expectation, [St } } - foreach (string failure in results.SelectClosestMatchFor()) + foreach (string failure in results.GetTheFailuresForTheSetWithTheFewestFailures()) { string replacedCurlyBraces = failure.EscapePlaceholders();