Skip to content

Commit

Permalink
Added check for duplicate keys and a few unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Feb 26, 2018
1 parent d62a497 commit c139108
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
37 changes: 24 additions & 13 deletions src/Thinktecture.Runtime.Extensions/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,34 @@ private static Dictionary<TKey, TEnum> GetItems()
if (fields.Any())
fields.First().GetValue(null);

return fields.Where(f => f.FieldType == _type)
.Select(f =>
{
if (!f.IsInitOnly)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" must be read-only.");
var items = fields.Where(f => f.FieldType == _type)
.Select(f =>
{
if (!f.IsInitOnly)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" must be read-only.");
var item = (TEnum)f.GetValue(null);
var item = (TEnum)f.GetValue(null);
if (item == null)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" is not initialized.");
if (item == null)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" is not initialized.");
if (!item.IsValid)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" is not valid.");
if (!item.IsValid)
throw new Exception($"The field \"{f.Name}\" of enumeration type \"{_type.FullName}\" is not valid.");
return item;
})
.ToDictionary(i => i.Key, KeyEqualityComparer);
return item;
});

var lookup = new Dictionary<TKey, TEnum>(KeyEqualityComparer);

foreach (var item in items)
{
if (lookup.ContainsKey(item.Key))
throw new ArgumentException($"The enumeration of type \"{_type.FullName}\" has multiple items with the key \"{item.Key}\".");

lookup.Add(item.Key, item);
}

return lookup;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Should_return_false_if_item_is_null()
public void Should_return_false_if_item_is_of_different_type()
{
// ReSharper disable once SuspiciousTypeConversion.Global
TestEnum.Item1.Equals(TestEnumWithNonDefaultComparer.Item1).Should().BeFalse();
TestEnum.Item1.Equals(TestEnumWithNonDefaultComparer.Item).Should().BeFalse();
}

[Fact]
Expand All @@ -36,5 +36,11 @@ public void Should_return_false_if_both_items_are_invalid_and_have_different_key
{
TestEnum.Get("unknown").Equals(TestEnum.Get("other")).Should().BeFalse();
}

[Fact]
public void Should_return_false_if_both_items_are_invalid_and_have_keys_that_differ_in_casing_if_comparer_honors_casing()
{
TestEnumWithNonDefaultComparer.Get("Item").Equals(TestEnumWithNonDefaultComparer.Get("item")).Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using FluentAssertions;
using Thinktecture.TestEnums;
using Xunit;

namespace Thinktecture.EnumTests
{
public class Initialization
{
[Fact]
public void Should_throw_if_enum_has_duplicate_key()
{
Action action = () => EnumWithDuplicateKey.GetAll();
action.Should().Throw<ArgumentException>()
.WithMessage($"The enumeration of type \"{typeof(EnumWithDuplicateKey).FullName}\" has multiple items with the key \"item\".");
}

[Fact]
public void Should_not_throw_if_enum_has_2_keys_that_differs_in_casing_only_if_comparer_honors_casing()
{
Action action = () => TestEnumWithNonDefaultComparer.GetAll();
action.Should().NotThrow<Exception>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Thinktecture.TestEnums
{
public class EnumWithDuplicateKey : Enum<EnumWithDuplicateKey>
{
public static readonly EnumWithDuplicateKey Item = new EnumWithDuplicateKey("Item");
public static readonly EnumWithDuplicateKey Duplicate = new EnumWithDuplicateKey("item");

public EnumWithDuplicateKey(string key)
: base(key)
{
}

protected override EnumWithDuplicateKey CreateInvalid(string key)
{
return new EnumWithDuplicateKey(key);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace Thinktecture.TestEnums
{
public class TestEnumWithNonDefaultComparer : Enum<TestEnumWithNonDefaultComparer>
{
public static readonly TestEnumWithNonDefaultComparer Item1 = new TestEnumWithNonDefaultComparer("item1");
public static readonly TestEnumWithNonDefaultComparer Item = new TestEnumWithNonDefaultComparer("item");
public static readonly TestEnumWithNonDefaultComparer AnotherItem = new TestEnumWithNonDefaultComparer("Item");

static TestEnumWithNonDefaultComparer()
{
Expand Down

0 comments on commit c139108

Please sign in to comment.