Skip to content

Commit

Permalink
Fix for failing Date UnitTest alternative locale.
Browse files Browse the repository at this point in the history
Supports for alternative combining Address strings based on rules in locale json.
  • Loading branch information
JvanderStad committed Feb 25, 2016
1 parent e46f4bf commit bfb57a0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Source/Bogus.Tests/AddressTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void can_get_canadian_zip_code()
[Test]
public void can_get_a_city_name()
{
address.City().Should().Be("Bernhard fort");
address.City().Should().Be("Bernhardfort");
}

[Test]
Expand Down
9 changes: 5 additions & 4 deletions Source/Bogus.Tests/DateTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Net;
using Bogus.DataSets;
using FluentAssertions;
Expand Down Expand Up @@ -31,7 +32,7 @@ public void can_get_date_in_past()
[Test]
public void can_get_date_in_past_with_custom_options()
{
var starting = DateTime.Parse("6/15/2000 4:17:41 PM");
var starting = DateTime.Parse("6/15/2000 4:17:41 PM", CultureInfo.InvariantCulture);
date.Past(refDate: starting, yearsToGoBack: 5).Should()
.BeOnOrBefore(starting)
.And
Expand All @@ -52,7 +53,7 @@ public void can_get_date_in_future()
[Test]
public void can_get_date_in_future_with_options()
{
var starting = DateTime.Parse("6/15/2000 4:17:41 PM");
var starting = DateTime.Parse("6/15/2000 4:17:41 PM", CultureInfo.InvariantCulture);
date.Future(refDate: starting, yearsToGoForward: 5).Should()
.BeOnOrBefore(starting.AddYears(5))
.And
Expand All @@ -63,8 +64,8 @@ public void can_get_date_in_future_with_options()
[Test]
public void can_get_random_time_between_two_dates()
{
var start = DateTime.Parse("6/15/2000 4:17:41 PM");
var end = DateTime.Parse("8/15/2000 4:17:41 PM");
var start = DateTime.Parse("6/15/2000 4:17:41 PM", CultureInfo.InvariantCulture);
var end = DateTime.Parse("8/15/2000 4:17:41 PM", CultureInfo.InvariantCulture);

date.Between(start, end)
.Should()
Expand Down
48 changes: 48 additions & 0 deletions Source/Bogus/DataSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.RegularExpressions;
using Bogus.Extensions;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -86,5 +87,52 @@ public string GetRandomArrayItem(string keyOrSubPath)
{
return Random.ArrayElement(GetArray(keyOrSubPath));
}


/// <summary>
/// Retreives a random value from the locale info.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>System.String.</returns>
protected string GetRandomValue( string name )
{
var value = GetRandomArrayItem( name );

var tokenResult = ParseTokens( value );

return Random.Replace( tokenResult );
}

/// <summary>
/// Recursive parse the tokens in the string .
/// </summary>
/// <param name="value">The value.</param>
/// <returns>System.String.</returns>
private string ParseTokens( string value )
{
var regex = new Regex( "\\#{(.*?)\\}" );
var cityResult = regex.Replace( value,
x =>
{
JArray result;
var groupValue = x.Groups[1].Value.ToLower().Split( '.' );
if ( groupValue.Length == 1 )
{
result = (JArray) Database.Get( Category, groupValue[0], Locale );
}
else
{
result = (JArray) Database.Get( groupValue[0], groupValue[1], Locale );
}
var randomElement = Random.ArrayElement( result );
//replace values
return ParseTokens( randomElement );
}
);
return cityResult;
}
}
}
60 changes: 24 additions & 36 deletions Source/Bogus/DataSets/Address.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
using System;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;

namespace Bogus.DataSets
{
/// <summary>
Expand All @@ -8,15 +12,15 @@ public class Address : DataSet
/// <summary>
/// The source to pull names from.
/// </summary>
protected Name Name = null;
protected Name Name;

/// <summary>
/// Default constructor
/// </summary>
/// <param name="locale"></param>
public Address(string locale = "en") : base(locale)
public Address( string locale = "en" ) : base( locale )
{
this.Name = new Name(locale);
this.Name = new Name( locale );
}

/// <summary>
Expand All @@ -25,12 +29,7 @@ public Address(string locale = "en") : base(locale)
/// <returns></returns>
public string ZipCode(string format = null)
{
if( format == null )
{
format = GetRandomArrayItem("postcode");
}

return Random.Replace(format);
return format == null ? GetRandomValue( "postcode" ) : Random.Replace(format);
}

/// <summary>
Expand All @@ -39,36 +38,20 @@ public string ZipCode(string format = null)
/// <returns></returns>
public string City()
{
var format = Random.Number(3);
if( format == 0 )
return string.Format("{0} {1} {2}", CityPrefix(), Name.FirstName(), CitySuffix());

if( format == 1 )
return string.Format("{0} {1}", CityPrefix(), Name.FirstName());

if( format == 2 )
return string.Format("{0} {1}", Name.FirstName(), CitySuffix());

return string.Format("{0} {1}", Name.LastName(), CitySuffix());
return GetRandomValue( "city" );
}



/// <summary>
/// Get a street address.
/// </summary>
/// <param name="useFullAddress"></param>
/// <returns></returns>
public string StreetAddress(bool useFullAddress = false)
{
var homeNumbers = new string('#', Random.Number(3, 5));

var houseNumber = Random.Replace(homeNumbers);

if( useFullAddress )
{
return string.Format("{0} {1} {2}", houseNumber, StreetName(), SecondaryAddress());
}

return string.Format("{0} {1}", houseNumber, StreetName());
var streetAddress = GetRandomValue( "street_address" );
return useFullAddress ? $"{streetAddress} {SecondaryAddress()}" : streetAddress;
}

/// <summary>
Expand All @@ -95,7 +78,16 @@ public string CitySuffix()
/// <returns></returns>
public string StreetName()
{
return $"{(Random.Bool() ? Name.FirstName() : Name.LastName())} {StreetSuffix()}".Trim();
return GetRandomValue( "street_name" );
}

/// <summary>
/// Get the buildingnumber
/// </summary>
/// <returns></returns>
public string BuildingNumber()
{
return GetRandomValue( "building_number" );
}

/// <summary>
Expand All @@ -113,11 +105,7 @@ public string StreetSuffix()
/// <returns></returns>
public string SecondaryAddress()
{
var formats = new[] {"Apt. ###", "Suite ###"};

var format = Random.ArrayElement(formats);

return Random.Replace(format);
return GetRandomValue( "secondary_address" );
}

/// <summary>
Expand Down

0 comments on commit bfb57a0

Please sign in to comment.