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
c70bd7b
commit 7036b09
Showing
194 changed files
with
5,415 additions
and
5,765 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions.Common; | ||
using FluentAssertions.Execution; | ||
using FluentAssertions.Formatting; | ||
|
||
namespace FluentAssertions; | ||
|
||
public class AndWhich<TParent, TSubject> : AndConstraint<TParent> | ||
{ | ||
private readonly AssertionChain assertionChain; | ||
private readonly string pathPostfix; | ||
private readonly Lazy<TSubject> getSubject; | ||
|
||
public AndWhich(TParent parent, TSubject subject, AssertionChain assertionChain, string pathPostfix) | ||
: base(parent) | ||
{ | ||
getSubject = new Lazy<TSubject>(() => subject); | ||
|
||
this.assertionChain = assertionChain; | ||
this.pathPostfix = pathPostfix; | ||
} | ||
|
||
public AndWhich(TParent parent, IEnumerable<TSubject> subjects) | ||
: base(parent) | ||
{ | ||
getSubject = new Lazy<TSubject>(() => SingleOrDefault(subjects)); | ||
} | ||
|
||
/// <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.