< Summary

Information
Class: MyNet.Fakers.Static.Faker
Assembly: MyNet.Fakers
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Fakers/Static/Faker.cs
Tag: 342_31701152804
Line coverage
0%
Covered lines: 0
Uncovered lines: 40
Coverable lines: 40
Total lines: 143
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
Configure(...)0%620%
get_Names()100%210%
get_Phones()100%210%
get_Mails()100%210%
get_Countries()100%210%
get_Addresses()100%210%
get_Streets()100%210%
get_Identities()100%210%
get_Texts()100%210%
get_Domains()100%210%
get_Colors()100%210%
CreateDefault()100%210%
Get(...)100%210%
TryGet(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Fakers/Static/Faker.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="Faker.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Diagnostics.CodeAnalysis;
 9using System.Globalization;
 10using System.Threading;
 11using MyNet.Fakers.Contacts;
 12using MyNet.Fakers.Geography;
 13using MyNet.Fakers.Identity;
 14using MyNet.Fakers.Internet;
 15using MyNet.Fakers.Media;
 16using MyNet.Fakers.Text;
 17using MyNet.Generator.Facade;
 18using MyNet.Globalization.Localization.Providers;
 19using MyNet.Text.Randomize;
 20
 21namespace MyNet.Fakers.Static;
 22
 23/// <summary>
 24/// Provides a static class for generating fake data across various categories such as names, phones, mails, countries, 
 25/// </summary>
 26public static class Faker
 27{
 028    private static IFakeDataGenerator _service = CreateDefault();
 29
 30    private static int _configured;
 31
 32    /// <summary>
 33    /// Configures the <see cref="Faker"/> class to use a custom implementation of the <see cref="IFakeDataGenerator"/>.
 34    /// </summary>
 35    /// <param name="generator">The custom implementation of the <see cref="IFakeDataGenerator"/> to be used by the <see
 36    /// <exception cref="ArgumentNullException">Thrown if the provided <paramref name="generator"/> is null.</exception>
 37    /// <exception cref="InvalidOperationException">Thrown if the <see cref="Faker"/> class has already been configured.
 38    public static void Configure(IFakeDataGenerator generator)
 39    {
 040        ArgumentNullException.ThrowIfNull(generator);
 41
 042        if (Interlocked.Exchange(ref _configured, 1) == 1)
 043            throw new InvalidOperationException("Faker has already been configured.");
 44
 045        _service = generator;
 046    }
 47
 48    /// <summary>
 49    /// Gets the current instance of the <see cref="IFakeDataGenerator"/> being used by the <see cref="Faker"/> class. T
 50    /// </summary>
 051    public static INameFaker Names => _service.Names;
 52
 53    /// <summary>
 54    /// Gets the current instance of the <see cref="IPhoneFaker"/> being used by the <see cref="Faker"/> class. This ins
 55    /// </summary>
 056    public static IPhoneFaker Phones => _service.Phones;
 57
 58    /// <summary>
 59    /// Gets the current instance of the <see cref="IMailFaker"/> being used by the <see cref="Faker"/> class. This inst
 60    /// </summary>
 061    public static IMailFaker Mails => _service.Mails;
 62
 63    /// <summary>
 64    /// Gets the current instance of the <see cref="ICountryFaker"/> being used by the <see cref="Faker"/> class. This i
 65    /// </summary>
 066    public static ICountryFaker Countries => _service.Countries;
 67
 68    /// <summary>
 69    /// Gets the current instance of the <see cref="IAddressFaker"/> being used by the <see cref="Faker"/> class. This i
 70    /// </summary>
 071    public static IAddressFaker Addresses => _service.Addresses;
 72
 73    /// <summary>
 74    /// Gets the current instance of the <see cref="IStreetFaker"/> being used by the <see cref="Faker"/> class. This in
 75    /// </summary>
 076    public static IStreetFaker Streets => _service.Streets;
 77
 78    /// <summary>
 79    /// Gets the current instance of the <see cref="IIdentityFaker"/> being used by the <see cref="Faker"/> class. This 
 80    /// </summary>
 081    public static IIdentityFaker Identities => _service.Identities;
 82
 83    /// <summary>
 84    /// Gets the current instance of the <see cref="ITextFaker"/> being used by the <see cref="Faker"/> class. This inst
 85    /// </summary>
 086    public static ITextFaker Texts => _service.Texts;
 87
 88    /// <summary>
 89    /// Gets the current instance of the <see cref="IDomainFaker"/> being used by the <see cref="Faker"/> class. This in
 90    /// </summary>
 091    public static IDomainFaker Domains => _service.Domains;
 92
 93    /// <summary>
 94    /// Gets the current instance of the <see cref="IColorFaker"/> being used by the <see cref="Faker"/> class. This ins
 95    /// </summary>
 096    public static IColorFaker Colors => _service.Colors;
 97
 98    private static FakeDataGenerator CreateDefault()
 99    {
 0100        var nameFaker = new NameFaker(RandomGenerator.Current, new CultureScopedServiceSource<INameFakerProvider>(ResxNa
 0101        var phoneFaker = new PhoneFaker(TextRandomGenerator.Current, RandomGenerator.Current, new CultureScopedServiceSo
 0102        var countryFaker = new CountryFaker(RandomGenerator.Current);
 0103        var textFaker = new TextFaker(RandomGenerator.Current);
 0104        var streetFaker = new StreetFaker(RandomGenerator.Current, new CultureScopedServiceSource<IAddressFakerProvider>
 0105        var addressFaker = new AddressFaker(TextRandomGenerator.Current, RandomGenerator.Current, new CultureScopedServi
 0106        var identityFaker = new IdentityFaker(RandomGenerator.Current, textFaker);
 0107        var domainFaker = new DomainFaker(RandomGenerator.Current, new CultureScopedServiceSource<IDomainFakerProvider>(
 0108        var colorFaker = new ColorFaker(RandomGenerator.Current);
 0109        var mailFaker = new MailFaker(RandomGenerator.Current, identityFaker, domainFaker);
 110
 0111        return new FakeDataGenerator(
 0112            nameFaker,
 0113            phoneFaker,
 0114            mailFaker,
 0115            countryFaker,
 0116            addressFaker,
 0117            streetFaker,
 0118            identityFaker,
 0119            textFaker,
 0120            domainFaker,
 0121            colorFaker);
 122    }
 123
 124    /// <summary>
 125    /// A simple implementation of the <see cref="ICultureScopedServiceSource{TService}"/> interface that always returns
 126    /// </summary>
 127    /// <param name="instance">The instance of the service to be returned.</param>
 128    /// <typeparam name="TService">The type of the service.</typeparam>
 129    private sealed class CultureScopedServiceSource<TService>(TService instance) : ICultureScopedServiceSource<TService>
 130        where TService : class, ICultureScoped
 131    {
 132        /// <inheritdoc/>
 0133        public TService Get(CultureInfo? culture = null) => instance;
 134
 135        /// <inheritdoc/>
 136        public bool TryGet(CultureInfo? culture, [NotNullWhen(true)] out TService? service)
 137        {
 0138            service = instance;
 0139            return true;
 140        }
 141    }
 142}
 143