-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/app-faker
# Conflicts: # README.md # src/FakerDotNet/Faker.cs # src/FakerDotNet/FakerContainer.cs # tests/FakerDotNet.Tests/FakerContainerTests.cs # tests/FakerDotNet.Tests/FakerTests.cs
- Loading branch information
Showing
8 changed files
with
169 additions
and
1 deletion.
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,7 @@ | ||
# Faker.Fake | ||
|
||
```cs | ||
Faker.Fake.F("{Name.FirstName}") //=> "John" | ||
Faker.Fake.F("{Name.FirstName} {Name.LastName}") //=> "John Smith" | ||
``` |
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
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,63 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace FakerDotNet.Fakers | ||
{ | ||
public interface IFakeFaker | ||
{ | ||
string F(string format); | ||
} | ||
|
||
internal class FakeFaker : IFakeFaker | ||
{ | ||
private readonly IFakerContainer _fakerContainer; | ||
|
||
public FakeFaker(IFakerContainer fakerContainer) | ||
{ | ||
_fakerContainer = fakerContainer; | ||
} | ||
|
||
public string F(string format) | ||
{ | ||
if (string.IsNullOrEmpty(format)) throw new FormatException("A string must be specified"); | ||
|
||
var result = format; | ||
Match match; | ||
while ((match = Regex.Match(result, @"\{(\w+).(\w+)\}")).Success) | ||
{ | ||
var matchData = ExtractMatchDataFrom(match); | ||
var faker = GetFaker(matchData.faker); | ||
var value = GetValue(faker, matchData.method); | ||
|
||
result = $"{result.Substring(0, match.Index)}{value}{result.Substring(match.Index + match.Length)}"; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private PropertyInfo GetFaker(string name) | ||
{ | ||
var propertyInfo = _fakerContainer.GetType().GetProperty(name); | ||
|
||
return propertyInfo ?? throw new FormatException($"Invalid module: {name}"); | ||
} | ||
|
||
private string GetValue(PropertyInfo propertyInfo, string methodName) | ||
{ | ||
var method = propertyInfo.PropertyType.GetMethod(methodName); | ||
|
||
if (method == null) throw new FormatException($"Invalid method: {propertyInfo.Name}.{methodName}"); | ||
|
||
return Convert.ToString(method.Invoke(propertyInfo.GetValue(_fakerContainer, null), new object[] { })); | ||
} | ||
|
||
private static (string faker, string method) ExtractMatchDataFrom(Match match) | ||
{ | ||
var className = match.Groups[1].Value; | ||
var methodName = match.Groups[2].Value; | ||
|
||
return (className, methodName); | ||
} | ||
} | ||
} |
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
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,81 @@ | ||
using System; | ||
using FakeItEasy; | ||
using FakerDotNet.Fakers; | ||
using NUnit.Framework; | ||
|
||
namespace FakerDotNet.Tests.Fakers | ||
{ | ||
[TestFixture] | ||
[Parallelizable] | ||
public class FakeFakerTests | ||
{ | ||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_fakerContainer = A.Fake<IFakerContainer>(); | ||
_fakeFaker = new FakeFaker(_fakerContainer); | ||
} | ||
|
||
private IFakerContainer _fakerContainer; | ||
private IFakeFaker _fakeFaker; | ||
|
||
[Test] | ||
public void F_returns_filled_in_string() | ||
{ | ||
const string format = "{Name.FirstName} {Name.LastName}"; | ||
|
||
A.CallTo(() => _fakerContainer.Name.FirstName()).Returns("John"); | ||
A.CallTo(() => _fakerContainer.Name.LastName()).Returns("Smith"); | ||
|
||
Assert.AreEqual("John Smith", _fakeFaker.F(format)); | ||
} | ||
|
||
[Test] | ||
public void F_handles_duplicate_placeholders() | ||
{ | ||
const string format = "{Name.FirstName} {Name.FirstName}"; | ||
|
||
A.CallTo(() => _fakerContainer.Name.FirstName()).ReturnsNextFromSequence("Test1", "Test2"); | ||
|
||
Assert.AreEqual("Test1 Test2", _fakeFaker.F(format)); | ||
} | ||
|
||
[Test] | ||
public void F_with_null_throws_FormatException() | ||
{ | ||
var ex = Assert.Throws<FormatException>(() => _fakeFaker.F(null)); | ||
|
||
Assert.AreEqual("A string must be specified", ex.Message); | ||
} | ||
|
||
[Test] | ||
public void F_with_empty_string_throws_FormatException() | ||
{ | ||
const string format = ""; | ||
|
||
var ex = Assert.Throws<FormatException>(() => _fakeFaker.F(format)); | ||
|
||
Assert.AreEqual("A string must be specified", ex.Message); | ||
} | ||
|
||
[Test] | ||
public void F_with_invalid_faker_throws_FormatException() | ||
{ | ||
const string format = "{Unknown.Test}"; | ||
|
||
var ex = Assert.Throws<FormatException>(() => _fakeFaker.F(format)); | ||
|
||
Assert.AreEqual("Invalid module: Unknown", ex.Message); | ||
} | ||
|
||
[Test] | ||
public void F_with_invalid_method_throws_FormatException() | ||
{ | ||
const string format = "{Name.BadMethod}"; | ||
|
||
var ex = Assert.Throws<FormatException>(() => _fakeFaker.F(format)); | ||
|
||
Assert.AreEqual("Invalid method: Name.BadMethod", ex.Message); | ||
} | ||
} | ||
} |