Skip to content

Commit

Permalink
Fixes #218 - Fixed bug that prevented global static `Faker.DefaultStr…
Browse files Browse the repository at this point in the history
…ictMode` from working.
  • Loading branch information
bchavez committed May 2, 2019
1 parent 9b5b5cf commit 90922aa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
6 changes: 4 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## v26.0.3 - /master current
Release Date: UNRELEASED
## v27.0.1
Release Date: 2019-05-02

* Issue 218: Fixed bug that prevented global static `Faker.DefaultStrictMode` from working.
* Issue 210: Added `Randomizer.Utf16String` that generates technically valid Unicode with paired high/low surrogates.
* Added `placeholder.com` image service.

Expand Down
56 changes: 56 additions & 0 deletions Source/Bogus.Tests/GitHubIssues/Issue218.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Bogus.Tests.Models;
using FluentAssertions;
using Xunit;

namespace Bogus.Tests.GitHubIssues
{
public class Issue218 : SeededTest
{
[Fact]
public void global_strict_mode_should_throw_on_incomplete_rules()
{
Faker.DefaultStrictMode = true;

var orderFaker = new Faker<Order>()
.RuleFor(x => x.Item, f => f.Commerce.Product());

Action gen = () => orderFaker.Generate();

gen.ShouldThrow<ValidationException>();

Faker.DefaultStrictMode = false;
}

[Fact]
public void local_struct_mode_faker_t_scope_should_throw_on_incomplete_rules()
{
Faker.DefaultStrictMode = false;

var orderFaker = new Faker<Order>()
.StrictMode(true)
.RuleFor(x => x.Item, f => f.Commerce.Product());

Action gen = () => orderFaker.Generate();

gen.ShouldThrow<ValidationException>();
}


[Fact]
public void local_strict_mode_should_take_precedence_always()
{
Faker.DefaultStrictMode = true;

var orderFaker = new Faker<Order>()
.StrictMode(false)
.RuleFor(x => x.Item, f => f.Commerce.Product());

Action gen = () => orderFaker.Generate();

gen.ShouldNotThrow<ValidationException>();

Faker.DefaultStrictMode = false;
}
}
}
9 changes: 7 additions & 2 deletions Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,13 @@ private ValidationResult ValidateInternal(string[] ruleSets)
var binderPropsOrFieldsOfT = this.TypeProperties.Keys;
foreach( var rule in ruleSets )
{
var strictMode = Faker.DefaultStrictMode;
this.StrictModes.TryGetValue(rule, out strictMode);
if( this.StrictModes.TryGetValue(rule, out var strictMode) )
{
}
else
{
strictMode = Faker.DefaultStrictMode;
}

//If strictMode is not enabled, skip and move on to the next ruleSet.
if( !strictMode ) continue;
Expand Down

0 comments on commit 90922aa

Please sign in to comment.