Skip to content

Commit

Permalink
Added the Bank faker (and the Regexify class) (#122)
Browse files Browse the repository at this point in the history
* Added documentation.

* Added the BankFaker to the main faker object.

* Added some bank data.

* Added the Regexify class for generating fake data from a regular expression.

* Split up regex replacements into separate files so we can test them individually.

* Implemented the BankFaker

* Added coverage tasks.
  • Loading branch information
mrstebo authored Apr 30, 2019
1 parent 7bcebe6 commit 8e7dde5
Show file tree
Hide file tree
Showing 32 changed files with 1,069 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,4 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/
lcov.info
57 changes: 56 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,62 @@
"build",
"${workspaceFolder}/tests/FakerDotNet.Tests/FakerDotNet.Tests.csproj"
],
"problemMatcher": "$msCompile"
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "test",
"command": "dotnet",
"type": "process",
"args": [
"test",
"${workspaceFolder}/tests/FakerDotNet.Tests/FakerDotNet.Tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "test with coverage",
"command": "dotnet",
"type": "process",
"args": [
"test",
"/p:CollectCoverage=true",
"/p:CoverletOutputFormat=lcov",
"/p:CoverletOutput=${workspaceFolder}/lcov.info",
"${workspaceFolder}/tests/FakerDotNet.Tests/FakerDotNet.Tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "watch test with coverage",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"--project",
"${workspaceFolder}/src/FakerDotNet/FakerDotNet.csproj",
"test",
"/p:CollectCoverage=true",
"/p:CoverletOutputFormat=lcov",
"/p:CoverletOutput=${workspaceFolder}/lcov.info",
"${workspaceFolder}/tests/FakerDotNet.Tests/FakerDotNet.Tests.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
}
]
}
1 change: 1 addition & 0 deletions FakerDotNet.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IP/@EntryIndexedValue">IP</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=RNG/@EntryIndexedValue">RNG</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=grayscale/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Regexify/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ukelele/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ A .NET port of the Ruby [faker](https://github.com/stympy/faker) gem
- [Faker.Ancient](doc/ancient.md)
- [Faker.App](doc/app.md)
- [Faker.Avatar](doc/avatar.md)
- [Faker.Bank](doc/bank.md)
- [Faker.Beer](doc/beer.md)
- [Faker.Book](doc/book.md)
- [Faker.Boolean](doc/boolean.md)
Expand Down
18 changes: 18 additions & 0 deletions doc/bank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Faker.Bank

```cs

Faker.Bank.AccountNumber() //=> 6738582379
Faker.Bank.AccountNumber(13) //=> 673858237902
Faker.Bank.Iban() //=> "GB19DZJM33188515981979"
// Optional argument bankCountryCode
// All countries should be supported
Faker.Bank.Iban("be") //=> "BE8975388567752043"
Faker.Bank.Name() //=> "ABN AMRO CORPORATE FINANCE LIMITED"
Faker.Bank.SwiftBic() //=> "AAFMGB21"
```
132 changes: 132 additions & 0 deletions src/FakerDotNet/Data/BankData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Collections.Generic;

namespace FakerDotNet.Data
{
internal static class BankData
{
public static readonly IEnumerable<string> Names = new[]
{
"UBS CLEARING AND EXECUTION SERVICES LIMITED",
"ABN AMRO CORPORATE FINANCE LIMITED",
"ABN AMRO FUND MANAGERS LIMITED",
"ABN AMRO HOARE GOVETT SECURITIES",
"ABN AMRO HOARE GOVETT CORPORATE FINANCE LTD.",
"ALKEN ASSET MANAGEMENT",
"ALKEN ASSET MANAGEMENT",
"ABN AMRO HOARE GOVETT LIMITED",
"AAC CAPITAL PARTNERS LIMITED",
"ABBOTSTONE AGRICULTURAL PROPERTY UNIT TRUST",
"ABN AMRO QUOTED INVESTMENTS (UK) LIMITED",
"ABN AMRO MEZZANINE (UK) LIMITED",
"ABBEY LIFE",
"SANTANDER UK PLC",
"OTKRITIE SECURITIES LIMITED",
"ABC INTERNATIONAL BANK PLC",
"ALLIED BANK PHILIPPINES (UK) PLC",
"ABU DHABI ISLAMIC BANK",
"ABG SUNDAL COLLIER LIMITED",
"PGMS (GLASGOW) LIMITED",
"ABINGWORTH MANAGEMENT LIMITED",
"THE ROYAL BANK OF SCOTLAND PLC (FORMER RBS NV)"
};

public static readonly IEnumerable<string> SwiftBics = new[]
{
"AACCGB21",
"AACNGB21",
"AAFMGB21",
"AAHOGB21",
"AAHVGB21",
"AANLGB21",
"AANLGB2L",
"AAOGGB21",
"AAPEGB21",
"AAPUGB21",
"AAQIGB21",
"ABAZGB21",
"ABBEGB21",
"ABBYGB2L",
"ABCCGB22",
"ABCEGB2L",
"ABCMGB21",
"ABDIGB21",
"ABECGB21",
"ABFIGB21",
"ABMNGB21",
"ABNAGB21VOC"
};

public static readonly IReadOnlyDictionary<string, (int length, string pattern)> IbanDetails =
new Dictionary<string, (int length, string pattern)>
{
{"ad", (24, @"\d{8}[A-Z0-9]{12}")}, // Andorra
{"ae", (23, @"\d{19}")}, // United Arab Emirates
{"al", (28, @"\d{8}[A-Z0-9]{16}")}, // Albania
{"at", (20, @"\d{16}")}, // Austria
{"az", (28, @"[A-Z]{4}[A-Z0-9]{20}")}, // Azerbaijan, Republic of
{"ba", (20, @"\d{16}")}, // Bosnia
{"be", (16, @"\d{12}")}, // Belgium
{"bg", (22, @"[A-Z]{4}\d{6}[A-Z0-9]{8}")}, // Bulgaria
{"bh", (22, @"[A-Z]{4}[A-Z0-9]{14}")}, // Bahrain
{"br", (29, @"[0-9]{8}[0-9]{5}[0-9]{10}[A-Z]{1}[A-Z0-9]{1}")}, // Brazil
{"ch", (21, @"\d{5}[A-Z0-9]{12}")}, // Switzerland
{"cr", (22, @"0\d{3}\d{14}")}, // Costa Rica
{"cy", (28, @"\d{8}[A-Z0-9]{16}")}, // Cyprus
{"cz", (24, @"\d{20}")}, // Czech Republic
{"de", (22, @"\d{18}")}, // Germany
{"dk", (18, @"\d{14}")}, // Denmark
{"do", (28, @"[A-Z]{4}\d{20}")}, // Dominican Republic
{"ee", (20, @"\d{16}")}, // Estonia
{"es", (24, @"\d{20}")}, // Spain
{"fi", (18, @"\d{14}")}, // Finland
{"fo", (18, @"\d{14}")}, // Faroe Islands
{"fr", (27, @"\d{10}[A-Z0-9]{11}\d{2}")}, // France
{"gb", (22, @"[A-Z]{4}\d{14}")}, // United Kingdom
{"ge", (22, @"[A-Z]{2}\d{16}")}, // Georgia
{"gi", (23, @"[A-Z]{4}[A-Z0-9]{15}")}, // Gibraltar
{"gl", (18, @"\d{14}")}, // Greenland
{"gr", (27, @"\d{7}[A-Z0-9]{16}")}, // Greece
{"gt", (28, @"[A-Z0-9]{4}\d{2}\d{2}[A-Z0-9]{16}")}, // Guatemala
{"hr", (21, @"\d{17}")}, // Croatia
{"hu", (28, @"\d{24}")}, // Hungary
{"ie", (22, @"[A-Z]{4}\d{14}")}, // Ireland
{"il", (23, @"\d{19}")}, // Israel
{"is", (26, @"\d{22}")}, // Iceland
{"it", (27, @"[A-Z]\d{10}[A-Z0-9]{12}")}, // Italy
{"kw", (30, @"[A-Z]{4}\d{22}")}, // Kuwait
{"kz", (20, @"[0-9]{3}[A-Z0-9]{13}")}, // Kazakhstan
{"lb", (28, @"\d{4}[A-Z0-9]{20}")}, // Lebanon
{"li", (21, @"\d{5}[A-Z0-9]{12}")}, // Liechtenstein
{"lt", (20, @"\d{16}")}, // Lithuania
{"lu", (20, @"\d{3}[A-Z0-9]{13}")}, // Luxembourg
{"lv", (21, @"[A-Z]{4}[A-Z0-9]{13}")}, // Latvia
{"mc", (27, @"\d{10}[A-Z0-9]{11}\d{2}")}, // Monaco
{"md", (24, @"[A-Z]{2}[A-Z0-9]{18}")}, // Moldova
{"me", (22, @"\d{18}")}, // Montenegro
{"mk", (19, @"\d{3}[A-Z0-9]{10}\d{2}")}, // Macedonia
{"mr", (27, @"\d{23}")}, // Mauritania
{"mt", (31, @"[A-Z]{4}\d{5}[A-Z0-9]{18}")}, // Malta
{"mu", (30, @"[A-Z]{4}\d{19}[A-Z]{3}")}, // Mauritius
{"nl", (18, @"[A-Z]{4}\d{10}")}, // Netherlands
{"no", (15, @"\d{11}")}, // Norway
{"pk", (24, @"[A-Z]{4}[A-Z0-9]{16}")}, // Pakistan
{"pl", (28, @"\d{8}[A-Z0-9]{16}")}, // Poland
{"ps", (29, @"[A-Z]{4}[A-Z0-9]{21}")}, // Palestinian Territory, Occupied
{"pt", (25, @"\d{21}")}, // Portugal
{"qa", (29, @"[A-Z]{4}[A-Z0-9]{21}")}, // Qatar
{"ro", (24, @"[A-Z]{4}[A-Z0-9]{16}")}, // Romania
{"rs", (22, @"\d{18}")}, // Serbia
{"sa", (24, @"\d{2}[A-Z0-9]{18}")}, // Saudi Arabia
{"se", (24, @"\d{20}")}, // Sweden
{"si", (19, @"\d{15}")}, // Slovenia
{"sk", (24, @"\d{20}")}, // Slovakia
{"sm", (27, @"[A-Z]\d{10}[A-Z0-9]{12}")}, // San Marino
{"tl", (23, @"\d{19}")}, // Timor-Leste
{"tn", (24, @"\d{20}")}, // Tunisia
{"tr", (26, @"\d{5}[A-Z0-9]{17}")}, // Turkey
{"ua", (29, @"\d{25}")}, // Ukraine
{"vg", (24, @"[A-Z]{4}\d{16}")}, // Virgin Islands, British
{"xk", (20, @"\d{16}")}, // Kosovo, Republic of
};
}
}
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class Faker
public static IAncientFaker Ancient { get; } = Container.Ancient;
public static IAppFaker App { get; } = Container.App;
public static IAvatarFaker Avatar { get; } = Container.Avatar;
public static IBankFaker Bank { get; } = Container.Bank;
public static IBeerFaker Beer { get; } = Container.Beer;
public static IBookFaker Book { get; } = Container.Book;
public static IBooleanFaker Boolean { get; } = Container.Boolean;
Expand Down
12 changes: 11 additions & 1 deletion src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FakerDotNet.Fakers;
using FakerDotNet.Fakers;
using FakerDotNet.FakerUtils;

namespace FakerDotNet
{
Expand All @@ -8,6 +9,7 @@ internal interface IFakerContainer
IAncientFaker Ancient { get; }
IAppFaker App { get; }
IAvatarFaker Avatar { get; }
IBankFaker Bank { get; }
IBeerFaker Beer { get; }
IBookFaker Book { get; }
IBooleanFaker Boolean { get; }
Expand Down Expand Up @@ -59,6 +61,8 @@ internal interface IFakerContainer
IUniversityFaker University { get; }
IVehicleFaker Vehicle { get; }
IZeldaFaker Zelda { get; }

IRegexify Regexify { get; }
}

internal class FakerContainer : IFakerContainer
Expand All @@ -69,6 +73,7 @@ public FakerContainer()
Ancient = new AncientFaker(this);
App = new AppFaker(this);
Avatar = new AvatarFaker(this);
Bank = new BankFaker(this);
Beer = new BeerFaker(this);
Book = new BookFaker(this);
Boolean = new BooleanFaker();
Expand Down Expand Up @@ -120,12 +125,15 @@ public FakerContainer()
University = new UniversityFaker(this);
Vehicle = new VehicleFaker(this);
Zelda = new ZeldaFaker(this);

Regexify = new Regexify(this);
}

public IAddressFaker Address { get; }
public IAncientFaker Ancient { get; }
public IAppFaker App { get; }
public IAvatarFaker Avatar { get; }
public IBankFaker Bank { get; }
public IBeerFaker Beer { get; }
public IBookFaker Book { get; }
public IBooleanFaker Boolean { get; }
Expand Down Expand Up @@ -177,5 +185,7 @@ public FakerContainer()
public IUniversityFaker University { get; }
public IVehicleFaker Vehicle { get; }
public IZeldaFaker Zelda { get; }

public IRegexify Regexify { get; }
}
}
36 changes: 36 additions & 0 deletions src/FakerDotNet/FakerUtils/Regexify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Linq;
using FakerDotNet.FakerUtils.RegexifyParsers;

namespace FakerDotNet.FakerUtils
{
internal interface IRegexify
{
string Parse(string pattern);
}

internal class Regexify : IRegexify
{
private readonly IList<IRegexifyParser> _parsers;

public Regexify(IFakerContainer fakerContainer)
{
_parsers = new IRegexifyParser[]
{
new DitchAnchors(),
new NumberPatternToRange(),
new ParseRanges(fakerContainer),
new ReplaceOneOfWordsWithWord(fakerContainer),
new ReplaceOneOfRangeWithLetter(fakerContainer),
new ReplaceOneOfLettersWithLetter(fakerContainer),
new ParseNumbers(fakerContainer),
new ParseLetters(fakerContainer),
};
}

public string Parse(string pattern)
{
return _parsers.Aggregate(pattern ?? "", (result, replacer) => replacer.Run(result));
}
}
}
15 changes: 15 additions & 0 deletions src/FakerDotNet/FakerUtils/RegexifyParsers/DitchAnchors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.RegularExpressions;

namespace FakerDotNet.FakerUtils.RegexifyParsers
{
internal class DitchAnchors : IRegexifyParser
{
public string Run(string pattern)
{
var result = pattern;
result = Regex.Replace(result, @"^\/?\^?", "");
result = Regex.Replace(result, @"\$\/?$", "");
return result;
}
}
}
7 changes: 7 additions & 0 deletions src/FakerDotNet/FakerUtils/RegexifyParsers/IRegexifyParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FakerDotNet.FakerUtils.RegexifyParsers
{
internal interface IRegexifyParser
{
string Run(string pattern);
}
}
16 changes: 16 additions & 0 deletions src/FakerDotNet/FakerUtils/RegexifyParsers/NumberPatternToRange.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.RegularExpressions;

namespace FakerDotNet.FakerUtils.RegexifyParsers
{
internal class NumberPatternToRange : IRegexifyParser
{
public string Run(string pattern)
{
var result = pattern;
result = Regex.Replace(result, @"\{(\d+)\}",
m => $@"{{{m.Groups[1].Value},{m.Groups[1].Value}}}"); // All {2} become {2,2}
result = Regex.Replace(result, @"\?", "{0,1}"); // All ? become {0,1}
return result;
}
}
}
19 changes: 19 additions & 0 deletions src/FakerDotNet/FakerUtils/RegexifyParsers/ParseLetters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.RegularExpressions;

namespace FakerDotNet.FakerUtils.RegexifyParsers
{
internal class ParseLetters : IRegexifyParser
{
private readonly IFakerContainer _fakerContainer;

public ParseLetters(IFakerContainer fakerContainer)
{
_fakerContainer = fakerContainer;
}

public string Run(string pattern)
{
return Regex.Replace(pattern, @"\\w", _ => _fakerContainer.Lorem.Character());
}
}
}
Loading

0 comments on commit 8e7dde5

Please sign in to comment.