Skip to content

Commit

Permalink
Fix for issue #534: Gender-digit is incorrect for Swedish Personnumme…
Browse files Browse the repository at this point in the history
…r generation (#535)

* Corrected gender handling in Swedish Personnummer

Second last digit should be even for Female
Second last digit should be odd for Male

* Reverted change in comment
  • Loading branch information
LarsBergqvist authored Mar 2, 2024
1 parent 55bde49 commit 3c21c0e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions Source/Bogus.Tests/ExtensionTests/SwedishExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void samordningsnummer_should_contain_offset_date_of_birth()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void when_person_is_male_second_last_number_is_even(bool isSamordningsnummer)
public void when_person_is_male_second_last_number_is_odd(bool isSamordningsnummer)
{
var f = new Faker("sv");
var person = f.Person;
Expand All @@ -71,15 +71,15 @@ public void when_person_is_male_second_last_number_is_even(bool isSamordningsnum
var secondLast = int.Parse(identificationNumber.Substring(identificationNumber.Length - 2, 1));

secondLast.Should()
.Match(x => x % 2 == 0)
.Match(x => x % 2 == 1)
.And.BeLessThan(10)
.And.BeGreaterThan(0);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void when_person_is_female_second_last_number_is_odd(bool isSamordningsnummer)
public void when_person_is_female_second_last_number_is_even(bool isSamordningsnummer)
{
var f = new Faker("sv");
var person = f.Person;
Expand All @@ -90,7 +90,7 @@ public void when_person_is_female_second_last_number_is_odd(bool isSamordningsnu
var secondLast = int.Parse(identificationNumber.Substring(identificationNumber.Length - 2, 1));

secondLast.Should()
.Match(x => x % 2 == 1)
.Match(x => x % 2 == 0)
.And.BeLessThan(10)
.And.BeGreaterThan(0);
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Bogus/Extensions/Sweden/ExtensionsForSweden.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static string Personnummer(this Person person)
/// </summary>
/// <remarks>
/// Coordination numbers enable Swedish public authorities and other organizations with a public function
/// to identify people who are not currently – and have never been – registered at an address in Sweden.
/// to identify people who are not currently – and have never been – registered at an address in Sweden.
/// </remarks>
public static string Samordningsnummer(this Person person)
{
Expand Down Expand Up @@ -90,9 +90,9 @@ private static string GenerateIndividualNumber(Randomizer r, Gender gender, Date
private static int GetGenderNumber(Randomizer r, Gender gender)
{
if( gender is Gender.Male )
return r.Even(1, 9);
if( gender is Gender.Female )
return r.Odd(1, 9);
if( gender is Gender.Female )
return r.Even(1, 9);
throw new ArgumentOutOfRangeException(nameof(gender), gender, "Gender not handled.");
}

Expand Down

0 comments on commit 3c21c0e

Please sign in to comment.