| | | 1 | | namespace Nabs.Tests; |
| | | 2 | | |
| | | 3 | | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] |
| | | 4 | | public class LoadFromCsvDataAttribute<T> : DataAttribute |
| | | 5 | | where T : class, new() |
| | | 6 | | { |
| | | 7 | | private readonly Type _relativeAssemblyType; |
| | | 8 | | private readonly string _resourceFilePathEndsWith; |
| | | 9 | | private readonly CsvConfiguration _csvConfiguration; |
| | | 10 | | private readonly EmbeddedResourceLoader _resourceLoader; |
| | | 11 | | |
| | 1 | 12 | | public LoadFromCsvDataAttribute(Type relativeAssemblyType, string resourceFilePathEndsWith) |
| | | 13 | | { |
| | 1 | 14 | | _relativeAssemblyType = relativeAssemblyType; |
| | 1 | 15 | | _resourceFilePathEndsWith = resourceFilePathEndsWith; |
| | 1 | 16 | | _resourceLoader = new EmbeddedResourceLoader(_relativeAssemblyType); |
| | 1 | 17 | | _csvConfiguration = GlobalSettings.CsvConfiguration; |
| | 1 | 18 | | } |
| | | 19 | | |
| | | 20 | | public override IEnumerable<object[]> GetData(MethodInfo testMethod) |
| | | 21 | | { |
| | 2 | 22 | | var items = _resourceLoader.GetResourceStreamContent(x => x.Path.EndsWith(_resourceFilePathEndsWith)) |
| | 1 | 23 | | .Match( |
| | 1 | 24 | | content => |
| | 1 | 25 | | { |
| | 1 | 26 | | var csvReader = new CsvReader(new StreamReader(content), _csvConfiguration); |
| | 1 | 27 | | var records = csvReader.GetRecords<T>(); |
| | 2 | 28 | | var data = records.Select(x => new object[] { x }); |
| | 1 | 29 | | return data; |
| | 1 | 30 | | }, |
| | 1 | 31 | | exception => |
| | 1 | 32 | | { |
| | 1 | 33 | | throw exception; |
| | 1 | 34 | | }); |
| | | 35 | | |
| | 3 | 36 | | foreach (var item in items) |
| | | 37 | | { |
| | 1 | 38 | | yield return item; |
| | | 39 | | } |
| | 1 | 40 | | } |
| | | 41 | | } |