< Summary

Information
Class: ArturRios.Data.Export.Exporters.TxtExporter<T>
Assembly: ArturRios.Data.Export
File(s): /home/runner/work/dotnet-data/dotnet-data/src/ArturRios.Data.Export/Exporters/TxtExporter.cs
Line coverage
91%
Covered lines: 11
Uncovered lines: 1
Coverable lines: 12
Total lines: 44
Line coverage: 91.6%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage
80%
Covered methods: 4
Fully covered methods: 4
Total methods: 5
Method coverage: 80%
Full method coverage: 80%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteCoreAsync(...)75%44100%
WriteAsync(...)100%11100%
WriteToFileAsync(...)100%210%
WriteLinesAsync()100%22100%

File(s)

/home/runner/work/dotnet-data/dotnet-data/src/ArturRios.Data.Export/Exporters/TxtExporter.cs

#LineLine coverage
 1using ArturRios.Data.Export.Configuration;
 2using ArturRios.Output;
 3using Microsoft.Extensions.Logging;
 4
 5namespace ArturRios.Data.Export.Exporters;
 6
 7/// <summary>Writes one line per record: default <see cref="object.ToString" />, or a custom selector.</summary>
 8/// <typeparam name="T">The record type.</typeparam>
 9/// <param name="options">TXT options.</param>
 10/// <param name="logger">Optional logger; see <see cref="ExporterBase{T}" />.</param>
 11public class TxtExporter<T>(TxtOptions options, ILogger<TxtExporter<T>>? logger = null)
 1212    : ExporterBase<T>(logger) where T : class
 13{
 14    /// <inheritdoc />
 15    protected override Task WriteCoreAsync(IEnumerable<T> data, Stream destination, CancellationToken ct) =>
 616        WriteLinesAsync(data, destination, item => item?.ToString() ?? string.Empty, ct);
 17
 18    /// <summary>Writes each record on its own line using <paramref name="lineSelector" />.</summary>
 19    public Task<ProcessOutput> WriteAsync(IEnumerable<T> data, Stream destination, Func<T, string> lineSelector,
 20        CancellationToken ct = default) =>
 621        GuardedWriteAsync(data, destination, stream => WriteLinesAsync(data, stream, lineSelector, ct));
 22
 23    /// <summary>Writes each record on its own line to a file using <paramref name="lineSelector" />.</summary>
 24    public Task<ProcessOutput> WriteToFileAsync(IEnumerable<T> data, string path, Func<T, string> lineSelector,
 25        CancellationToken ct = default) =>
 026        GuardedFileAsync(data, path, stream => WriteLinesAsync(data, stream, lineSelector, ct));
 27
 28    private async Task WriteLinesAsync(IEnumerable<T> data, Stream destination, Func<T, string> lineSelector,
 29        CancellationToken ct)
 30    {
 631        var writer = new StreamWriter(destination, options.Encoding, leaveOpen: true);
 32
 633        await using var writerScope = writer.ConfigureAwait(false);
 34
 2435        foreach (var item in data)
 36        {
 637            ct.ThrowIfCancellationRequested();
 638            await writer.WriteAsync(lineSelector(item)).ConfigureAwait(false);
 639            await writer.WriteAsync(options.NewLine).ConfigureAwait(false);
 40        }
 41
 642        await writer.FlushAsync(ct).ConfigureAwait(false);
 643    }
 44}