From d72391df1f16270fff9c16ac7a25ef50a9b836a8 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Fri, 26 Jul 2019 16:34:02 +0300 Subject: [PATCH] Move custom equality comparers to tests to increase coverage --- CliFx.Tests/CommandInputParserTests.cs | 1 + CliFx.Tests/CommandSchemaResolverTests.cs | 1 + .../Internal}/CommandInputEqualityComparer.cs | 9 ++--- .../CommandOptionInputEqualityComparer.cs | 9 ++--- .../CommandOptionSchemaEqualityComparer.cs | 12 ++---- .../CommandSchemaEqualityComparer.cs | 11 ++---- CliFx/Internal/HashCodeBuilder.cs | 38 ------------------- 7 files changed, 14 insertions(+), 67 deletions(-) rename {CliFx/Models => CliFx.Tests/Internal}/CommandInputEqualityComparer.cs (73%) rename {CliFx/Models => CliFx.Tests/Internal}/CommandOptionInputEqualityComparer.cs (75%) rename {CliFx/Models => CliFx.Tests/Internal}/CommandOptionSchemaEqualityComparer.cs (71%) rename {CliFx/Models => CliFx.Tests/Internal}/CommandSchemaEqualityComparer.cs (71%) delete mode 100644 CliFx/Internal/HashCodeBuilder.cs diff --git a/CliFx.Tests/CommandInputParserTests.cs b/CliFx.Tests/CommandInputParserTests.cs index cac7d5d..17a3e24 100644 --- a/CliFx.Tests/CommandInputParserTests.cs +++ b/CliFx.Tests/CommandInputParserTests.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using CliFx.Models; using CliFx.Services; +using CliFx.Tests.Internal; using NUnit.Framework; namespace CliFx.Tests diff --git a/CliFx.Tests/CommandSchemaResolverTests.cs b/CliFx.Tests/CommandSchemaResolverTests.cs index c519f16..91904a1 100644 --- a/CliFx.Tests/CommandSchemaResolverTests.cs +++ b/CliFx.Tests/CommandSchemaResolverTests.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using CliFx.Attributes; using CliFx.Models; using CliFx.Services; +using CliFx.Tests.Internal; using NUnit.Framework; namespace CliFx.Tests diff --git a/CliFx/Models/CommandInputEqualityComparer.cs b/CliFx.Tests/Internal/CommandInputEqualityComparer.cs similarity index 73% rename from CliFx/Models/CommandInputEqualityComparer.cs rename to CliFx.Tests/Internal/CommandInputEqualityComparer.cs index 9f0f550..88eca83 100644 --- a/CliFx/Models/CommandInputEqualityComparer.cs +++ b/CliFx.Tests/Internal/CommandInputEqualityComparer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using CliFx.Internal; +using CliFx.Models; -namespace CliFx.Models +namespace CliFx.Tests.Internal { public partial class CommandInputEqualityComparer : IEqualityComparer { @@ -21,10 +21,7 @@ namespace CliFx.Models } /// - public int GetHashCode(CommandInput obj) => new HashCodeBuilder() - .Add(obj.CommandName, StringComparer.OrdinalIgnoreCase) - .AddMany(obj.Options, CommandOptionInputEqualityComparer.Instance) - .Build(); + public int GetHashCode(CommandInput obj) => throw new NotSupportedException(); } public partial class CommandInputEqualityComparer diff --git a/CliFx/Models/CommandOptionInputEqualityComparer.cs b/CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs similarity index 75% rename from CliFx/Models/CommandOptionInputEqualityComparer.cs rename to CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs index ae34f04..41a6f92 100644 --- a/CliFx/Models/CommandOptionInputEqualityComparer.cs +++ b/CliFx.Tests/Internal/CommandOptionInputEqualityComparer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using CliFx.Internal; +using CliFx.Models; -namespace CliFx.Models +namespace CliFx.Tests.Internal { public partial class CommandOptionInputEqualityComparer : IEqualityComparer { @@ -21,10 +21,7 @@ namespace CliFx.Models } /// - public int GetHashCode(CommandOptionInput obj) => new HashCodeBuilder() - .Add(obj.Alias, StringComparer.OrdinalIgnoreCase) - .AddMany(obj.Values, StringComparer.Ordinal) - .Build(); + public int GetHashCode(CommandOptionInput obj) => throw new NotSupportedException(); } public partial class CommandOptionInputEqualityComparer diff --git a/CliFx/Models/CommandOptionSchemaEqualityComparer.cs b/CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs similarity index 71% rename from CliFx/Models/CommandOptionSchemaEqualityComparer.cs rename to CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs index c889957..bb3a70f 100644 --- a/CliFx/Models/CommandOptionSchemaEqualityComparer.cs +++ b/CliFx.Tests/Internal/CommandOptionSchemaEqualityComparer.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; -using CliFx.Internal; +using CliFx.Models; -namespace CliFx.Models +namespace CliFx.Tests.Internal { public partial class CommandOptionSchemaEqualityComparer : IEqualityComparer { @@ -23,13 +23,7 @@ namespace CliFx.Models } /// - public int GetHashCode(CommandOptionSchema obj) => new HashCodeBuilder() - .Add(obj.Property) - .Add(obj.Name, StringComparer.OrdinalIgnoreCase) - .Add(obj.ShortName) - .Add(obj.GroupName, StringComparer.OrdinalIgnoreCase) - .Add(obj.Description, StringComparer.Ordinal) - .Build(); + public int GetHashCode(CommandOptionSchema obj) => throw new NotSupportedException(); } public partial class CommandOptionSchemaEqualityComparer diff --git a/CliFx/Models/CommandSchemaEqualityComparer.cs b/CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs similarity index 71% rename from CliFx/Models/CommandSchemaEqualityComparer.cs rename to CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs index 943beb6..fbc3a60 100644 --- a/CliFx/Models/CommandSchemaEqualityComparer.cs +++ b/CliFx.Tests/Internal/CommandSchemaEqualityComparer.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; -using CliFx.Internal; +using CliFx.Models; -namespace CliFx.Models +namespace CliFx.Tests.Internal { public partial class CommandSchemaEqualityComparer : IEqualityComparer { @@ -23,12 +23,7 @@ namespace CliFx.Models } /// - public int GetHashCode(CommandSchema obj) => new HashCodeBuilder() - .Add(obj.Type) - .Add(obj.Name, StringComparer.OrdinalIgnoreCase) - .Add(obj.Description, StringComparer.Ordinal) - .AddMany(obj.Options, CommandOptionSchemaEqualityComparer.Instance) - .Build(); + public int GetHashCode(CommandSchema obj) => throw new NotSupportedException(); } public partial class CommandSchemaEqualityComparer diff --git a/CliFx/Internal/HashCodeBuilder.cs b/CliFx/Internal/HashCodeBuilder.cs deleted file mode 100644 index 6395346..0000000 --- a/CliFx/Internal/HashCodeBuilder.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Collections.Generic; -using System.Linq; - -namespace CliFx.Internal -{ - internal class HashCodeBuilder - { - private int _code = 17; - - public HashCodeBuilder Add(int hashCode) - { - unchecked - { - _code = _code * 23 + hashCode; - } - - return this; - } - - public HashCodeBuilder Add(IEnumerable hashCodes) - { - foreach (var hashCode in hashCodes) - Add(hashCode); - - return this; - } - - public HashCodeBuilder Add(T obj, IEqualityComparer comparer) => Add(comparer.GetHashCode(obj)); - - public HashCodeBuilder Add(T obj) => Add(obj, EqualityComparer.Default); - - public HashCodeBuilder AddMany(IEnumerable objs, IEqualityComparer comparer) => Add(objs.Select(comparer.GetHashCode)); - - public HashCodeBuilder AddMany(IEnumerable objs) => AddMany(objs, EqualityComparer.Default); - - public int Build() => _code; - } -} \ No newline at end of file