-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExcelTestResultMatcher.cs
More file actions
84 lines (72 loc) · 3.89 KB
/
ExcelTestResultMatcher.cs
File metadata and controls
84 lines (72 loc) · 3.89 KB
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
using SpecSync.Parsing;
using SpecSync.PublishTestResults;
using SpecSync.PublishTestResults.Matchers;
namespace SpecSync.Plugin.ExcelTestResults;
public class ExcelTestResultMatcher(ExcelResultParameters excelResultParameters) : ITestRunnerResultMatcher
{
public virtual string ServiceDescription => "Excel Test Result";
public virtual bool CanProcess(TestRunnerResultMatcherArgs args)
=> args.TestFrameworkIdentifier.Equals(ExcelTestResultLoader.FormatSpecifier, StringComparison.InvariantCultureIgnoreCase);
public MatchResultSelector GetLocalTestCaseResultSelector(ILocalTestCase localTestCase,
ISourceDocument localTestCaseContainer, TestRunnerResultMatcherArgs args)
{
var scenarioName = localTestCase.Name;
var featureName = localTestCaseContainer.Name;
var featureFileName = Path.GetFileName(localTestCaseContainer.SourceReference.ProjectRelativePath);
var testCaseId = localTestCase.IdLink!.Id.GetNumericId();
string? IdCellValueConverter(string cellValue)
{
return ExcelTestResultLoader.GetTestCaseLink(cellValue, args.TagServices)?.Id.ToString();
}
return
CombineSelectorsOr(
CreateColumnMatch(excelResultParameters.TestCaseIdColumnName, testCaseId.ToString(), IdCellValueConverter, resultIfNotSpecified: false),
CombineSelectorsAnd(
CreateColumnMatch(excelResultParameters.FeatureFileColumnName, featureFileName),
CreateColumnMatch(excelResultParameters.FeatureColumnName, featureName),
CreateColumnMatch(excelResultParameters.ScenarioColumnName, scenarioName),
CreateColumnMatch(excelResultParameters.TestCaseIdColumnName, testCaseId.ToString(), IdCellValueConverter)
));
}
private MatchResultSelector CreateColumnMatch(string columnName, string value, Func<string, string?>? cellValueConverter = null, bool resultIfNotSpecified = true)
{
return new MatchResultSelector($"[{columnName}] is '{value}' (if specified)",
td => EqualsToStringIfSpecified(td, columnName, value, cellValueConverter, resultIfNotSpecified));
}
private MatchResultSelector CombineSelectorsAnd(params MatchResultSelector[] selectors)
{
var validSelectors = selectors.Where(s => s != null).ToArray();
return new MatchResultSelector(
string.Join(" and ", validSelectors.Select(s => s.DiagMessage)),
td => validSelectors.All(s => s.Func(td))
);
}
private MatchResultSelector CombineSelectorsOr(params MatchResultSelector[] selectors)
{
var validSelectors = selectors.Where(s => s != null).ToArray();
return new MatchResultSelector(
string.Join(" or ", validSelectors.Select(s => $"({s.DiagMessage})")),
td => validSelectors.Any(s => s.Func(td))
);
}
private bool EqualsToStringIfSpecified(LocalTestResult localTestResult, string columnName, string value, Func<string, string?>? cellValueConverter, bool resultIfNotSpecified)
{
var cellValue = GetCellValue<string>(localTestResult, columnName);
if (cellValueConverter != null)
cellValue = cellValueConverter(cellValue);
if (string.IsNullOrEmpty(cellValue))
return resultIfNotSpecified;
return value.Equals(cellValue, StringComparison.OrdinalIgnoreCase);
}
private T GetCellValue<T>(LocalTestResult localTestResult, string columnName)
{
var objValue = localTestResult.GetProperty<object>(columnName);
if (objValue is T value)
return value;
return (T)Convert.ChangeType(objValue, typeof(T));
}
public IDictionary<string, string>? GetInvocationArguments(LocalTestResult testResult, ILocalTestCase localTestCase, ISourceDocument sourceDocument, TestRunnerResultMatcherArgs args)
{
return null;
}
}