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 d4f4274992..894d101dc9 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;
@@ -148,7 +149,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.
///
@@ -166,7 +166,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());
}
///
@@ -176,7 +176,7 @@ public static ExecutionTimeAssertions Should(this ExecutionTime executionTime)
[Pure]
public static AssemblyAssertions Should(this Assembly assembly)
{
- return new AssemblyAssertions(assembly);
+ return new AssemblyAssertions(assembly, AssertionChain.GetOrCreate());
}
///
@@ -186,7 +186,7 @@ public static AssemblyAssertions Should(this Assembly assembly)
[Pure]
public static XDocumentAssertions Should(this XDocument actualValue)
{
- return new XDocumentAssertions(actualValue);
+ return new XDocumentAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -196,7 +196,7 @@ public static XDocumentAssertions Should(this XDocument actualValue)
[Pure]
public static XElementAssertions Should(this XElement actualValue)
{
- return new XElementAssertions(actualValue);
+ return new XElementAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -206,7 +206,7 @@ public static XElementAssertions Should(this XElement actualValue)
[Pure]
public static XAttributeAssertions Should(this XAttribute actualValue)
{
- return new XAttributeAssertions(actualValue);
+ return new XAttributeAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -216,7 +216,7 @@ public static XAttributeAssertions Should(this XAttribute actualValue)
[Pure]
public static StreamAssertions Should(this Stream actualValue)
{
- return new StreamAssertions(actualValue);
+ return new StreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -226,7 +226,7 @@ public static StreamAssertions Should(this Stream actualValue)
[Pure]
public static BufferedStreamAssertions Should(this BufferedStream actualValue)
{
- return new BufferedStreamAssertions(actualValue);
+ return new BufferedStreamAssertions(actualValue, AssertionChain.GetOrCreate());
}
///
@@ -283,7 +283,12 @@ private static void ForceEnumeration(T subject, Func enumerab
[Pure]
public static ObjectAssertions Should(this object actualValue)
{
- return new ObjectAssertions(actualValue);
+ return new ObjectAssertions(actualValue, AssertionChain.GetOrCreate());
+ }
+
+ public static ObjectAssertions Should(this WhichResult