-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSetupTestingBook.cs
95 lines (73 loc) · 3.49 KB
/
SetupTestingBook.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
namespace NetCash.Tests;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
/// <summary>
/// Provides URIs for pre-made books or automaticaly generate unique URIs for each testcase.
/// </summary>
public class SetupTestingBookAttribute : DataAttribute
{
/// <summary>
/// Copy from a premade book. Mutually-exclusive with UsePremade.
/// </summary>
public string CopyPremade { get; init; }
/// <summary>
/// Use a premade book (as readonly). Mutually-exclusive with UsePremade.
/// </summary>
public string UsePremade { get; init; }
protected virtual IEnumerable<object[]> GetAdditionalData(MethodInfo testMethod) => Enumerable.Empty<object[]>();
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
// Appended with a GUID because a single test method could be attributed with multiple InlineData or MemberData.
var autoBookName = $"{testMethod.DeclaringType.Name}~{testMethod.Name}~{Guid.NewGuid().ToString("n").Substring(0, 8)}";
var bookName = UsePremade ?? autoBookName;
object[] makeDataRow(string scheme)
{
var testingBook = new TestingBook(TestingBook.MakeUri(scheme, bookName));
if (UsePremade == null)
{
DbHelper.EnsureDatabase(scheme, testingBook.Uri.Path);
}
if (this.CopyPremade != null)
{
testingBook.BaseBookUri = TestingBook.MakeUri(scheme, this.CopyPremade);
// Due to this side-effect, we have to turn off "preEnumerateTheories" in xunit.runner.json to prevent repeated generation of test data.
using var _ = Book.OpenRead(testingBook.BaseBookUri).SaveAs(testingBook.Uri);
}
return new[] { testingBook };
}
var dataRowsForBackends = Config.SupportedBackends.Select(makeDataRow);
var additionalData = this.GetAdditionalData(testMethod);
if (!additionalData.Any())
return dataRowsForBackends;
else
return additionalData.SelectMany(additionalRow => dataRowsForBackends.Select(row => additionalRow.Concat(row).ToArray()));
}
}
public class SetupTestingBookWithInlineDataAttribute : SetupTestingBookAttribute
{
InlineDataAttribute innerAttribute;
public SetupTestingBookWithInlineDataAttribute(params object[] data) => this.innerAttribute = new InlineDataAttribute(data);
protected override IEnumerable<object[]> GetAdditionalData(MethodInfo testMethod) => this.innerAttribute.GetData(testMethod);
}
public class SetupTestingBookWithClassDataAttribute : SetupTestingBookAttribute
{
ClassDataAttribute innerAttribute;
public SetupTestingBookWithClassDataAttribute(Type @class) => this.innerAttribute = new ClassDataAttribute(@class);
protected override IEnumerable<object[]> GetAdditionalData(MethodInfo testMethod) => this.innerAttribute.GetData(testMethod);
}
public class SetupTestingBookWithMemberDataAttribute : SetupTestingBookAttribute
{
MemberDataAttribute innerAttribute;
public SetupTestingBookWithMemberDataAttribute(string memberName, params object[] parameters)
=> this.innerAttribute = new MemberDataAttribute(memberName, parameters);
protected override IEnumerable<object[]> GetAdditionalData(MethodInfo testMethod) => this.innerAttribute.GetData(testMethod);
}