bchavez / Bogus

:card_index: A simple fake data generator for C#, F#, and VB.NET. Based on and ported from the famed faker.js.
Other
8.62k stars 491 forks source link

I using Random.String2 correct? #470

Closed raffaelmiranda closed 1 year ago

raffaelmiranda commented 1 year ago

Version Information

Software Version(s)
Bogus NuGet Package 34.0.3
.NET Core? 3.1
.NET Full Framework? No
Windows OS? Windows 10
Linux OS? No
Visual Studio? 2019

What locale are you using with Bogus?

Brazil

What's the problem?

When I use f.Random.String2(25), even if I set pt_BR in locale, it returns data that is impossible to read for another encoding or for another language. Maybe there was a missing setting in Bogus?

What possible solutions have you considered?

I considered only what is shown in the code example

Do you have sample code to show what you're trying to do?

public class DadosPendenciaReturn
    {
        public string NumeroLancamento { get; set; }
        public string CodigoDocumento { get; set; }
        public string CodigoCliente { get; set; }
        public string CpfCnpj { get; set; }
        public string PCpfCnpj { get; set; }
        public string TipoDocumento { get; set; }
        public string Status { get; set; }
        public string DataLancamentoP { get; set; }
        public string DataInicio { get; set; }
        public string DataVencimento { get; set; }
        public string Valor { get; set; }
        public string Moeda { get; set; }
        public string Descricao { get; set; }
        public string TipoOperacaoDescricao { get; set; }
        public string NumeroContrato { get; set; }
        public string Produto { get; set; }
        public string NumeroDocumento { get; set; }
        public string Codigoproduto { get; set; }
        public string DocumentoReferencia { get; set; }
        public string Empresa { get; set; }
        public string StatusCliente { get; set; }
        public string Numero { get; set; }
        public string Cnpj { get; set; }
        public string CodigoGerente { get; set; }
        public string Gerente { get; set; }
        public string Nome { get; set; }
        public string Categoria { get; set; }
        public string DtVencimento { get; set; }
        public string DocumentoEntregue { get; set; }
        public string TempoPendencia { get; set; }
    }
var dataFake = new Faker<DadosPendenciaReturn>("pt_BR");
dataFake.RuleFor(x => x.NumeroLancamento, f => f.Random.String2(25));
dataFake.RuleFor(x => x.CodigoDocumento, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.CodigoCliente, f => f.Random.Number(0, 10000).ToString().PadLeft(4, '0'));
dataFake.RuleFor(x => x.CpfCnpj, f => f.Company.Cnpj(false));
dataFake.RuleFor(x => x.PCpfCnpj, f => f.Company.Cnpj(false));
dataFake.RuleFor(x => x.TipoDocumento, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Status, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.DataLancamentoP, f => f.Date.Recent(100).ToString("dd/MM/yyyy"));
dataFake.RuleFor(x => x.DataInicio, f => f.Date.Recent(100).ToString("dd/MM/yyyy"));
dataFake.RuleFor(x => x.DataVencimento, f => f.Date.Recent(100).ToString("dd/MM/yyyy"));
dataFake.RuleFor(x => x.Valor, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Moeda, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Descricao, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.TipoOperacaoDescricao, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.NumeroContrato, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Produto, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.NumeroDocumento, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Codigoproduto, f => f.Random.Int(0, 10000).ToString().PadLeft(4, '0'));
dataFake.RuleFor(x => x.DocumentoReferencia, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Empresa, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.StatusCliente, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Numero, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.Cnpj, f => f.Company.Cnpj(false));
dataFake.RuleFor(x => x.CodigoGerente, f => f.Random.Int(0, 10000).ToString().PadLeft(4, '0'));
dataFake.RuleFor(x => x.Gerente, f => f.Person.FullName);
dataFake.RuleFor(x => x.Nome, f => f.Person.FullName);
dataFake.RuleFor(x => x.Categoria, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.DtVencimento, f => f.Date.Recent(100).ToString("dd/MM/yyyy"));
dataFake.RuleFor(x => x.DocumentoEntregue, f => f.Random.String(5, 10));
dataFake.RuleFor(x => x.TempoPendencia, f => f.Random.String(5, 10));

var response = dataFake.Generate(5);

Result: image

(Please be complete. Provide all code necessary to run your example in LINQPad.) (The more complete code examples are, the more accurate answers will be.) (https://www.linqpad.net)

bchavez commented 1 year ago

Hi @raffaelmiranda ,

First,

Software Version(s)
Bogus NuGet Package 34.0.3

At the time of this writing, Version 34.0.3 does not exist:

Second, your screenshot shows you are mostly using Random.String() not Random.String2().

To understand the difference, you'll need to read the implementation and XML documentation for String and String2 in how they differ shown below:

.String()

https://github.com/bchavez/Bogus/blob/ed55eae18836f79e7819e2111295ddfbdf35cb36/Source/Bogus/Randomizer.cs#L332-L346

.String2()

https://github.com/bchavez/Bogus/blob/ed55eae18836f79e7819e2111295ddfbdf35cb36/Source/Bogus/Randomizer.cs#L364-L381

Notice, that both implementations do not work with any locale. They are mostly a-z and char.MinValue - char.MaxValue methods.

Hope that helps.