forked from fluentassertions/fluentassertions
/
SelectMemberByPathSelectionRule.cs
62 lines (50 loc) · 1.93 KB
/
SelectMemberByPathSelectionRule.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace FluentAssertions.Equivalency.Selection
{
internal abstract class SelectMemberByPathSelectionRule : IMemberSelectionRule
{
private string selectedPath;
protected SelectMemberByPathSelectionRule(string selectedPath)
{
this.selectedPath = selectedPath;
}
public virtual bool IncludesMembers => false;
protected void SetSelectedPath(string path)
{
this.selectedPath = path;
}
public IEnumerable<IMember> SelectMembers(INode currentNode, IEnumerable<IMember> selectedMembers,
MemberSelectionContext context)
{
string currentPath = currentNode.PathAndName;
// If we're part of a collection comparison, the selected path will not include an index,
// so we need to remove it from the current node as well.
if (!ContainsIndexingQualifiers(selectedPath))
{
currentPath = RemoveIndexQualifiers(currentPath);
}
var members = selectedMembers.ToList();
AddOrRemoveMembersFrom(members, currentNode, currentPath, context);
return members;
}
protected abstract void AddOrRemoveMembersFrom(List<IMember> selectedMembers,
INode parent, string parentPath,
MemberSelectionContext context);
private static bool ContainsIndexingQualifiers(string path)
{
return path.Contains("[", StringComparison.Ordinal) && path.Contains("]", StringComparison.Ordinal);
}
private static string RemoveIndexQualifiers(string path)
{
Match match = new Regex(@"^\[\d+]").Match(path);
if (match.Success)
{
path = path.Substring(match.Length);
}
return path;
}
}
}