-
-
Notifications
You must be signed in to change notification settings - Fork 539
/
EquivalencyValidator.cs
103 lines (85 loc) · 3.71 KB
/
EquivalencyValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions.Execution;
namespace FluentAssertions.Equivalency
{
/// <summary>
/// Is responsible for validating the equality of one or more properties of a subject with another object.
/// </summary>
public class EquivalencyValidator : IEquivalencyValidator
{
#region Private Definitions
private const int MaxDepth = 10;
private readonly IEquivalencyAssertionOptions config;
#endregion
public EquivalencyValidator(IEquivalencyAssertionOptions config)
{
this.config = config;
}
public void AssertEquality(EquivalencyValidationContext context)
{
using (var scope = new AssertionScope())
{
scope.AddReportable("configuration", config.ToString());
scope.AddNonReportable("objects", new ObjectTracker(config.CyclicReferenceHandling));
scope.BecauseOf(context.Because, context.BecauseArgs);
AssertEqualityUsing(context);
}
}
public void AssertEqualityUsing(IEquivalencyValidationContext context)
{
if (ContinueRecursion(context.SelectedMemberPath))
{
AssertionScope scope = AssertionScope.Current;
scope.AddNonReportable("context", (context.SelectedMemberDescription.Length == 0) ? "subject" : context.SelectedMemberDescription);
scope.AddNonReportable("subject", context.Subject);
scope.AddNonReportable("expectation", context.Expectation);
var objectTracker = scope.Get<ObjectTracker>("objects");
if (!objectTracker.IsCyclicReference(new ObjectReference(context.Subject, context.SelectedMemberPath)))
{
bool wasHandled = false;
foreach (var step in AssertionOptions.EquivalencySteps)
{
if (step.CanHandle(context, config))
{
if (step.Handle(context, this, config))
{
wasHandled = true;
break;
}
}
}
if (!wasHandled)
{
Execute.Assertion.FailWith(
"No IEquivalencyStep was found to handle the context. " +
"This is likely a bug in Fluent Assertions.");
}
}
}
}
private bool ContinueRecursion(string memberAccessPath)
{
if (config.AllowInfiniteRecursion || !HasReachedMaximumRecursionDepth(memberAccessPath))
{
return true;
}
AssertionScope.Current.FailWith(
"The maximum recursion depth was reached. " +
"The maximum recursion depth limitation prevents stack overflow from " +
"occuring when certain types of cycles exist in the object graph " +
"or the object graph's depth is very high or infinite. " +
"This limitation may be disabled using the config parameter." +
Environment.NewLine + Environment.NewLine +
"The member access chain when max depth was hit was: " +
memberAccessPath);
return false;
}
private static bool HasReachedMaximumRecursionDepth(string propertyPath)
{
int depth = propertyPath.Cast<char>().Count(chr => chr == '.');
return (depth >= MaxDepth);
}
}
}