Skip to content

Commit

Permalink
Locale data parity with faker.js at https://github.com/Marak/faker.js…
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez authored Sep 14, 2020
1 parent 8cbbef6 commit 380ff08
Show file tree
Hide file tree
Showing 70 changed files with 19,887 additions and 1,787 deletions.
22 changes: 22 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
## v31.0.1
Release Date: 2020-09-13

* Locale data parity with faker-js@91dc8a3.
* Added `Music` dataset with `.Genre()` method.
* Added `Date.TimeZoneString()`.
* Added new Finnish `fi` locale.
* Added new Hrvatski `hr` locale.
* `cz` locales updated.
* `en` locales updated.
* `en_IE` locales updated.
* `en_IND` locales updated.
* `fa` locales updated.
* `fr` locales updated.
* `ja` locales updated.
* `pl` locales updated.
* `pt_BR` locales updated.
* `ru` locales updated.
* `sv` locales updated.
* `vi` locales updated.
* `zh_CN` locales updated.

## v30.0.4
Release Date: 2020-08-15

Expand Down
45 changes: 23 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,31 @@ public void With_Korean_Locale()

| Locale Code | Language | | Locale Code | Language |
|:--------------:|:-----------------------:|-|:--------------:|:------------------------:|
|`af_ZA `|South Africa (Afrikaans) ||`fr_CH `|French (Switzerland) |
|`af_ZA `|Afrikaans ||`fr_CH `|French (Switzerland) |
|`ar `|Arabic ||`ge `|Georgian |
|`az `|Azerbaijani ||`id_ID `|Indonesia |
|`cz `|Czech ||`it `|Italian |
|`de `|German ||`ja `|Japanese |
|`de_AT `|German (Austria) ||`ko `|Korean |
|`de_CH `|German (Switzerland) ||`lv `|Latvian |
|`el `|Greek ||`nb_NO `|Norwegian |
|`en `|English ||`nep `|Nepalese |
|`en_AU `|Australia (English) ||`nl `|Dutch |
|`en_au_ocker `|Australia Ocker (English) ||`nl_BE `|Dutch (Belgium) |
|`en_BORK `|Bork (English) ||`pl `|Polish |
|`en_CA `|Canada (English) ||`pt_BR `|Portuguese (Brazil) |
|`en_GB `|Great Britain (English) ||`pt_PT `|Portuguese (Portugal) |
|`en_IE `|Ireland (English) ||`ro `|Romanian |
|`en_IND `|India (English) ||`ru `|Russian |
|`en_NG `|Nigeria (English) ||`sk `|Slovakian |
|`en_US `|United States (English) ||`sv `|Swedish |
|`en_ZA `|South Africa (English) ||`tr `|Turkish |
|`es `|Spanish ||`uk `|Ukrainian |
|`es_MX `|Spanish Mexico ||`vi `|Vietnamese |
|`fa `|Farsi ||`zh_CN `|Chinese |
|`az `|Azerbaijani ||`hr `|Hrvatski |
|`cz `|Czech ||`id_ID `|Indonesia |
|`de `|German ||`it `|Italian |
|`de_AT `|German (Austria) ||`ja `|Japanese |
|`de_CH `|German (Switzerland) ||`ko `|Korean |
|`el `|Greek ||`lv `|Latvian |
|`en `|English ||`nb_NO `|Norwegian |
|`en_AU `|English (Australia) ||`nep `|Nepalese |
|`en_au_ocker `|English (Australia Ocker) ||`nl `|Dutch |
|`en_BORK `|English (Bork) ||`nl_BE `|Dutch (Belgium) |
|`en_CA `|English (Canada) ||`pl `|Polish |
|`en_GB `|English (Great Britain) ||`pt_BR `|Portuguese (Brazil) |
|`en_IE `|English (Ireland) ||`pt_PT `|Portuguese (Portugal) |
|`en_IND `|English (India) ||`ro `|Romanian |
|`en_NG `|Nigeria (English) ||`ru `|Russian |
|`en_US `|English (United States) ||`sk `|Slovakian |
|`en_ZA `|English (South Africa) ||`sv `|Swedish |
|`es `|Spanish ||`tr `|Turkish |
|`es_MX `|Spanish (Mexico) ||`uk `|Ukrainian |
|`fa `|Farsi ||`vi `|Vietnamese |
|`fi `|Finnish ||`zh_CN `|Chinese |
|`fr `|French ||`zh_TW `|Chinese (Taiwan) |
|`fr_CA `|Canada (French) ||`zu_ZA `|South Africa (Zulu) |
|`fr_CA `|French (Canada) ||`zu_ZA `|Zulu (South Africa) |


***Note:*** Some locales may not have a complete data set. For example, [`zh_CN`](https://github.com/Marak/faker.js/tree/master/lib/locales/zh_CN) does not have a `lorem` data set, but [`ko`](https://github.com/Marak/faker.js/tree/master/lib/locales/ko) has a `lorem` data set. **Bogus** will default to `en` if a *locale-specific* data set is not found. To further illustrate the previous example, the missing `zh_CN:lorem` data set will default to the `en:lorem` data set.
Expand Down
4 changes: 2 additions & 2 deletions Source/Bogus.Tests/DataSetTests/AddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void can_get_a_longitude()
[Fact]
public void can_get_a_random_country_code()
{
address.CountryCode().Should().Be("MQ");
address.CountryCode().Should().Be("MR");
}

[Fact]
public void can_get_a_random_country_code_alpha3()
{
address.CountryCode(Iso3166Format.Alpha3).Should().Be("MNP");
address.CountryCode(Iso3166Format.Alpha3).Should().Be("CMR");
}

[Fact]
Expand Down
6 changes: 6 additions & 0 deletions Source/Bogus.Tests/DataSetTests/DateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,11 @@ public void can_set_global_static_time_source()
d.PastOffset().Offset.Should().Be(DateTimeOffset.Now.Offset);
d.RecentOffset().Offset.Should().Be(DateTimeOffset.Now.Offset);
}

[Fact]
public void can_get_timezone_string()
{
date.TimeZoneString().Should().Be("Asia/Yerevan");
}
}
}
25 changes: 25 additions & 0 deletions Source/Bogus.Tests/DataSetTests/MusicTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Bogus.DataSets;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Bogus.Tests.DataSetTests
{
public class MusicTests : SeededTest
{
private readonly ITestOutputHelper console;
private Music music;

public MusicTests(ITestOutputHelper console)
{
this.console = console;
this.music = new Music();
}

[Fact]
public void can_generate_genre()
{
this.music.Genre().Should().Be("Hip Hop");
}
}
}
4 changes: 2 additions & 2 deletions Source/Bogus.Tests/DataSetTests/VehicleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public void can_get_a_vin_number()
[Fact]
public void can_get_a_manufacture()
{
vehicle.Manufacturer().Should().Be("Aston Martin");
vehicle.Manufacturer().Should().Be("Maserati");
}

[Fact]
public void can_get_a_model()
{
vehicle.Model().Should().Be("Roadster");
vehicle.Model().Should().Be("Prius");
}

[Fact]
Expand Down
125 changes: 125 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue321.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using Xunit;
using Bogus.Extensions;
using Xunit.Abstractions;

namespace Bogus.Tests.GitHubIssues
{
public class Issue321 : SeededTest
{
private readonly ITestOutputHelper console;

public Issue321(ITestOutputHelper console)
{
this.console = console;
}

[Fact]
public void reuse_data_from_custom_instantiator()
{
var unions = new[]
{
new Union("Married"),
new Union("Single"),
new Union("Divorced"),
};

var memberFaker = new Faker<Member>()
.CustomInstantiator(f =>
{
//Store intermediate state here.
var email = f.Internet.ExampleEmail();
var selectedUnion = f.PickRandom(unions);

return new Member(
f.Random.Guid().ToString("N"),
f.Name.FullName(),
f.Date.Between(new DateTime(1950, 3, 9), new DateTime(2010, 4, 2)),
f.Address.FullAddress(),
f.Phone.PhoneNumber(),
email,
CreateAppUser(email).Id,
selectedUnion,
selectedUnion.Id,
f.Random.ReplaceNumbers("######"),
f.Rant.Random.Words(5)
);
});

var testMembers = memberFaker.GenerateBetween(4, 10);
console.Dump(testMembers);
}

private AppUser CreateAppUser(string email)
{
return new AppUser(email);
}

public class AppUser
{
public AppUser(string email)
{
this.Email = email;
this.Id = $"appuser_id:{this.Email.ToLower()}";
}

public string Email { get; set; }

public string Id { get; set; }
}

public class Union
{
public Union(string description)
{
this.Description = description;
this.Id = $"union_id:{this.Description.ToLower()}";
}

public string Description { get; set; }

public string Id { get; set; }
}

public class Member
{
public string Id { get; }
public string Name { get; }
public DateTime Dob { get; }
public string Address { get; }
public string Phone { get; }
public string Email { get; }
public string AppUserId { get; }
public Union Union { get; }
public string UnionId { get; }
public string Code { get; }
public string Description { get; }

public Member(
string id,
string name,
DateTime dob,
string address,
string phone,
string email,
string appUserId,
Union union,
string unionId,
string code,
string description)
{
this.Id = id;
this.Name = name;
this.Dob = dob;
this.Address = address;
this.Phone = phone;
this.Email = email;
this.AppUserId = appUserId;
this.Union = union;
this.UnionId = unionId;
this.Code = code;
this.Description = description;
}
}
}
}
1 change: 0 additions & 1 deletion Source/Bogus.Tests/README_Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public void get_all_locales()
count++;
var code = kv.Key;
var title = kv.Value["title"].StringValue;
title = title.Replace("Ελληνικά", "Greek");

return new {code, title};
}).ToArray();
Expand Down
12 changes: 11 additions & 1 deletion Source/Bogus/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ protected internal virtual BArray GetArray(string path)
return (BArray)Get(path);
}

protected internal virtual BArray GetArray(string category, string path)
{
return (BArray)Get(category, path);
}

/// <summary>
/// Returns a BSON object given a JSON path into the data set. Only simple "." dotted JSON paths are supported.
/// </summary>
Expand All @@ -140,7 +145,12 @@ protected internal virtual BObject GetObject(string path)
/// <returns>A random item from the BSON array.</returns>
protected internal virtual string GetRandomArrayItem(string path, int? min = null, int? max = null)
{
var arr = GetArray(path);
return this.GetRandomArrayItem(this.Category, path, min, max);
}

protected internal virtual string GetRandomArrayItem(string category, string path, int? min = null, int? max = null)
{
var arr = GetArray(category, path);
if (!arr.HasValues)
{
return string.Empty;
Expand Down
2 changes: 1 addition & 1 deletion Source/Bogus/DataSets/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public string CountryCode(Iso3166Format format = Iso3166Format.Alpha2)

if( format == Iso3166Format.Alpha3 )
{
return GetRandomArrayItem("country_code3");
return GetRandomArrayItem("country_code_alpha_3");
}

throw new ArgumentException("Invalid country code", nameof(format));
Expand Down
8 changes: 8 additions & 0 deletions Source/Bogus/DataSets/Date.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,13 @@ public string Weekday(bool abbreviation = false, bool useContext = false)

return GetRandomArrayItem("weekday." + type);
}

/// <summary>
/// Get a timezone string. Eg: America/Los_Angeles
/// </summary>
public string TimeZoneString()
{
return GetRandomArrayItem("address", "time_zone");
}
}
}
13 changes: 13 additions & 0 deletions Source/Bogus/DataSets/Music.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Bogus.DataSets
{
public class Music : DataSet
{
/// <summary>
/// Get a music genre
/// </summary>
public string Genre()
{
return GetRandomArrayItem("genre");
}
}
}
8 changes: 8 additions & 0 deletions Source/Bogus/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public Faker(string locale = "en")
this.Rant = this.Notifier.Flow(new Rant());
this.Vehicle = this.Notifier.Flow(new Vehicle());

this.Music = this.Notifier.Flow(new Music());

this.Hashids = new Hashids();
}

Expand Down Expand Up @@ -191,6 +193,12 @@ public string Parse(string str)
[RegisterMustasheMethods]
public Vehicle Vehicle { get; set; }

/// <summary>
/// Generates data related to music.
/// </summary>
[RegisterMustasheMethods]
public Music Music { get; set; }

/// <summary>
/// Helper method to pick a random element.
/// </summary>
Expand Down
Binary file modified Source/Bogus/data/af_ZA.locale.bson
Binary file not shown.
2 changes: 1 addition & 1 deletion Source/Bogus/data/af_ZA.locale.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"title": "South Africa (Afrikaans)",
"title": "Afrikaans",
"address": {
"default_country": [
"South Africa"
Expand Down
Binary file modified Source/Bogus/data/cz.locale.bson
Binary file not shown.
Loading

0 comments on commit 380ff08

Please sign in to comment.