Skip to content

Commit

Permalink
The result of custom implementation of CreateInvalidItem is being che…
Browse files Browse the repository at this point in the history
…ck so it doesn't return a key equals to valid key.
  • Loading branch information
PawelGerr committed Feb 10, 2021
1 parent 54ce901 commit 2bca454
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,14 @@ private void GenerateGet(bool needCreateInvalidImplementation)
_sb.Append(@"
if (item.IsValid)
throw new Exception(""The implementation of method 'CreateInvalidItem' must return an instance with property 'IsValid' equals to 'false'."");");

if (!needCreateInvalidImplementation)
{
_sb.Append($@"
if ({_state.EnumType}.TryGet(item.{_state.KeyPropertyName}, out _))
throw new Exception(""The implementation of method 'CreateInvalidItem' must not return an instance with property '{_state.KeyPropertyName}' equals to one of a valid item."");");
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,9 @@ string IEnum<string>.GetKey()
if (item.IsValid)
throw new Exception(""The implementation of method 'CreateInvalidItem' must return an instance with property 'IsValid' equals to 'false'."");
if (Thinktecture.Tests.TestEnum.TryGet(item.Name, out _))
throw new Exception(""The implementation of method 'CreateInvalidItem' must not return an instance with property 'Name' equals to one of a valid item."");
}
return item;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Thinktecture.Runtime.Tests.TestEnums
{
public partial class TestEnumWithInvalidCreateInvalidItem : IValidatableEnum<int>
{
public const int INVALID_KEY_FOR_TESTING_KEY_REUSE = 0;
public const int INVALID_KEY_FOR_TESTING_ISVALID_TRUE = -1;

public static readonly TestEnumWithInvalidCreateInvalidItem Item1 = new(1);

private static TestEnumWithInvalidCreateInvalidItem CreateInvalidItem(int key)
{
if (key == INVALID_KEY_FOR_TESTING_KEY_REUSE)
return new(Item1.Key, false);

if (key == INVALID_KEY_FOR_TESTING_ISVALID_TRUE)
return new(key, true);

return new(key, false);
}
}
}
14 changes: 14 additions & 0 deletions test/Thinktecture.Runtime.Extensions.Tests/EnumTests/Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public void Should_return_invalid_item_if_enum_doesnt_have_item_with_provided_ke
extendedItem.Key.Should().Be("unknown");
}

[Fact]
public void Should_throw_if_CreateInvalidItem_uses_key_of_valid_item()
{
Action action = () => TestEnumWithInvalidCreateInvalidItem.Get(TestEnumWithInvalidCreateInvalidItem.INVALID_KEY_FOR_TESTING_KEY_REUSE);
action.Should().Throw<Exception>().WithMessage("The implementation of method 'CreateInvalidItem' must not return an instance with property 'Key' equals to one of a valid item.");
}

[Fact]
public void Should_throw_if_CreateInvalidItem_isValid_is_true()
{
Action action = () => TestEnumWithInvalidCreateInvalidItem.Get(TestEnumWithInvalidCreateInvalidItem.INVALID_KEY_FOR_TESTING_ISVALID_TRUE);
action.Should().Throw<Exception>().WithMessage("The implementation of method 'CreateInvalidItem' must return an instance with property 'IsValid' equals to 'false'.");
}

[Fact]
public void Should_throw_if_custom_validation_throws()
{
Expand Down

0 comments on commit 2bca454

Please sign in to comment.