< Summary

Information
Class: Nabs.Tests.LoadEnumerableFromJsonDataAttribute<T>
Assembly: Nabs.Tests
File(s): /home/runner/work/Nabs/Nabs/src/Nabs.Tests/LoadEnumerableFromJsonDataAttribute.cs
Tag: 90_14636759620
Line coverage
100%
Covered lines: 22
Uncovered lines: 0
Coverable lines: 22
Total lines: 43
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetData()100%44100%

File(s)

/home/runner/work/Nabs/Nabs/src/Nabs.Tests/LoadEnumerableFromJsonDataAttribute.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace Nabs.Tests;
 4
 5[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
 6public class LoadEnumerableFromJsonDataAttribute<T> : DataAttribute
 7    where T : notnull, new()
 8{
 9    private readonly Type _relativeAssemblyType;
 10    private readonly string _resourceFilePathEndsWith;
 11    private readonly EmbeddedResourceLoader _resourceLoader;
 12    private readonly JsonSerializerOptions _jsonSerializerOptions;
 13
 214    public LoadEnumerableFromJsonDataAttribute(Type relativeAssemblyType, string resourceFilePathEndsWith)
 15    {
 216        _relativeAssemblyType = relativeAssemblyType;
 217        _resourceFilePathEndsWith = resourceFilePathEndsWith;
 218        _resourceLoader = new EmbeddedResourceLoader(_relativeAssemblyType);
 219        _jsonSerializerOptions = GlobalSettings.JsonSerializerOptions;
 220    }
 21
 22    public override IEnumerable<object[]> GetData(MethodInfo testMethod)
 23    {
 424        var items = _resourceLoader.GetResourceStreamContent(x => x.Path.EndsWith(_resourceFilePathEndsWith))
 225            .Match(
 226                content =>
 227                {
 228                    var streamReader = new StreamReader(content);
 229                    var records = JsonSerializer.Deserialize<T[]>(streamReader.ReadToEnd(), _jsonSerializerOptions)!;
 430                    var data = records.Select(x => new object[] { x });
 231                    return data;
 232                },
 233                exception =>
 234                {
 135                    throw exception;
 236                });
 37
 638        foreach (var item in items)
 39        {
 240            yield return item;
 41        }
 242    }
 43}