-
-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added new Chilean RUT generator #487
Open
acidrod
wants to merge
7
commits into
bchavez:master
Choose a base branch
from
acidrod:feature/UnitTestingForChileanRUT
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c4e462
Added the full extension for Chilean RUT for Person and Company.
acidrod 03d3bfd
Fixed ArgumentOutOfRangeException and Random errors by adding a using…
acidrod 2203317
Fixed the return for both extension methods.
acidrod 025cd00
Added unit tests for the ExtensionsForChile and set the generator to …
acidrod 3715b64
Added the full extension for Chilean RUT for Person and Company, incl…
acidrod e84814a
Merge branch 'feature/UnitTestingForChileanRUT' of https://github.com…
acidrod 1de4cf6
Removed the [Theory] and replaced with [Fact] Trying to figure out wh…
acidrod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
Source/Bogus.Tests/ExtensionTests/ChileanRutExtensionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Bogus.Extensions.Chile; | ||
using FluentAssertions; | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Xunit; | ||
|
||
namespace Bogus.Tests.ExtensionTests | ||
{ | ||
public class ChileanRutExtensionTest | ||
{ | ||
|
||
[Fact] | ||
public void ChileanRutExtension_Should_BeValid_When_UsingDotFormat() | ||
{ | ||
// Arrange | ||
var faker = new Faker<Person>() | ||
.RuleFor(p => p.Rut, f => f.Person.Rut(true)); | ||
|
||
// Act | ||
var person = faker.Generate(); | ||
|
||
// Assert | ||
InternalValidationRutHelper.IsRutValid(person.Rut).Should().Be(true); | ||
} | ||
|
||
[Fact] | ||
public void ChileanRutExtension_Should_BeValid_When_NotUsingDotFormat() | ||
{ | ||
// Arrange | ||
var faker = new Faker<Person>() | ||
.RuleFor(p => p.Rut, f => f.Person.Rut(false)); | ||
|
||
// Act | ||
var person = faker.Generate(); | ||
|
||
// Assert | ||
InternalValidationRutHelper.IsRutValid(person.Rut).Should().Be(true); | ||
} | ||
|
||
private class Person | ||
{ | ||
internal string Rut { get; set; } = ""; | ||
} | ||
} | ||
|
||
internal static class InternalValidationRutHelper | ||
{ | ||
internal static bool IsRutValid(string rut) | ||
{ | ||
if (string.IsNullOrEmpty(rut)) | ||
return false; | ||
|
||
rut = rut.Replace(".", ""); | ||
|
||
var expresion = new Regex("^([0-9]+-[0-9K])$"); | ||
string dv = rut.Substring(rut.Length - 1, 1); | ||
if (!expresion.IsMatch(rut)) | ||
{ | ||
return false; | ||
} | ||
char[] charCorte = { '-' }; | ||
string[] rutTemp = rut.Split(charCorte); | ||
if (dv != Digito(int.Parse(rutTemp[0]))) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private static string Digito(int rut) | ||
{ | ||
if (rut < 1000000 || rut > 99999999) | ||
throw new ArgumentOutOfRangeException($"The provided integer {nameof(rut)} is outside of the range between 1000000 and 99999999"); | ||
|
||
int suma = 0; | ||
int multiplicador = 1; | ||
|
||
while (rut != 0) | ||
{ | ||
multiplicador++; | ||
if (multiplicador == 8) | ||
multiplicador = 2; | ||
suma += (rut % 10) * multiplicador; | ||
rut /= 10; | ||
} | ||
|
||
suma = 11 - (suma % 11); | ||
|
||
if (suma == 11) | ||
{ | ||
return "0"; | ||
} | ||
else if (suma == 10) | ||
{ | ||
return "K"; | ||
} | ||
else | ||
{ | ||
return suma.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using Bogus.DataSets; | ||
|
||
namespace Bogus.Extensions.Chile | ||
{ | ||
/// <summary> | ||
/// API extensions specific for a geographical location. | ||
/// </summary> | ||
public static class ExtensionsForChile | ||
{ | ||
/// <summary> | ||
/// Generates a valid Chilean RUT for a Person (Rol Unico Tributario) | ||
/// </summary> | ||
/// <param name="dotFormat">Use the thousands separator for the Chilean RUT (11.111.111-1)</param> | ||
/// <returns>A string representation for a valid Chilean RUT (Rol Unico Tributario)</returns> | ||
public static string Rut(this Person person, bool dotFormat = false) | ||
{ | ||
return GenerateChileanId(dotFormat); | ||
} | ||
|
||
/// <summary> | ||
/// Generates a valid Chilean RUT for a Company (Rol Unico Tributario) | ||
/// </summary> | ||
/// <param name="dotFormat">Use the thousands separator for the Chilean RUT (11.111.111-1)</param> | ||
/// <returns>A string representation for a valid Chilean RUT (Rol Unico Tributario)</returns> | ||
|
||
public static string Rut(this Company company, bool dotFormat = true) | ||
{ | ||
return GenerateChileanId(dotFormat); | ||
} | ||
|
||
/// <summary> | ||
/// A general Chilean ID generator | ||
/// </summary> | ||
/// <param name="dotFormat">Use the thousands separator for the Chilean RUT (11.111.111-1)</param> | ||
/// <returns></returns> | ||
private static string GenerateChileanId(bool dotFormat = true) | ||
{ | ||
Random rnd = new(); | ||
int num = rnd.Next(1000000, 99999999); | ||
|
||
string dig = Digito(num); | ||
|
||
if (dotFormat) | ||
{ | ||
var rut = string.Format("{0:0,0}", num); | ||
return $"{rut}-{dig}"; | ||
} | ||
|
||
return $"{num}-{dig}"; | ||
} | ||
|
||
/// <summary> | ||
/// Algorithm to generate a verification digit based on an integer between 1000000 and 99999999 | ||
/// </summary> | ||
/// <param name="rut">Represents a full valid RUT number</param> | ||
/// <returns>A string representing a number that validates the provided number</returns> | ||
private static string Digito(int rut) | ||
{ | ||
if (rut < 1000000 || rut > 99999999) | ||
throw new ArgumentOutOfRangeException($"The provided integer is outside of the range between 1000000 and 99999999"); | ||
|
||
int suma = 0; | ||
int multiplicador = 1; | ||
|
||
while (rut != 0) | ||
{ | ||
multiplicador++; | ||
if (multiplicador == 8) | ||
multiplicador = 2; | ||
suma += (rut % 10) * multiplicador; | ||
rut /= 10; | ||
} | ||
|
||
suma = 11 - (suma % 11); | ||
|
||
if (suma == 11) | ||
{ | ||
return "0"; | ||
} | ||
else if (suma == 10) | ||
{ | ||
return "K"; | ||
} | ||
else | ||
{ | ||
return suma.ToString(); | ||
} | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't call
new Random()
in Bogus because it's not deterministic. We'll need to source thernd
fromPerson.Random
to get the randomizer, and then ask for thernd.Next()
.