-
-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
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
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Bogus" Version="33.0.2" /> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,20 @@ | ||
namespace ExtendingBogus | ||
{ | ||
/// <summary> | ||
/// Augment the existing <seealso cref="Bogus.DataSets.Address"/> DataSet via C# extension method. | ||
/// </summary> | ||
public static class ExtensionsForAddress | ||
{ | ||
private static readonly string[] CanadaDowntownTorontoPostalCodes = | ||
{ | ||
"M5S", "M5B", "M5X", "M5V", "M4W", "M4X", "M4Y", | ||
"M5A", "M5C", "M5T", "M5E", "M5G", "M5H", "M5J", | ||
"M5K", "M5L", "M6G" | ||
}; | ||
|
||
public static string DowntownTorontoPostalCode(this Bogus.DataSets.Address address) | ||
{ | ||
return address.Random.ArrayElement(CanadaDowntownTorontoPostalCodes); | ||
} | ||
} | ||
} |
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,18 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace ExtendingBogus | ||
{ | ||
public static class ExtensionsForTesting | ||
{ | ||
public static void Dump(this object obj) | ||
{ | ||
Console.WriteLine(obj.DumpString()); | ||
} | ||
|
||
public static string DumpString(this object obj) | ||
{ | ||
return JsonConvert.SerializeObject(obj, Formatting.Indented); | ||
} | ||
} | ||
} |
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,43 @@ | ||
using Bogus; | ||
using Bogus.Premium; | ||
|
||
namespace ExtendingBogus | ||
{ | ||
/// <summary> | ||
/// The following shows how to create a dedicated DataSet accessible via C# extension method. | ||
/// </summary> | ||
public static class ExtensionsForCandy | ||
{ | ||
public static Food Food(this Faker faker) | ||
{ | ||
return ContextHelper.GetOrSet(faker, () => new Food()); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This DatSet can be created manually using `new Candy()`, or by fluent extension method via <seealso cref="ExtensionsForCandy"/>. | ||
/// </summary> | ||
public class Food : DataSet | ||
{ | ||
private static readonly string[] Candies = | ||
{ | ||
"Hard candy", "Taffy", "Chocolate bar", "Stick candy", | ||
"Jelly bean", "Mint", "Cotton candy", "Lollipop" | ||
}; | ||
|
||
/// <summary> | ||
/// Returns some type of candy. | ||
/// </summary> | ||
public string Candy() | ||
{ | ||
return this.Random.ArrayElement(Candies); | ||
} | ||
|
||
private static readonly string[] Drinks = { "Soda", "Water", "Beer", "Wine", "Coffee", "Lemonade", "Milk" }; | ||
public string Drink() | ||
{ | ||
return this.Random.ArrayElement(Drinks); | ||
} | ||
} | ||
|
||
} |
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,27 @@ | ||
using Bogus; | ||
|
||
namespace ExtendingBogus | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var userFaker = new Faker<User>() | ||
//Extend Bogus with a 'new' Food data set; see FoodDataSet.cs | ||
.RuleFor(p => p.FaveCandy, f => f.Food().Candy()) | ||
.RuleFor(p => p.FaveDrink, f => f.Food().Drink()) | ||
//Extend the existing Address data set with a custom C# extension method; see ExtensionsForAddress.cs | ||
.RuleFor(p => p.PostCode, f => f.Address.DowntownTorontoPostalCode()); | ||
|
||
var user = userFaker.Generate(); | ||
user.Dump(); | ||
} | ||
} | ||
|
||
public class User | ||
{ | ||
public string FaveDrink; | ||
public string FaveCandy; | ||
public string PostCode; | ||
} | ||
} |
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,37 @@ | ||
[1]:https://github.com/bchavez/Bogus#the-great-c-example | ||
|
||
## Getting Started with Bogus | ||
|
||
#### Requirements | ||
* **.NET Core 3.1** or later | ||
|
||
#### Description | ||
|
||
The `ExtendingBogus` example shows how to extend **Bogus**' APIs in the following ways: | ||
|
||
1. Using a custom C# extension method; see `ExtensionsForAddress.cs`. | ||
|
||
Augmenting **Bogus**' APIs via C# extension is useful to make APIs cleaner and more suitable for your specific situation. | ||
|
||
1. Using a custom data set; see `FoodDataSet.cs`. | ||
|
||
Creating a custom data set is useful when categorizing many 'related' APIs together. | ||
|
||
To run the example, perform the following commands *inside* this `ExtendingBogus` folder: | ||
|
||
* `dotnet restore` | ||
* `dotnet build` | ||
* `dotnet run` | ||
|
||
After the `dotnet` commands are successfully executed above, you should see some extended *custom* fake data printed to the console! | ||
|
||
``` | ||
> dotnet run | ||
``` | ||
```json | ||
{ | ||
"FaveDrink": "Soda", | ||
"FaveCandy": "Jelly bean", | ||
"PostCode": "M4W" | ||
} | ||
``` |
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