-
-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UseConstructor(), SkipConstructor() and fixing StrictMode() error #338
Open
Basyras
wants to merge
2
commits into
bchavez:master
Choose a base branch
from
Basyras:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Bogus.DataSets; | ||
using Bogus.Extensions; | ||
using Bogus.Tests.Models; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
|
||
namespace Bogus.Tests | ||
{ | ||
public class FakerTests | ||
{ | ||
[Fact] | ||
public void SkipConstructor_Default_StrictMode_Satisfied() | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.SkipConstructor() | ||
.RuleFor(x => x.Obj, x => null) | ||
.RuleFor(x => x.Obj2, x => null) | ||
.RuleFor(x => x.Obj3, x => null) | ||
.Generate(1); | ||
} | ||
|
||
[Fact] | ||
public void SkipConstructor_WithRule_Default_StrictMode_Not_Satisfied() | ||
{ | ||
Assert.Throws<ValidationException>(() => | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.SkipConstructor() | ||
.RuleFor(x => x.Obj, x => null) | ||
.Generate(1); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void SkipConstructor_WithoutRules_Default_StrictMode_Not_Satisfied() | ||
{ | ||
Assert.Throws<ValidationException>(() => | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.SkipConstructor() | ||
.Generate(1); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Not_Skipping_Constructor_Without_Parameterless_Ctor() | ||
{ | ||
Assert.Throws<NoParameterlessCtorException<NoParameterlessCtorClass>>(() => | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.Generate(1); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Skipping_Constructor_Overiding_StrictMode() | ||
{ | ||
|
||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.SkipConstructor() | ||
.StrictMode(false); | ||
|
||
Assert.Contains(faker.StrictModes, (x => x.Value == false)); | ||
_ = faker.Generate(1); | ||
|
||
|
||
|
||
Assert.Throws<ValidationException>(() => | ||
{ | ||
faker = new Faker<NoParameterlessCtorClass>() | ||
.SkipConstructor() | ||
.StrictMode(true); | ||
|
||
Assert.Contains(faker.StrictModes, (x => x.Value == true)); | ||
_ = faker.Generate(1); | ||
|
||
}); | ||
|
||
} | ||
|
||
|
||
[Fact] | ||
public void UseConstructor() | ||
{ | ||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.UseConstructor(x => new NoParameterlessCtorClass(new Order(), 5)); | ||
faker.Generate(1); | ||
|
||
var faker2 = new Faker<Order>() | ||
.UseConstructor(x => new Order() { OrderId = 68 }); | ||
var order = faker2.Generate(1)[0]; | ||
|
||
Assert.Equal(68, order.OrderId); | ||
} | ||
|
||
|
||
[Fact] | ||
public void UseConstructor_WithRule_Default_StrictMode_Not_Satisfied() | ||
{ | ||
Assert.Throws<ValidationException>(() => | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.UseConstructor(x => new NoParameterlessCtorClass(default, default)) | ||
.StrictMode(true) | ||
.RuleFor(x => x.Obj, x => null) | ||
.Generate(1); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void UseConstructor_WithoutRules_Default_StrictMode_Not_Satisfied() | ||
{ | ||
Assert.Throws<ValidationException>(() => | ||
{ | ||
_ = new Faker<NoParameterlessCtorClass>() | ||
.StrictMode(true) | ||
.UseConstructor(x => new NoParameterlessCtorClass(default, default)) | ||
.Generate(1); | ||
}); | ||
} | ||
|
||
|
||
[Fact] | ||
public void UseContructor_After_SkipConstructor() | ||
{ | ||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.StrictMode(false) | ||
.UseConstructor(x => throw new Exception("Use Constructor was not overriden")) | ||
.SkipConstructor(); | ||
_ = faker.Generate(1); | ||
|
||
var faker2 = new Faker<Order>() | ||
.StrictMode(false) | ||
.UseConstructor(x => throw new Exception("Use Constructor was not overriden")) | ||
.SkipConstructor(); | ||
|
||
_ = faker2.Generate(1); | ||
|
||
} | ||
|
||
[Fact] | ||
public void SkipConstructor_After_UseContructor() | ||
{ | ||
|
||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.StrictMode(false) | ||
.SkipConstructor() | ||
.UseConstructor(x => new NoParameterlessCtorClass(new Order(), 5)); | ||
var instance = faker.Generate(1)[0]; | ||
Assert.Equal(5, instance.Obj2); | ||
|
||
var faker2 = new Faker<Order>() | ||
.StrictMode(false) | ||
.SkipConstructor() | ||
.UseConstructor(x => new Order() { OrderId = 68 }); | ||
var order = faker2.Generate(1)[0]; | ||
Assert.Equal(68, order.OrderId); | ||
|
||
} | ||
|
||
|
||
[Fact] | ||
public void UseParameterlessConstructor_Hidden_Ctor() | ||
{ | ||
Assert.Throws<NoParameterlessCtorException<NoParameterlessCtorClass>>(() => | ||
{ | ||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.UseConstructor(true); | ||
_ = faker.Generate(1); | ||
}); | ||
|
||
Assert.Throws<NoParameterlessCtorException<Order>>(() => | ||
{ | ||
var faker2 = new Faker<Order>() | ||
.UseConstructor(true); | ||
_ = faker2.Generate(1); | ||
}); | ||
|
||
var faker2 = new Faker<HiddenParameterlessCtorClass>() | ||
.UseConstructor(true); | ||
_ = faker2.Generate(1); | ||
} | ||
|
||
[Fact] | ||
public void UseParameterlessConstructor_Public_Ctor() | ||
{ | ||
Assert.Throws<NoParameterlessCtorException<NoParameterlessCtorClass>>(() => | ||
{ | ||
var faker = new Faker<NoParameterlessCtorClass>() | ||
.UseConstructor(false); | ||
_ = faker.Generate(1); | ||
}); | ||
|
||
var faker2 = new Faker<Order>() | ||
.UseConstructor(false); | ||
_ = faker2.Generate(1); | ||
|
||
Assert.Throws<NoParameterlessCtorException<HiddenParameterlessCtorClass>>(() => | ||
{ | ||
var faker3 = new Faker<HiddenParameterlessCtorClass>() | ||
.UseConstructor(false); | ||
_ = faker3.Generate(1); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void GenerateDefaultConstructor() | ||
{ | ||
|
||
Assert.Throws<NoParameterlessCtorException<NoParameterlessCtorClass>>(() => | ||
{ | ||
var faker = new Faker<NoParameterlessCtorClass>(); | ||
_ = faker.Generate(1); | ||
}); | ||
|
||
var faker2 = new Faker<Order>(); | ||
_ = faker2.Generate(1); | ||
|
||
Assert.Throws<NoParameterlessCtorException<HiddenParameterlessCtorClass>>(() => | ||
{ | ||
|
||
var faker3 = new Faker<HiddenParameterlessCtorClass>(); | ||
_ = faker3.Generate(1); | ||
}); | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
public class HiddenParameterlessCtorClass | ||
{ | ||
|
||
|
||
protected HiddenParameterlessCtorClass() | ||
{ | ||
|
||
} | ||
|
||
public object Obj { get; } | ||
public object Obj2 { get; set; } | ||
public object Obj3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
public class NoParameterlessCtorClass | ||
{ | ||
public NoParameterlessCtorClass(object obj, object obj2) | ||
{ | ||
Obj = obj; | ||
Obj2 = obj2; | ||
} | ||
|
||
public object Obj { get; } | ||
public object Obj2 { get; set; } | ||
public object Obj3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
public class ParameterlessCtorClass | ||
{ | ||
|
||
|
||
public ParameterlessCtorClass() | ||
{ | ||
|
||
} | ||
|
||
public object Obj { get; } | ||
public object Obj2 { get; set; } | ||
public object Obj3; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is debatable if using both
UseConstructor
andSkipConstructor
should be prohibited or not although it does not break anything.