Skip to content

Commit

Permalink
Merge 65b3e30 into 09a1319
Browse files Browse the repository at this point in the history
  • Loading branch information
mrstebo committed Feb 6, 2019
2 parents 09a1319 + 65b3e30 commit bf82d0d
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ A .NET port of the Ruby [faker](https://github.com/stympy/faker) gem
- [Faker.Commerce](doc/commerce.md)
- [Faker.Company](doc/company.md)
- [Faker.Date](doc/date.md)
- [Faker.Demographic](doc/demographic.md)
- [Faker.DragonBall](doc/dragon_ball.md)
- [Faker.Educator](doc/educator.md)
- [Faker.Fake](doc/fake.md)
Expand Down
17 changes: 17 additions & 0 deletions doc/demographic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Faker.Demographic

```cs
Faker.Demographic.Race() //=> "Native Hawaiian or Other Pacific Islander"
Faker.Demographic.EducationalAttainment() //=> "GED or alternative credential"
Faker.Demographic.Demonym() //=> "Panamanian"
Faker.Demographic.MaritalStatus() //=> "Widowed"
Faker.Demographic.Sex() //=> "Female"
Faker.Demographic.Height() //=> "1.61"
Faker.Demographic.Height(UnitType.Imperial) //=> "6 ft, 2 in"
```
53 changes: 53 additions & 0 deletions src/FakerDotNet/Data/DemographicData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;

namespace FakerDotNet.Data
{
internal static class DemographicData
{
public static readonly IEnumerable<string> Races = new[]
{
"American Indian or Alaska Native",
"Asian",
"Black or African American",
"Native Hawaiian or Other Pacific Islander",
"White"
};

public static readonly IEnumerable<string> Sexes = new[]
{
"Male",
"Female"
};

public static readonly IEnumerable<string> Demonyms = new[]
{
"Afghan","Albanian","Algerian","American","Andorran","Angolan","Argentine","Armenian","Aromanian","Aruban","Australian","Austrian","Azerbaijani","Bahamian","Bahraini","Bangladeshi","Barbadian","Basotho","Basque","Belarusian","Belgian","Belizean","Bermudian","Bissau-Guinean","Boer","Bosniak","Brazilian","Breton","Briton","British Virgin Islander","Bruneian","Bulgarian","Burkinabè","Burundian","Cambodian","Cameroonian","Canadian","Catalan","Cape Verdean","Chadian","Chilean","Chinese","Colombian","Comorian","Congolese","Croatian","Cuban","Cypriot","Czech","Dane","Dominican","Dutch","East Timorese","Ecuadorian","Egyptian","Emirati","English","Eritrean","Estonian","Ethiopian","Falkland Islander","Faroese","Finn","Fijian","Filipino","French","Georgian","German","Ghanaian","Gibraltar","Greek","Grenadian","Guatemalan","French Guianan","Guinean","Guyanese","Haitian","Honduran","Hong Konger","Hungarian","Icelander","I-Kiribati","Indian","Indonesian","Iranian","Iraqi","Irish","Israeli","Italian","Ivoirian","Jamaican","Japanese","Jordanian","Kazakh","Kenyan","Korean","Kosovar","Kurd","Kuwaiti","Kyrgyz","Lao","Latvian","Lebanese","Liberian","Libyan","Liechtensteiner","Lithuanian","Luxembourger","Macanese","Macedonian","Malagasy","Malaysian","Malawian","Maldivian","Malian","Maltese","Manx","Mauritian","Mexican","Moldovan","Moroccan","Mongolian","Montenegrin","Namibian","Nepalese","New Zealander","Nicaraguan","Nigerien","Nigerian","Norwegian","Pakistani","Palauan","Palestinian","Panamanian","Papua New Guinean","Paraguayan","Peruvian","Pole","Portuguese","Puerto Rican","Quebecer","Romanian","Russian","Rwandan","Salvadoran","São Toméan","Saudi","Scottish","Senegalese","Serb","Sierra Leonean","Singaporean","Sindhian","Slovak","Slovene","Somali","Somalilander","South African","Spaniard","Sri Lankan","St Lucian","Sudanese","Surinamese","Swede","Swiss","Syriac","Syrian","Tajik","Taiwanese","Tanzanian","Thai","Tibetan","Tobagonian","Trinidadian","Tunisian","Turk","Tuvaluan","Ugandan","Ukrainian","Uruguayan","Uzbek","Vanuatuan","Venezuelan","Vietnamese","Welsh","Yemeni","Zambian","Zimbabwean"
};

public static readonly IEnumerable<string> EducationalAttainments = new[]
{
"No schooling completed",
"Nursery school",
"Kindergarten",
"Grade 1 though 11",
"12th grade - No Diploma",
"Regular high school diploma",
"GED or alternative credential",
"Some college",
"Associate's degree",
"Bachelor's degree",
"Master's degree",
"Professional degree",
"Doctorate degree"
};

public static readonly IEnumerable<string> MaritalStatuses = new[]
{
"Married",
"Widowed",
"Divorced",
"Separated",
"Never married"
};
}
}
1 change: 1 addition & 0 deletions src/FakerDotNet/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class Faker
public static ICommerceFaker Commerce { get; } = Container.Commerce;
public static ICompanyFaker Company { get; } = Container.Company;
public static IDateFaker Date { get; } = Container.Date;
public static IDemographicFaker Demographic { get; } = Container.Demographic;
public static IDragonBallFaker DragonBall { get; } = Container.DragonBall;
public static IEducatorFaker Educator { get; } = Container.Educator;
public static IFakeFaker Fake { get; } = Container.Fake;
Expand Down
3 changes: 3 additions & 0 deletions src/FakerDotNet/FakerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal interface IFakerContainer
ICommerceFaker Commerce { get; }
ICompanyFaker Company { get; }
IDateFaker Date { get; }
IDemographicFaker Demographic { get; }
IDragonBallFaker DragonBall { get; }
IEducatorFaker Educator { get; }
IFakeFaker Fake { get; }
Expand Down Expand Up @@ -76,6 +77,7 @@ public FakerContainer()
Commerce = new CommerceFaker(this);
Company = new CompanyFaker(this);
Date = new DateFaker();
Demographic = new DemographicFaker(this);
DragonBall = new DragonBallFaker(this);
Educator = new EducatorFaker(this);
Fake = new FakeFaker(this);
Expand Down Expand Up @@ -129,6 +131,7 @@ public FakerContainer()
public ICommerceFaker Commerce { get; }
public ICompanyFaker Company { get; }
public IDateFaker Date { get; }
public IDemographicFaker Demographic { get; }
public IDragonBallFaker DragonBall { get; }
public IEducatorFaker Educator { get; }
public IFakeFaker Fake { get; }
Expand Down
63 changes: 63 additions & 0 deletions src/FakerDotNet/Fakers/DemographicFaker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using FakerDotNet.Data;

namespace FakerDotNet.Fakers
{
public interface IDemographicFaker
{
string Race();
string EducationalAttainment();
string Demonym();
string MaritalStatus();
string Sex();
string Height(UnitType unitType = UnitType.Metric);
}

internal class DemographicFaker : IDemographicFaker
{
private readonly IFakerContainer _fakerContainer;

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

public string Race()
{
return _fakerContainer.Random.Element(DemographicData.Races);
}

public string EducationalAttainment()
{
return _fakerContainer.Random.Element(DemographicData.EducationalAttainments);
}

public string Demonym()
{
return _fakerContainer.Random.Element(DemographicData.Demonyms);
}

public string MaritalStatus()
{
return _fakerContainer.Random.Element(DemographicData.MaritalStatuses);
}

public string Sex()
{
return _fakerContainer.Random.Element(DemographicData.Sexes);
}

public string Height(UnitType unitType = UnitType.Metric)
{
switch (unitType)
{
case UnitType.Imperial:
var inches = _fakerContainer.Number.Between(57, 86);
return $"{inches / 12:#} ft, {inches % 12} in";

default:
return _fakerContainer.Number.Between(1.45, 2.13).ToString("#.##");
}
}
}
}
8 changes: 8 additions & 0 deletions src/FakerDotNet/UnitType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FakerDotNet
{
public enum UnitType
{
Metric,
Imperial
}
}
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public void Date_returns_IDateFaker()
Assert.IsInstanceOf<IDateFaker>(_fakerContainer.Date);
}

[Test]
public void Demographic_returns_IDemographicFaker()
{
Assert.IsInstanceOf<IDemographicFaker>(_fakerContainer.Demographic);
}

[Test]
public void DragonBall_returns_IDragonBallFaker()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/FakerDotNet.Tests/FakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public void Date_returns_IDateFaker()
Assert.IsInstanceOf<IDateFaker>(Faker.Date);
}

[Test]
public void Demographic_returns_IDemographicFaker()
{
Assert.IsInstanceOf<IDemographicFaker>(Faker.Demographic);
}

[Test]
public void DragonBall_returns_IDragonBallFaker()
{
Expand Down
85 changes: 85 additions & 0 deletions tests/FakerDotNet.Tests/Fakers/DemographicFakerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using FakeItEasy;
using FakerDotNet.Data;
using FakerDotNet.Fakers;
using NUnit.Framework;

namespace FakerDotNet.Tests.Fakers
{
[TestFixture]
[Parallelizable]
public class DemographicFakerTests
{
[SetUp]
public void SetUp()
{
_fakerContainer = A.Fake<IFakerContainer>();
_demographicFaker = new DemographicFaker(_fakerContainer);
}

private IFakerContainer _fakerContainer;
private IDemographicFaker _demographicFaker;

[Test]
public void Race_returns_a_race()
{
A.CallTo(() => _fakerContainer.Random.Element(DemographicData.Races))
.Returns("Native Hawaiian or Other Pacific Islander");

Assert.AreEqual("Native Hawaiian or Other Pacific Islander", _demographicFaker.Race());
}

[Test]
public void EducationalAttainment_returns_an_educational_attainment()
{
A.CallTo(() => _fakerContainer.Random.Element(DemographicData.EducationalAttainments))
.Returns("GED or alternative credential");

Assert.AreEqual("GED or alternative credential", _demographicFaker.EducationalAttainment());
}

[Test]
public void Demonym_returns_a_demonym()
{
A.CallTo(() => _fakerContainer.Random.Element(DemographicData.Demonyms))
.Returns("Panamanian");

Assert.AreEqual("Panamanian", _demographicFaker.Demonym());
}

[Test]
public void MaritalStatus_returns_a_marital_status()
{
A.CallTo(() => _fakerContainer.Random.Element(DemographicData.MaritalStatuses))
.Returns("Widowed");

Assert.AreEqual("Widowed", _demographicFaker.MaritalStatus());
}

[Test]
public void Sex_returns_a_sex()
{
A.CallTo(() => _fakerContainer.Random.Element(DemographicData.Sexes))
.Returns("Female");

Assert.AreEqual("Female", _demographicFaker.Sex());
}

[Test]
public void Height_returns_a_height_in_metric_units()
{
A.CallTo(() => _fakerContainer.Number.Between(1.45, 2.13))
.Returns(1.613243);

Assert.AreEqual("1.61", _demographicFaker.Height());
}

[Test]
public void Height_returns_a_height_in_imperial_units_when_specified()
{
A.CallTo(() => _fakerContainer.Number.Between(57, 86))
.Returns(74);

Assert.AreEqual("6 ft, 2 in", _demographicFaker.Height(UnitType.Imperial));
}
}
}

0 comments on commit bf82d0d

Please sign in to comment.