-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDataAttribute.cs
42 lines (34 loc) · 1.02 KB
/
FileDataAttribute.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
using System.Reflection;
using Xunit.Sdk;
namespace AdventOfCode2022;
internal enum FileContents
{
SingleString,
StringPerLine,
}
internal class FileDataAttribute : DataAttribute
{
private readonly string _filePath;
private readonly FileContents _contents;
private readonly object[] _args;
public FileDataAttribute(string filePath, params object[] args) : this(filePath, FileContents.SingleString, args)
{
}
public FileDataAttribute(string filePath, FileContents contents, params object[] args)
{
_filePath = filePath;
_contents = contents;
_args = args;
}
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
var data = ReadFile();
yield return _args.Prepend(data).ToArray();
}
private object ReadFile() => _contents switch
{
FileContents.SingleString => File.ReadAllText(_filePath),
FileContents.StringPerLine => File.ReadLines(_filePath),
_ => throw new ArgumentException(),
};
}