forked from fluentassertions/fluentassertions
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35da36b
commit 41679de
Showing
201 changed files
with
5,458 additions
and
5,951 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
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
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,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Formatting; | ||
|
||
namespace FluentAssertions; | ||
|
||
/// <summary> | ||
/// Provides a <see cref="Which"/> property that can be used in chained assertions where the prior assertions returns a | ||
/// single object that the assertion continues on. | ||
/// </summary> | ||
public class AndWhich<TParent, TSubject> : AndConstraint<TParent> | ||
{ | ||
private readonly AssertionChain assertionChain; | ||
private readonly string pathPostfix; | ||
private readonly Lazy<TSubject> getSubject; | ||
|
||
/// <summary> | ||
/// Creates an object that allows continuing an assertion executed through <paramref name="parent"/> and | ||
/// which resulted in a single <paramref name="subject"/> on an existing <paramref name="assertionChain"/>, but where | ||
/// the previous caller identifier is post-fixed with <paramref name="pathPostfix"/>. | ||
/// </summary> | ||
public AndWhich(TParent parent, TSubject subject, AssertionChain assertionChain, string pathPostfix) | ||
: base(parent) | ||
{ | ||
getSubject = new Lazy<TSubject>(() => subject); | ||
|
||
this.assertionChain = assertionChain; | ||
this.pathPostfix = pathPostfix; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an object that allows continuing an assertion executed through <paramref name="parent"/> and | ||
/// which resulted in a potential collection of objects through <paramref name="subjects"/> on an | ||
/// existing <paramref name="assertionChain"/>, but where | ||
/// the previous caller identifier is post-fixed with <paramref name="pathPostfix"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// If <paramref name="subjects"/> contains more than one object, a clear exception is thrown. | ||
/// </remarks> | ||
public AndWhich(TParent parent, IEnumerable<TSubject> subjects, AssertionChain assertionChain, string pathPostfix) | ||
: base(parent) | ||
{ | ||
getSubject = new Lazy<TSubject>(() => SingleOrDefault(subjects)); | ||
|
||
this.assertionChain = assertionChain; | ||
this.pathPostfix = pathPostfix; | ||
} | ||
|
||
/// <summary> | ||
/// Returns the single result of a prior assertion that is used to select a nested or collection item. | ||
/// </summary> | ||
/// <remarks> | ||
/// Just a convenience property that returns the same value as <see cref="Which"/>. | ||
/// </remarks> | ||
public TSubject Subject => Which; | ||
|
||
/// <summary> | ||
/// Returns the single result of a prior assertion that is used to select a nested or collection item. | ||
/// </summary> | ||
public TSubject Which | ||
{ | ||
get | ||
{ | ||
assertionChain.WithCallerPostfix(pathPostfix).ReuseOnce(); | ||
|
||
return getSubject.Value; | ||
} | ||
} | ||
|
||
private static TSubject SingleOrDefault(IEnumerable<TSubject> 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(); | ||
} | ||
} |
Oops, something went wrong.