< Summary

Information
Class: MyNet.Fakers.Text.TextFaker
Assembly: MyNet.Fakers
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Fakers/Text/TextFaker.cs
Tag: 342_31701152804
Line coverage
97%
Covered lines: 33
Uncovered lines: 1
Coverable lines: 34
Total lines: 103
Line coverage: 97%
Branch coverage
85%
Covered branches: 17
Total branches: 20
Branch coverage: 85%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
BuildUniqueWords()100%11100%
Word(...)75%44100%
Words(...)100%44100%
Sentence(...)50%22100%
Paragraph(...)100%44100%
Lorem(...)83.33%6690%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Fakers/Text/TextFaker.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TextFaker.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using System.Linq;
 10using System.Text;
 11using MyNet.Generator;
 12
 13namespace MyNet.Fakers.Text;
 14
 15/// <summary>
 16/// Current implementation of <see cref="ITextFaker"/>.
 17/// </summary>
 18public sealed class TextFaker(IRandomGenerator random) : ITextFaker
 19{
 20    private const string LoremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendis
 21
 322    private static readonly string[] LoremWords = BuildUniqueWords();
 23
 24    private static string[] BuildUniqueWords()
 25    {
 326        var cleaned = LoremIpsum
 327            .Replace(",", string.Empty, StringComparison.Ordinal)
 328            .Replace(".", string.Empty, StringComparison.Ordinal);
 29
 330        var words = cleaned.Split(' ', StringSplitOptions.RemoveEmptyEntries).Distinct(StringComparer.Ordinal).ToList();
 31
 332        return [..words];
 33    }
 34
 35    /// <inheritdoc />
 36    public string Word(int? length = null)
 37    {
 3938        var word = random.Item(LoremWords);
 39
 3940        return length is null ? word : word.Length > length ? word[..length.Value] : word;
 41    }
 42
 43    /// <inheritdoc />
 44    public string Words(int min, int max)
 45    {
 946        var count = random.Int(min, max);
 947        var builder = new StringBuilder();
 48
 9049        for (var i = 0; i < count; i++)
 50        {
 3651            if (i > 0)
 2752                builder.Append(' ');
 3653            builder.Append(Word());
 54        }
 55
 956        return builder.ToString();
 57    }
 58
 59    /// <inheritdoc />
 60    public string Sentence(int minWords = 4, int maxWords = 12)
 61    {
 962        var words = Words(minWords, maxWords);
 63
 964        return words.Length == 0 ? "." : char.ToUpper(words[0], CultureInfo.CurrentCulture) + words[1..] + ".";
 65    }
 66
 67    /// <inheritdoc />
 68    public string Paragraph(int minSentences = 2, int maxSentences = 5)
 69    {
 370        var count = random.Int(minSentences, maxSentences);
 371        var builder = new StringBuilder();
 72
 1873        for (var i = 0; i < count; i++)
 74        {
 675            if (i > 0)
 376                builder.Append(' ');
 677            builder.Append(Sentence());
 78        }
 79
 380        return builder.ToString();
 81    }
 82
 83    /// <inheritdoc />
 84    public string Lorem(int? wordCount = null)
 85    {
 386        if (!wordCount.HasValue)
 087            return LoremIpsum;
 88
 389        var words = LoremIpsum.Split(' ', StringSplitOptions.RemoveEmptyEntries);
 390        var builder = new StringBuilder();
 391        var count = Math.Min(wordCount.Value, words.Length);
 92
 3693        for (var i = 0; i < count; i++)
 94        {
 1595            if (i > 0)
 1296                builder.Append(' ');
 1597            builder.Append(words[i]);
 98        }
 99
 3100        return builder.ToString();
 101    }
 102}
 103