diff --git a/.editorconfig b/.editorconfig
index 5432624a17..f4874fecc7 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -331,6 +331,10 @@ resharper_parentheses_redundancy_style = remove_if_not_clarifies_precedence
dotnet_analyzer_diagnostic.category-roslynator.severity = error
+# Purpose: Remove trailing white-space
+# Reason: It also complains about this when pressing enter to start a new empty line.
+dotnet_diagnostic.RCS1037.severity = none
+
# Remove suffix 'Async' from non-asynchronous method name. Disabled because we like that suffix for now.
dotnet_diagnostic.RCS1047.severity = none
diff --git a/Build/_build.csproj.DotSettings b/Build/_build.csproj.DotSettings
index 9aac7d8e8d..7348ae5acb 100644
--- a/Build/_build.csproj.DotSettings
+++ b/Build/_build.csproj.DotSettings
@@ -13,6 +13,8 @@
False
<Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" />
+ <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy>
+ <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /></Policy>
True
True
True
@@ -21,4 +23,5 @@
True
True
True
- True
+ True
+ True
diff --git a/FluentAssertions.sln.DotSettings b/FluentAssertions.sln.DotSettings
index 692c8c68da..785299fbe3 100644
--- a/FluentAssertions.sln.DotSettings
+++ b/FluentAssertions.sln.DotSettings
@@ -104,6 +104,8 @@
UseExplicitType
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
+ <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy>
+ <Policy><Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static fields (private)"><ElementKinds><Kind Name="FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy>
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" />
<Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" />
@@ -154,26 +156,27 @@
True
True
True
+ True
D:\Workspaces\FluentAssertions\Default.testsettings
4
False
True
- True
- 1
- True
- 0
+ False
+
+ False
+
aaa
Arrange-Act-Assert
- [TestMethod]
-public void When_$scenario$_it_should_$behavior$()
-{
- // Arrange
- $END$
-
- // Act
-
-
- // Assert
+ [Fact]
+public void $END$()
+{
+ // Arrange
+
+
+ // Act
+
+
+ // Assert
}
True
True
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/AndWhich.cs b/Src/FluentAssertions/AndWhich.cs
new file mode 100644
index 0000000000..0e0e2d30ce
--- /dev/null
+++ b/Src/FluentAssertions/AndWhich.cs
@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using FluentAssertions.Common;
+using FluentAssertions.Execution;
+using FluentAssertions.Formatting;
+
+namespace FluentAssertions;
+
+public class AndWhich : AndConstraint
+{
+ private readonly AssertionChain assertionChain;
+ private readonly string pathPostfix;
+ private readonly Lazy getSubject;
+
+ public AndWhich(TParent parent, TSubject subject, AssertionChain assertionChain, string pathPostfix)
+ : base(parent)
+ {
+ getSubject = new Lazy(() => subject);
+
+ this.assertionChain = assertionChain;
+ this.pathPostfix = pathPostfix;
+ }
+
+ public AndWhich(TParent parent, IEnumerable subjects)
+ : base(parent)
+ {
+ getSubject = new Lazy(() => SingleOrDefault(subjects));
+ }
+
+ ///
+ /// Returns the single result of a prior assertion that is used to select a nested or collection item.
+ ///
+ ///
+ /// Just a convenience property that returns the same value as .
+ ///
+ public TSubject Subject => Which;
+
+ ///
+ /// Returns the single result of a prior assertion that is used to select a nested or collection item.
+ ///
+ public TSubject Which
+ {
+ get
+ {
+ assertionChain.AddCallerPostfix(pathPostfix);
+
+ AssertionChain.ReuseOnce(assertionChain);
+
+ return getSubject.Value;
+ }
+ }
+
+ private static TSubject SingleOrDefault(IEnumerable subjects)
+ {
+ TSubject[] matchedElements = subjects.ToArray();
+ if (matchedElements.Length > 1)
+ {
+ string foundObjects = string.Join(Environment.NewLine,
+ matchedElements.Select(ele => "\t" + Formatter.ToString(ele)));
+
+ string message = "More than one object found. FluentAssertions cannot determine which object is meant."
+ + $" Found objects:{Environment.NewLine}{foundObjects}";
+
+ Services.ThrowException(message);
+ }
+
+ return matchedElements.Single();
+ }
+}
diff --git a/Src/FluentAssertions/AssertionExtensions.cs b/Src/FluentAssertions/AssertionExtensions.cs
index 0c69d58c41..e1dd0d7918 100644
--- a/Src/FluentAssertions/AssertionExtensions.cs
+++ b/Src/FluentAssertions/AssertionExtensions.cs
@@ -11,6 +11,7 @@
using System.Xml.Linq;
using FluentAssertions.Collections;
using FluentAssertions.Common;
+using FluentAssertions.Execution;
using FluentAssertions.Numeric;
using FluentAssertions.Primitives;
using FluentAssertions.Specialized;
@@ -149,7 +150,6 @@ public static ExecutionTime ExecutionTime(this Action action, StartTimer createT
///
/// Provides methods for asserting the execution time of an async action.
///
- /// An async action to measure the execution time of.
///
/// Returns an object for asserting that the execution time matches certain conditions.
///
@@ -167,7 +167,7 @@ public static ExecutionTime ExecutionTime(this Func action)
[Pure]
public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
{
- return new ExecutionTimeAssertions(executionTime);
+ return new ExecutionTimeAssertions(executionTime, AssertionChain.GetOrCreate());
}
///
@@ -177,7 +177,7 @@ public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
[Pure]
public static AssemblyAssertions Should([NotNull] this Assembly assembly)
{
- return new AssemblyAssertions(assembly);
+ return new AssemblyAssertions(assembly, AssertionChain.GetOrCreate());
}
///
@@ -187,7 +187,7 @@ public static AssemblyAssertions Should([NotNull] this Assembly assembly)
[Pure]
public static XDocumentAssertions Should([NotNull] this XDocument actualValue)
{
- return new XDocumentAssertions(actualValue);
+ return new XDocumentAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -197,7 +197,7 @@ public static XDocumentAssertions Should([NotNull] this XDocument actualValue)
[Pure]
public static XElementAssertions Should([NotNull] this XElement actualValue)
{
- return new XElementAssertions(actualValue);
+ return new XElementAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -207,7 +207,7 @@ public static XElementAssertions Should([NotNull] this XElement actualValue)
[Pure]
public static XAttributeAssertions Should([NotNull] this XAttribute actualValue)
{
- return new XAttributeAssertions(actualValue);
+ return new XAttributeAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -217,7 +217,7 @@ public static XAttributeAssertions Should([NotNull] this XAttribute actualValue)
[Pure]
public static StreamAssertions Should([NotNull] this Stream actualValue)
{
- return new StreamAssertions(actualValue);
+ return new StreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -227,7 +227,7 @@ public static StreamAssertions Should([NotNull] this Stream actualValue)
[Pure]
public static BufferedStreamAssertions Should([NotNull] this BufferedStream actualValue)
{
- return new BufferedStreamAssertions(actualValue);
+ return new BufferedStreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -284,7 +284,7 @@ private static void ForceEnumeration(T subject, Func enumerab
[Pure]
public static ObjectAssertions Should([NotNull] this object actualValue)
{
- return new ObjectAssertions(actualValue);
+ return new ObjectAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -294,7 +294,7 @@ public static ObjectAssertions Should([NotNull] this object actualValue)
[Pure]
public static BooleanAssertions Should(this bool actualValue)
{
- return new BooleanAssertions(actualValue);
+ return new BooleanAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -304,7 +304,7 @@ public static BooleanAssertions Should(this bool actualValue)
[Pure]
public static NullableBooleanAssertions Should(this bool? actualValue)
{
- return new NullableBooleanAssertions(actualValue);
+ return new NullableBooleanAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -314,7 +314,7 @@ public static NullableBooleanAssertions Should(this bool? actualValue)
[Pure]
public static HttpResponseMessageAssertions Should([NotNull] this HttpResponseMessage actualValue)
{
- return new HttpResponseMessageAssertions(actualValue);
+ return new HttpResponseMessageAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -324,7 +324,7 @@ public static HttpResponseMessageAssertions Should([NotNull] this HttpResponseMe
[Pure]
public static GuidAssertions Should(this Guid actualValue)
{
- return new GuidAssertions(actualValue);
+ return new GuidAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -334,7 +334,7 @@ public static GuidAssertions Should(this Guid actualValue)
[Pure]
public static NullableGuidAssertions Should(this Guid? actualValue)
{
- return new NullableGuidAssertions(actualValue);
+ return new NullableGuidAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -344,7 +344,7 @@ public static NullableGuidAssertions Should(this Guid? actualValue)
[Pure]
public static GenericCollectionAssertions Should([NotNull] this IEnumerable actualValue)
{
- return new GenericCollectionAssertions(actualValue);
+ return new GenericCollectionAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -354,7 +354,7 @@ public static GenericCollectionAssertions Should([NotNull] this IEnumerabl
[Pure]
public static StringCollectionAssertions Should([NotNull] this IEnumerable @this)
{
- return new StringCollectionAssertions(@this);
+ return new StringCollectionAssertions(@this, AssertionChain.GetOrCreate());
}
///
@@ -365,7 +365,7 @@ public static StringCollectionAssertions Should([NotNull] this IEnumerable, TKey, TValue> Should(
[NotNull] this IDictionary actualValue)
{
- return new GenericDictionaryAssertions, TKey, TValue>(actualValue);
+ return new GenericDictionaryAssertions, TKey, TValue>(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -376,7 +376,8 @@ public static GenericDictionaryAssertions, TKey, TValu
public static GenericDictionaryAssertions>, TKey, TValue> Should(
[NotNull] this IEnumerable> actualValue)
{
- return new GenericDictionaryAssertions>, TKey, TValue>(actualValue);
+ return new GenericDictionaryAssertions>, TKey, TValue>(actualValue,
+ AssertionChain.GetOrCreate());
}
///
@@ -388,7 +389,7 @@ public static GenericDictionaryAssertions Should>
{
- return new GenericDictionaryAssertions(actualValue);
+ return new GenericDictionaryAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -398,7 +399,7 @@ public static GenericDictionaryAssertions Should
@@ -408,7 +409,7 @@ public static DateTimeAssertions Should(this DateTime actualValue)
[Pure]
public static DateTimeOffsetAssertions Should(this DateTimeOffset actualValue)
{
- return new DateTimeOffsetAssertions(actualValue);
+ return new DateTimeOffsetAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -418,7 +419,7 @@ public static DateTimeOffsetAssertions Should(this DateTimeOffset actualValue)
[Pure]
public static NullableDateTimeAssertions Should(this DateTime? actualValue)
{
- return new NullableDateTimeAssertions(actualValue);
+ return new NullableDateTimeAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -428,7 +429,7 @@ public static NullableDateTimeAssertions Should(this DateTime? actualValue)
[Pure]
public static NullableDateTimeOffsetAssertions Should(this DateTimeOffset? actualValue)
{
- return new NullableDateTimeOffsetAssertions(actualValue);
+ return new NullableDateTimeOffsetAssertions(actualValue, AssertionChain.GetOrCreate());
}
#if NET6_0_OR_GREATER
@@ -439,7 +440,7 @@ public static NullableDateTimeOffsetAssertions Should(this DateTimeOffset? actua
[Pure]
public static DateOnlyAssertions Should(this DateOnly actualValue)
{
- return new DateOnlyAssertions(actualValue);
+ return new DateOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -449,7 +450,7 @@ public static DateOnlyAssertions Should(this DateOnly actualValue)
[Pure]
public static NullableDateOnlyAssertions Should(this DateOnly? actualValue)
{
- return new NullableDateOnlyAssertions(actualValue);
+ return new NullableDateOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -459,7 +460,7 @@ public static NullableDateOnlyAssertions Should(this DateOnly? actualValue)
[Pure]
public static TimeOnlyAssertions Should(this TimeOnly actualValue)
{
- return new TimeOnlyAssertions(actualValue);
+ return new TimeOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -469,7 +470,7 @@ public static TimeOnlyAssertions Should(this TimeOnly actualValue)
[Pure]
public static NullableTimeOnlyAssertions Should(this TimeOnly? actualValue)
{
- return new NullableTimeOnlyAssertions(actualValue);
+ return new NullableTimeOnlyAssertions(actualValue, AssertionChain.GetOrCreate());
}
#endif
@@ -481,7 +482,7 @@ public static NullableTimeOnlyAssertions Should(this TimeOnly? actualValue)
[Pure]
public static ComparableTypeAssertions Should([NotNull] this IComparable comparableValue)
{
- return new ComparableTypeAssertions(comparableValue);
+ return new ComparableTypeAssertions(comparableValue, AssertionChain.GetOrCreate());
}
///
@@ -491,7 +492,7 @@ public static ComparableTypeAssertions Should([NotNull] this IComparable Should(this int actualValue)
{
- return new Int32Assertions(actualValue);
+ return new Int32Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -501,7 +502,7 @@ public static NumericAssertions Should(this int actualValue)
[Pure]
public static NullableNumericAssertions Should(this int? actualValue)
{
- return new NullableInt32Assertions(actualValue);
+ return new NullableInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -511,7 +512,7 @@ public static NullableNumericAssertions Should(this int? actualValue)
[Pure]
public static NumericAssertions Should(this uint actualValue)
{
- return new UInt32Assertions(actualValue);
+ return new UInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -521,7 +522,7 @@ public static NumericAssertions Should(this uint actualValue)
[Pure]
public static NullableNumericAssertions Should(this uint? actualValue)
{
- return new NullableUInt32Assertions(actualValue);
+ return new NullableUInt32Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -531,7 +532,7 @@ public static NullableNumericAssertions Should(this uint? actualValue)
[Pure]
public static NumericAssertions Should(this decimal actualValue)
{
- return new DecimalAssertions(actualValue);
+ return new DecimalAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -541,7 +542,7 @@ public static NumericAssertions Should(this decimal actualValue)
[Pure]
public static NullableNumericAssertions Should(this decimal? actualValue)
{
- return new NullableDecimalAssertions(actualValue);
+ return new NullableDecimalAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -551,7 +552,7 @@ public static NullableNumericAssertions Should(this decimal? actualValu
[Pure]
public static NumericAssertions Should(this byte actualValue)
{
- return new ByteAssertions(actualValue);
+ return new ByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -561,7 +562,7 @@ public static NumericAssertions Should(this byte actualValue)
[Pure]
public static NullableNumericAssertions Should(this byte? actualValue)
{
- return new NullableByteAssertions(actualValue);
+ return new NullableByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -571,7 +572,7 @@ public static NullableNumericAssertions Should(this byte? actualValue)
[Pure]
public static NumericAssertions Should(this sbyte actualValue)
{
- return new SByteAssertions(actualValue);
+ return new SByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -581,7 +582,7 @@ public static NumericAssertions Should(this sbyte actualValue)
[Pure]
public static NullableNumericAssertions Should(this sbyte? actualValue)
{
- return new NullableSByteAssertions(actualValue);
+ return new NullableSByteAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -591,7 +592,7 @@ public static NullableNumericAssertions Should(this sbyte? actualValue)
[Pure]
public static NumericAssertions Should(this short actualValue)
{
- return new Int16Assertions(actualValue);
+ return new Int16Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -601,7 +602,7 @@ public static NumericAssertions Should(this short actualValue)
[Pure]
public static NullableNumericAssertions Should(this short? actualValue)
{
- return new NullableInt16Assertions(actualValue);
+ return new NullableInt16Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -611,7 +612,7 @@ public static NullableNumericAssertions Should(this short? actualValue)
[Pure]
public static NumericAssertions Should(this ushort actualValue)
{
- return new UInt16Assertions(actualValue);
+ return new UInt16Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -621,7 +622,7 @@ public static NumericAssertions Should(this ushort actualValue)
[Pure]
public static NullableNumericAssertions Should(this ushort? actualValue)
{
- return new NullableUInt16Assertions(actualValue);
+ return new NullableUInt16Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -631,7 +632,7 @@ public static NullableNumericAssertions Should(this ushort? actualValue)
[Pure]
public static NumericAssertions Should(this long actualValue)
{
- return new Int64Assertions(actualValue);
+ return new Int64Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -641,7 +642,7 @@ public static NumericAssertions Should(this long actualValue)
[Pure]
public static NullableNumericAssertions Should(this long? actualValue)
{
- return new NullableInt64Assertions(actualValue);
+ return new NullableInt64Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -651,7 +652,7 @@ public static NullableNumericAssertions Should(this long? actualValue)
[Pure]
public static NumericAssertions Should(this ulong actualValue)
{
- return new UInt64Assertions(actualValue);
+ return new UInt64Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -661,7 +662,7 @@ public static NumericAssertions Should(this ulong actualValue)
[Pure]
public static NullableNumericAssertions Should(this ulong? actualValue)
{
- return new NullableUInt64Assertions(actualValue);
+ return new NullableUInt64Assertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -671,7 +672,7 @@ public static NullableNumericAssertions Should(this ulong? actualValue)
[Pure]
public static NumericAssertions Should(this float actualValue)
{
- return new SingleAssertions(actualValue);
+ return new SingleAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -681,7 +682,7 @@ public static NumericAssertions Should(this float actualValue)
[Pure]
public static NullableNumericAssertions Should(this float? actualValue)
{
- return new NullableSingleAssertions(actualValue);
+ return new NullableSingleAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -691,7 +692,7 @@ public static NullableNumericAssertions Should(this float? actualValue)
[Pure]
public static NumericAssertions Should(this double actualValue)
{
- return new DoubleAssertions(actualValue);
+ return new DoubleAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -701,7 +702,7 @@ public static NumericAssertions Should(this double actualValue)
[Pure]
public static NullableNumericAssertions Should(this double? actualValue)
{
- return new NullableDoubleAssertions(actualValue);
+ return new NullableDoubleAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -711,7 +712,8 @@ public static NullableNumericAssertions Should(this double? actualValue)
[Pure]
public static StringAssertions Should([NotNull] this string actualValue)
{
- return new StringAssertions(actualValue);
+ return new StringAssertions(actualValue,
+ AssertionChain.GetOrCreate());
}
///
@@ -721,7 +723,7 @@ public static StringAssertions Should([NotNull] this string actualValue)
[Pure]
public static SimpleTimeSpanAssertions Should(this TimeSpan actualValue)
{
- return new SimpleTimeSpanAssertions(actualValue);
+ return new SimpleTimeSpanAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -731,7 +733,7 @@ public static SimpleTimeSpanAssertions Should(this TimeSpan actualValue)
[Pure]
public static NullableSimpleTimeSpanAssertions Should(this TimeSpan? actualValue)
{
- return new NullableSimpleTimeSpanAssertions(actualValue);
+ return new NullableSimpleTimeSpanAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -741,7 +743,7 @@ public static NullableSimpleTimeSpanAssertions Should(this TimeSpan? actualValue
[Pure]
public static TypeAssertions Should([NotNull] this Type subject)
{
- return new TypeAssertions(subject);
+ return new TypeAssertions(subject, AssertionChain.GetOrCreate());
}
///
@@ -754,7 +756,7 @@ public static TypeSelectorAssertions Should(this TypeSelector typeSelector)
{
Guard.ThrowIfArgumentIsNull(typeSelector);
- return new TypeSelectorAssertions(typeSelector.ToArray());
+ return new TypeSelectorAssertions(AssertionChain.GetOrCreate(), typeSelector.ToArray());
}
///
@@ -765,7 +767,7 @@ public static TypeSelectorAssertions Should(this TypeSelector typeSelector)
[Pure]
public static ConstructorInfoAssertions Should([NotNull] this ConstructorInfo constructorInfo)
{
- return new ConstructorInfoAssertions(constructorInfo);
+ return new ConstructorInfoAssertions(constructorInfo, AssertionChain.GetOrCreate());
}
///
@@ -775,7 +777,7 @@ public static ConstructorInfoAssertions Should([NotNull] this ConstructorInfo co
[Pure]
public static MethodInfoAssertions Should([NotNull] this MethodInfo methodInfo)
{
- return new MethodInfoAssertions(methodInfo);
+ return new MethodInfoAssertions(methodInfo, AssertionChain.GetOrCreate());
}
///
@@ -789,7 +791,7 @@ public static MethodInfoSelectorAssertions Should(this MethodInfoSelector method
{
Guard.ThrowIfArgumentIsNull(methodSelector);
- return new MethodInfoSelectorAssertions(methodSelector.ToArray());
+ return new MethodInfoSelectorAssertions(AssertionChain.GetOrCreate(), methodSelector.ToArray());
}
///
@@ -800,7 +802,7 @@ public static MethodInfoSelectorAssertions Should(this MethodInfoSelector method
[Pure]
public static PropertyInfoAssertions Should([NotNull] this PropertyInfo propertyInfo)
{
- return new PropertyInfoAssertions(propertyInfo);
+ return new PropertyInfoAssertions(propertyInfo, AssertionChain.GetOrCreate());
}
///
@@ -814,7 +816,7 @@ public static PropertyInfoSelectorAssertions Should(this PropertyInfoSelector pr
{
Guard.ThrowIfArgumentIsNull(propertyInfoSelector);
- return new PropertyInfoSelectorAssertions(propertyInfoSelector.ToArray());
+ return new PropertyInfoSelectorAssertions(AssertionChain.GetOrCreate(), propertyInfoSelector.ToArray());
}
///
@@ -824,7 +826,7 @@ public static PropertyInfoSelectorAssertions Should(this PropertyInfoSelector pr
[Pure]
public static ActionAssertions Should([NotNull] this Action action)
{
- return new ActionAssertions(action, Extractor);
+ return new ActionAssertions(action, Extractor, AssertionChain.GetOrCreate());
}
///
@@ -834,7 +836,7 @@ public static ActionAssertions Should([NotNull] this Action action)
[Pure]
public static NonGenericAsyncFunctionAssertions Should([NotNull] this Func action)
{
- return new NonGenericAsyncFunctionAssertions(action, Extractor);
+ return new NonGenericAsyncFunctionAssertions(action, Extractor, AssertionChain.GetOrCreate());
}
///
@@ -844,7 +846,7 @@ public static NonGenericAsyncFunctionAssertions Should([NotNull] this Func
[Pure]
public static GenericAsyncFunctionAssertions Should([NotNull] this Func> action)
{
- return new GenericAsyncFunctionAssertions(action, Extractor);
+ return new GenericAsyncFunctionAssertions(action, Extractor, AssertionChain.GetOrCreate());
}
///
@@ -854,7 +856,7 @@ public static GenericAsyncFunctionAssertions Should([NotNull] this Func Should([NotNull] this Func func)
{
- return new FunctionAssertions(func, Extractor);
+ return new FunctionAssertions(func, Extractor, AssertionChain.GetOrCreate());
}
///
@@ -864,7 +866,7 @@ public static FunctionAssertions Should([NotNull] this Func func)
[Pure]
public static TaskCompletionSourceAssertions Should(this TaskCompletionSource tcs)
{
- return new TaskCompletionSourceAssertions(tcs);
+ return new TaskCompletionSourceAssertions(tcs, AssertionChain.GetOrCreate());
}
#if !NETSTANDARD2_0
@@ -906,7 +908,7 @@ public static IMonitor Monitor(this T eventSource, Action : GenericCollectionAssertions, T, GenericCollectionAssertions>
{
- public GenericCollectionAssertions(IEnumerable actualValue)
- : base(actualValue)
+ public GenericCollectionAssertions(IEnumerable actualValue, AssertionChain assertionChain)
+ : base(actualValue, assertionChain)
{
}
}
@@ -28,22 +28,26 @@ public class GenericCollectionAssertions
: GenericCollectionAssertions>
where TCollection : IEnumerable
{
- public GenericCollectionAssertions(TCollection actualValue)
- : base(actualValue)
+ public GenericCollectionAssertions(TCollection actualValue, AssertionChain assertionChain)
+ : base(actualValue, assertionChain)
{
}
}
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
-[DebuggerNonUserCode]
+
+// [DebuggerNonUserCode]
public class GenericCollectionAssertions : ReferenceTypeAssertions
where TCollection : IEnumerable
where TAssertions : GenericCollectionAssertions
{
- public GenericCollectionAssertions(TCollection actualValue)
- : base(actualValue)
+ private readonly AssertionChain assertionChain;
+
+ public GenericCollectionAssertions(TCollection actualValue, AssertionChain assertionChain)
+ : base(actualValue, assertionChain)
{
+ this.assertionChain = assertionChain;
}
///
@@ -62,10 +66,10 @@ public GenericCollectionAssertions(TCollection actualValue)
///
/// Zero or more objects to format using the placeholders in .
///
- public AndWhichConstraint> AllBeAssignableTo(
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ public AndWhichConstraint> AllBeAssignableTo(string because = "",
+ params object[] becauseArgs)
{
- bool success = Execute.Assertion
+ assertionChain
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected type to be {0}{reason}, but found {context:the collection} is .",
@@ -73,18 +77,18 @@ public AndWhichConstraint> AllBeAssignabl
IEnumerable matches = [];
- if (success)
+ if (assertionChain.Succeeded)
{
- Execute.Assertion
+ using var _ = assertionChain
.BecauseOf(because, becauseArgs)
- .WithExpectation("Expected type to be {0}{reason}, ", typeof(TExpectation).FullName)
+ .WithExpectation("Expected type to be {0}{reason}, ", typeof(TExpectation).FullName);
+
+ assertionChain
.ForCondition(Subject!.All(x => x is not null))
.FailWith("but found a null element.")
.Then
.ForCondition(Subject.All(x => typeof(TExpectation).IsAssignableFrom(GetType(x))))
- .FailWith("but found {0}.", () => $"[{string.Join(", ", Subject.Select(x => GetType(x).FullName))}]")
- .Then
- .ClearExpectation();
+ .FailWith("but found {0}.", () => $"[{string.Join(", ", Subject.Select(x => GetType(x).FullName))}]");
matches = Subject.OfType();
}
@@ -104,14 +108,15 @@ public AndWhichConstraint> AllBeAssignabl
/// Zero or more objects to format using the placeholders in .
///
/// is .
- public AndConstraint AllBeAssignableTo(Type expectedType,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ public AndConstraint AllBeAssignableTo(Type expectedType, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedType);
- Execute.Assertion
+ using var _ = assertionChain
.BecauseOf(because, becauseArgs)
- .WithExpectation("Expected type to be {0}{reason}, ", expectedType.FullName)
+ .WithExpectation("Expected type to be {0}{reason}, ", expectedType.FullName);
+
+ assertionChain
.Given(() => Subject)
.ForCondition(subject => subject is not null)
.FailWith("but found {context:collection} is .")
@@ -120,9 +125,7 @@ public AndConstraint AllBeAssignableTo(Type expectedType,
.FailWith("but found a null element.")
.Then
.ForCondition(subject => subject.All(x => expectedType.IsAssignableFrom(GetType(x))))
- .FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]")
- .Then
- .ClearExpectation();
+ .FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]");
return new AndConstraint((TAssertions)this);
}
@@ -147,7 +150,7 @@ public AndConstraint AllBeAssignableTo(Type expectedType,
/// Zero or more objects to format using the placeholders in .
///
public AndConstraint AllBeEquivalentTo(TExpectation expectation,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ string because = "", params object[] becauseArgs)
{
return AllBeEquivalentTo(expectation, options => options, because, becauseArgs);
}
@@ -180,7 +183,8 @@ public AndConstraint AllBeEquivalentTo(TExpectation e
/// is .
public AndConstraint AllBeEquivalentTo(TExpectation expectation,
Func, EquivalencyOptions> config,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ string because = "",
+ params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config);
@@ -209,10 +213,10 @@ public AndConstraint AllBeEquivalentTo(TExpectation e
///
/// Zero or more objects to format using the placeholders in .
///
- public AndWhichConstraint> AllBeOfType(
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ public AndWhichConstraint> AllBeOfType(string because = "",
+ params object[] becauseArgs)
{
- bool success = Execute.Assertion
+ assertionChain
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected type to be {0}{reason}, but found {context:collection} is .",
@@ -220,18 +224,18 @@ public AndWhichConstraint> AllBeOfType matches = [];
- if (success)
+ if (assertionChain.Succeeded)
{
- Execute.Assertion
+ using var _ = assertionChain
.BecauseOf(because, becauseArgs)
- .WithExpectation("Expected type to be {0}{reason}, ", typeof(TExpectation).FullName)
+ .WithExpectation("Expected type to be {0}{reason}, ", typeof(TExpectation).FullName);
+
+ assertionChain
.ForCondition(Subject!.All(x => x is not null))
.FailWith("but found a null element.")
.Then
.ForCondition(Subject.All(x => typeof(TExpectation) == GetType(x)))
- .FailWith("but found {0}.", () => $"[{string.Join(", ", Subject.Select(x => GetType(x).FullName))}]")
- .Then
- .ClearExpectation();
+ .FailWith("but found {0}.", () => $"[{string.Join(", ", Subject.Select(x => GetType(x).FullName))}]");
matches = Subject.OfType();
}
@@ -251,14 +255,15 @@ public AndWhichConstraint> AllBeOfType.
///
/// is .
- public AndConstraint AllBeOfType(Type expectedType,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ public AndConstraint AllBeOfType(Type expectedType, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedType);
- Execute.Assertion
+ using var _ = assertionChain
.BecauseOf(because, becauseArgs)
- .WithExpectation("Expected type to be {0}{reason}, ", expectedType.FullName)
+ .WithExpectation("Expected type to be {0}{reason}, ", expectedType.FullName);
+
+ assertionChain
.Given(() => Subject)
.ForCondition(subject => subject is not null)
.FailWith("but found {context:collection} is .")
@@ -267,9 +272,7 @@ public AndConstraint AllBeOfType(Type expectedType,
.FailWith("but found a null element.")
.Then
.ForCondition(subject => subject.All(x => expectedType == GetType(x)))
- .FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]")
- .Then
- .ClearExpectation();
+ .FailWith("but found {0}.", subject => $"[{string.Join(", ", subject.Select(x => GetType(x).FullName))}]");
return new AndConstraint((TAssertions)this);
}
@@ -284,20 +287,20 @@ public AndConstraint AllBeOfType(Type expectedType,
///
/// Zero or more objects to format using the placeholders in .
///
- public AndConstraint BeEmpty([StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ public AndConstraint BeEmpty(string because = "", params object[] becauseArgs)
{
var singleItemArray = Subject?.Take(1).ToArray();
- Execute.Assertion
+
+ using var _ = assertionChain
.BecauseOf(because, becauseArgs)
- .WithExpectation("Expected {context:collection} to be empty{reason}, ")
- .Given(() => singleItemArray)
+ .WithExpectation("Expected {context:collection} to be empty{reason}, ");
+
+ assertionChain.Given(() => singleItemArray)
.ForCondition(subject => subject is not null)
.FailWith("but found .")
.Then
.ForCondition(subject => subject.Length == 0)
- .FailWith("but found at least one item {0}.", singleItemArray)
- .Then
- .ClearExpectation();
+ .FailWith("but found at least one item {0}.", singleItemArray);
return new AndConstraint((TAssertions)this);
}
@@ -322,7 +325,7 @@ public AndConstraint BeEmpty([StringSyntax("CompositeFormat")] stri
/// Zero or more objects to format using the placeholders in .
///
public AndConstraint BeEquivalentTo(IEnumerable expectation,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ string because = "", params object[] becauseArgs)
{
return BeEquivalentTo(expectation, config => config, because, becauseArgs);
}
@@ -354,8 +357,8 @@ public AndConstraint BeEquivalentTo(IEnumerable
/// is .
public AndConstraint BeEquivalentTo(IEnumerable expectation,
- Func, EquivalencyOptions> config,
- [StringSyntax("CompositeFormat")] string because = "", params object[] becauseArgs)
+ Func, EquivalencyOptions> config, string because = "",
+ params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(config);
@@ -363,7 +366,7 @@ public AndConstraint BeEquivalentTo(IEnumerable()).AsCollection();
var context =
- new EquivalencyValidationContext(Node.From>(() => AssertionScope.Current.CallerIdentity),
+ new EquivalencyValidationContext(Node.From>(() => CurrentAssertionChain.CallerIdentifier),
options)
{
Reason = new Reason(because, becauseArgs),
@@ -400,8 +403,7 @@ public AndConstraint BeEquivalentTo(IEnumerable
public AndConstraint> BeInAscendingOrder(
- Expression