Navigation Menu

Skip to content

Commit

Permalink
Implement Exclude on Randomizer.Enum #7
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Nov 2, 2015
1 parent 6fb31e4 commit 994a224
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Expand Up @@ -7,8 +7,9 @@ Contributing
Here are some helpful guidelines to keep in mind when contributing. While following them isn't absolutely required, it does help everyone to accept your pull-requests with maximum awesomeness.

* :heavy_check_mark: **CONSIDER** adding a unit test if your PR resolves an issue or adds features.
* :heavy_check_mark: **DO** add XML comment documentation to new API calls along parameter documentation.
* :heavy_check_mark: **DO** keep pull requests small so they can be easily reviewed.
* :heavy_check_mark: **DO** make sure unit tests pass.
* :x: **AVOID** breaking the continuous integration build.
* :x: **AVOID** Breaking compatibility with **faker.js** locale data.
* :x: **AVOID** Adding new locales to Bogus. New locales should be added upstream to **faker.js**. [See this wiki page for creating locales](https://github.com/bchavez/Bogus/wiki/Creating-Locales).
* :x: **AVOID** Adding new locales to Bogus. New locales should be added upstream to **faker.js**. [See this wiki page for creating locales](https://github.com/bchavez/Bogus/wiki/Creating-Locales).
1 change: 1 addition & 0 deletions Source/Bogus.Tests/Bogus.Tests.csproj
Expand Up @@ -73,6 +73,7 @@
<Compile Include="NameTests.cs" />
<Compile Include="PhoneNumbersTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RandomizerTest.cs" />
<Compile Include="SeededTest.cs" />
<Compile Include="StrictModeTests.cs" />
</ItemGroup>
Expand Down
53 changes: 53 additions & 0 deletions Source/Bogus.Tests/RandomizerTest.cs
@@ -0,0 +1,53 @@
using System;
using FluentAssertions;
using NUnit.Framework;

namespace Bogus.Tests
{
[TestFixture]
public class RandomizerTest : SeededTest
{
private Randomizer r;

[TestFixtureSetUp]
public void BeforeRunningTestSession()
{
r = new Randomizer();
}

public enum Foo
{
ExcludeMe,
A,B,C,D
}

[Test]
public void pick_an_enum()
{
var f = r.Enum<Foo>();
f.Should().Be(Foo.C);
}

[Test]
public void exclude_an_enum()
{
//seeded value of 14 gets "ExcludeMe", ensure exclude works.
Randomizer.Seed = new Random(14);
var f = r.Enum( exclude: Foo.ExcludeMe);
f.ToString().Dump();

f.Should().NotBe(Foo.ExcludeMe);
}

[Test]
public void exclude_all_throws_an_error()
{
Action act = () => r.Enum(Foo.ExcludeMe, Foo.A, Foo.B, Foo.C, Foo.D);

act.ShouldThrow<ArgumentException>();
}
}



}
2 changes: 1 addition & 1 deletion Source/Bogus/Bogus.csproj
Expand Up @@ -66,7 +66,7 @@
<Compile Include="Person.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PropertyName.cs" />
<Compile Include="Random.cs" />
<Compile Include="Randomizer.cs" />
<Compile Include="Tokenizer.cs" />
<Compile Include="Utils.cs" />
</ItemGroup>
Expand Down
28 changes: 21 additions & 7 deletions Source/Bogus/Random.cs → Source/Bogus/Randomizer.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -123,7 +123,7 @@ public string Replace(string format)
}
return c;
})
.ToArray();
.ToArray();

return new string(chars);
}
Expand All @@ -132,13 +132,27 @@ public string Replace(string format)
/// Picks a random Enum of T. Works only with Enums.
/// </summary>
/// <typeparam name="T">Must be an Enum</typeparam>
public T Enum<T>() where T : struct
/// <param name="exclude">Exclude enum values from being returned</param>
public T Enum<T>(params T[] exclude) where T : struct
{
var e = typeof(T);
if( !e.IsEnum )
throw new ArgumentException("When calling PickRandom<T>() with no parameters T must be an enum.");
if (!e.IsEnum)
throw new ArgumentException("When calling Enum<T>() with no parameters T must be an enum.");

var selection = System.Enum.GetNames(e);

if( exclude.Any() )
{
var excluded = exclude.Select(ex => System.Enum.GetName(e, ex));
selection = selection.Except(excluded).ToArray();
}

if ( !selection.Any() )
{
throw new ArgumentException("There are no values after exclusion choose from.");
}

var val = this.ArrayElement(System.Enum.GetNames(e));
var val = this.ArrayElement(selection);

T picked;
System.Enum.TryParse(val, out picked);
Expand All @@ -160,4 +174,4 @@ public IEnumerable<T> Shuffle<T>(IEnumerable<T> source)
}
}
}
}
}

0 comments on commit 994a224

Please sign in to comment.