/
ObjectReference.cs
70 lines (61 loc) · 2.56 KB
/
ObjectReference.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
using System;
using System.Linq;
using FluentAssertions.Common;
namespace FluentAssertions.Equivalency
{
/// <summary>
/// Represents an object tracked by the <see cref="CyclicReferenceDetector"/> including it's location within an object graph.
/// </summary>
internal class ObjectReference
{
private readonly object @object;
private readonly string path;
private readonly bool? isComplexType;
private string[] pathElements;
public ObjectReference(object @object, string path, bool? isComplexType = null)
{
this.@object = @object;
this.path = path;
this.isComplexType = isComplexType;
}
/// <summary>
/// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
public override bool Equals(object obj)
{
if (!(obj is ObjectReference other))
{
return false;
}
return ReferenceEquals(@object, other.@object) && IsParentOf(other);
}
private string[] GetPathElements() => pathElements
?? (pathElements = path.ToLowerInvariant().Replace("][", "].[").Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries));
private bool IsParentOf(ObjectReference other)
{
string[] path = GetPathElements();
string[] otherPath = other.GetPathElements();
return (otherPath.Length > path.Length) && otherPath.Take(path.Length).SequenceEqual(path);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
/// <filterpriority>2</filterpriority>
public override int GetHashCode()
{
return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(@object);
}
public override string ToString()
{
return $"{{\"{path}\", {@object}}}";
}
public bool IsComplexType => isComplexType ?? (!(@object is null) && !@object.GetType().OverridesEquals());
}
}