Skip to content

Commit

Permalink
Use C# 7 features, code cleanup. 🚿
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Apr 24, 2017
1 parent c88a9ea commit 0e5ff36
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 51 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added `ru` (Russian locale) hacker adjective, ing-verb, noun, and verb.
* Added `Internet.Mac` address separator parameter.
* Feature parity with **faker.js** @ 6cdb93ef...
* Using new C# 7 features. =)

## v15.0.1
* Building with Visual Studio 2017.
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 @@ -135,7 +135,7 @@ public string FullAddress()
var street = StreetAddress();
var city = City();
var country = Country();
return string.Format("{0}, {1}, {2}", street, city, country);
return $"{street}, {city}, {country}";
}

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions Source/Bogus/DataSets/Commerce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ public string[] Categories(int num)
/// </summary>
public string ProductName()
{
return string.Format( "{0} {1} {2}",
ProductAdjective(),
ProductMaterial(),
Product()
);
return $"{ProductAdjective()} {ProductMaterial()} {Product()}";
}

/// <summary>
Expand Down
10 changes: 2 additions & 8 deletions Source/Bogus/DataSets/Company.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public string CompanyName(string format)
/// <returns></returns>
public string CatchPhrase()
{
return string.Format("{0} {1} {2}",
CatchPhraseAdjective(),
CatchPhraseDescriptor(),
CatchPhraseNoun());
return $"{CatchPhraseAdjective()} {CatchPhraseDescriptor()} {CatchPhraseNoun()}";
}

/// <summary>
Expand All @@ -75,10 +72,7 @@ public string CatchPhrase()
/// <returns></returns>
public string Bs()
{
return string.Format("{0} {1} {2}",
BsAdjective(),
BsBuzz(),
BsNoun());
return $"{BsAdjective()} {BsBuzz()} {BsNoun()}";
}

#pragma warning disable 1591
Expand Down
2 changes: 1 addition & 1 deletion Source/Bogus/DataSets/Finance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public string Account(int length = 8)
public string AccountName()
{
var type = GetRandomArrayItem("account_type");
return string.Format("{0} Account", type);
return $"{type} Account";
}

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions Source/Bogus/DataSets/Internet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ public string DomainSuffix()
/// </summary>
public string Ip()
{
return string.Format("{0}.{1}.{2}.{3}",
Random.Number(255),
Random.Number(255),
Random.Number(255),
Random.Number(255));
return $"{Random.Number(255)}.{Random.Number(255)}.{Random.Number(255)}.{Random.Number(255)}";
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Bogus/DataSets/Name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public string JobTitle()
var level = JobArea();
var job = JobType();

return string.Format("{0} {1} {2}", descriptor, level, job);
return $"{descriptor} {level} {job}";
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/Bogus/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static JObject Initialize()
/// </summary>
public static JToken Get(string category, string key, string locale = "en", string localeFallback = "en" )
{
var path = string.Format("{0}.{1}.{2}", locale, category, key);
var path = $"{locale}.{category}.{key}";
var jtoken = Data.Value.SelectToken(path);

if( jtoken != null && jtoken.HasValues )
Expand All @@ -69,7 +69,7 @@ public static JToken Get(string category, string key, string locale = "en", stri
}

//fallback path
var fallbackPath = string.Format("{0}.{1}.{2}", localeFallback, category, key);
var fallbackPath = $"{localeFallback}.{category}.{key}";

return Data.Value.SelectToken(fallbackPath);
}
Expand Down
50 changes: 25 additions & 25 deletions Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ private Faker<T> RuleFor<TProperty>(string propertyOrField, Func<Faker, TPropert
Func<Faker, T, object> invoker = (f, t) => setter(f);

var rule = new PopulateAction<T>
{
Action = invoker,
RuleSet = currentRuleSet,
PropertyName = propertyOrField,
};
{
Action = invoker,
RuleSet = currentRuleSet,
PropertyName = propertyOrField,
};

this.Actions.Add(currentRuleSet, propertyOrField, rule);

Expand Down Expand Up @@ -191,13 +191,11 @@ public Faker<T> RuleForType<TType>(Type type, Func<Faker, TType> setterForType)

private Type GetFieldOrPropertyType(MemberInfo mi)
{
var pi = mi as PropertyInfo;
if( pi != null )
if( mi is PropertyInfo pi )
{
return pi.PropertyType;
}
var fi = mi as FieldInfo;
if( fi != null )
if( mi is FieldInfo fi )
{
return fi.FieldType;
}
Expand Down Expand Up @@ -228,8 +226,7 @@ public Faker<T> Ignore<TPropertyOrField>(Expression<Func<T, TPropertyOrField>> p
{
var propNameOrField = PropertyName.For(propertyOrField);

MemberInfo mi;
if( !this.TypeProperties.TryGetValue(propNameOrField, out mi) )
if( !this.TypeProperties.TryGetValue(propNameOrField, out MemberInfo mi) )
{
throw new ArgumentException($"The property or field {propNameOrField} was not found on {typeof(T)} during the binding discovery of T. Can't ignore something that doesn't exist.");
}
Expand Down Expand Up @@ -337,13 +334,11 @@ private void PopulateInternal(T instance, string[] ruleSets)

foreach ( var ruleSet in ruleSets )
{
Dictionary<string, PopulateAction<T>> populateActions;
if( this.Actions.TryGetValue(ruleSet, out populateActions) )
if( this.Actions.TryGetValue(ruleSet, out var populateActions) )
{
foreach( var action in populateActions.Values )
{
MemberInfo member;
typeProps.TryGetValue(action.PropertyName, out member);
typeProps.TryGetValue(action.PropertyName, out MemberInfo member);
var valueFactory = action.Action;

if( member != null )
Expand All @@ -365,8 +360,7 @@ private void PopulateInternal(T instance, string[] ruleSets)

foreach( var ruleSet in ruleSets )
{
FinalizeAction<T> finalizer;
if( this.FinalizeActions.TryGetValue(ruleSet, out finalizer) )
if( this.FinalizeActions.TryGetValue(ruleSet, out FinalizeAction<T> finalizer) )
{
finalizer.Action(this.FakerHub, instance);
}
Expand Down Expand Up @@ -431,24 +425,30 @@ private ValidationResult ValidateInternal(string[] ruleSets)
var strictMode = Faker.DefaultStrictMode;
this.StrictModes.TryGetValue(rule, out strictMode);

HashSet<string> ignores;
this.Ignores.TryGetValue(rule, out ignores);
this.Ignores.TryGetValue(rule, out HashSet<string> ignores);

Dictionary<string, PopulateAction<T>> populateActions;
this.Actions.TryGetValue(rule, out populateActions);
this.Actions.TryGetValue(rule, out var populateActions);

var finalSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (ignores != null)
if( ignores != null )
{
finalSet.UnionWith(ignores);
if (populateActions != null)
}
if( populateActions != null )
{
finalSet.UnionWith(populateActions.Keys);
}

if (!finalSet.SetEquals(propsOrFieldsOfT))
{
var delta = new List<string>();
foreach (var propOrField in propsOrFieldsOfT)
if (!finalSet.Contains(propOrField))
foreach( var propOrField in propsOrFieldsOfT )
{
if( !finalSet.Contains(propOrField) )
{
delta.Add(propOrField);
}
}
result.MissingRules.AddRange(delta);
result.IsValid = !strictMode;
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Bogus/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public static string Parse(string str, params DataSet[] dataSets)
MustashMethod mm;
if( !MustashMethods.TryGetValue(methodName, out mm) )
{
throw new ArgumentException(string.Format("Unknown method {0} can't be found.", methodName));
throw new ArgumentException($"Unknown method {methodName} can't be found.");
}

var module = dataSets.FirstOrDefault(o => o.GetType() == mm.Method.DeclaringType);

if( module == null )
{
throw new ArgumentException(string.Format("Can't parse {0} because the dataset was not provided in the dataSets parameter.", methodName));
throw new ArgumentException($"Can't parse {methodName} because the dataset was not provided in the dataSets parameter.");
}

var fakeVal = mm.Method.Invoke(module, mm.OptionalArgs) as string;
Expand Down Expand Up @@ -117,7 +117,7 @@ public static string ParseOld(string expr, params DataSet[] dataSets)
var module = dataSets.FirstOrDefault(o => o.GetType() == mustashMethod.Method.DeclaringType);

if( module == null )
throw new ArgumentException(string.Format("Can't parse {0} because the dataset was not provided in the dataSets parameter.", handle));
throw new ArgumentException($"Can't parse {handle} because the dataset was not provided in the dataSets parameter.");

var val = mustashMethod.Method.Invoke(module, null) as string;

Expand Down

0 comments on commit 0e5ff36

Please sign in to comment.