diff --git a/Source/aweXpect.Core/Formatting/ValueFormatters.Type.cs b/Source/aweXpect.Core/Formatting/ValueFormatters.Type.cs index 30a0cc12a..7201ae3ad 100644 --- a/Source/aweXpect.Core/Formatting/ValueFormatters.Type.cs +++ b/Source/aweXpect.Core/Formatting/ValueFormatters.Type.cs @@ -108,7 +108,15 @@ private static void FormatType( Type value, StringBuilder stringBuilder) { - if (value.IsArray) + if (value == typeof(void)) + { + stringBuilder.Append("void"); + } + else if (value.IsGenericParameter) + { + stringBuilder.Append(value.Name); + } + else if (value.IsArray) { FormatType(value.GetElementType()!, stringBuilder); stringBuilder.Append("[]"); diff --git a/Tests/aweXpect.Core.Tests/Formatting/ValueFormatters.TypeTests.cs b/Tests/aweXpect.Core.Tests/Formatting/ValueFormatters.TypeTests.cs index 75a8dff1c..dd8783fda 100644 --- a/Tests/aweXpect.Core.Tests/Formatting/ValueFormatters.TypeTests.cs +++ b/Tests/aweXpect.Core.Tests/Formatting/ValueFormatters.TypeTests.cs @@ -1,5 +1,7 @@ using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Text; namespace aweXpect.Core.Tests.Formatting; @@ -105,9 +107,9 @@ public async Task ShouldSupportNestedGenericTypeDefinitions() } [Theory] - [InlineData(typeof(IDictionary<,>), 0, "IDictionary<, >.TKey")] - [InlineData(typeof(IDictionary<,>), 1, "IDictionary<, >.TValue")] - [InlineData(typeof(IEnumerable<>), 0, "IEnumerable<>.T")] + [InlineData(typeof(IDictionary<,>), 0, "TKey")] + [InlineData(typeof(IDictionary<,>), 1, "TValue")] + [InlineData(typeof(IEnumerable<>), 0, "T")] public async Task ShouldSupportOpenGenericParametersOfIDictionary( Type genericType, int argumentIndex, string expectedResult) { @@ -187,6 +189,25 @@ public async Task Types_ShouldOnlyIncludeTheName() await That(sb.ToString()).IsEqualTo(expectedResult); } + [Fact] + public async Task WhenGenericParameter_ShouldUseOnlyName() + { + MethodInfo method = GetType() + .GetMethods(BindingFlags.Static | BindingFlags.NonPublic) + .Single(x => x.Name.StartsWith(nameof(DummyMethodToGetSpecialTypes))); + string expectedResult = "TParameter"; + Type value = method.GetGenericArguments()[0]; + StringBuilder sb = new(); + + string result = Formatter.Format(value); + string objectResult = Formatter.Format((object?)value); + Formatter.Format(sb, value); + + await That(result).IsEqualTo(expectedResult); + await That(objectResult).IsEqualTo(expectedResult); + await That(sb.ToString()).IsEqualTo(expectedResult); + } + [Fact] public async Task WhenNull_ShouldUseDefaultNullString() { @@ -202,9 +223,34 @@ public async Task WhenNull_ShouldUseDefaultNullString() await That(sb.ToString()).IsEqualTo(ValueFormatter.NullString); } + [Fact] + public async Task WhenVoid_ShouldUseSimpleName() + { + MethodInfo method = GetType() + .GetMethods(BindingFlags.Static | BindingFlags.NonPublic) + .Single(x => x.Name.StartsWith(nameof(DummyMethodToGetSpecialTypes))); + string expectedResult = "void"; + Type value = method.ReturnType; + StringBuilder sb = new(); + + string result = Formatter.Format(value); + string objectResult = Formatter.Format((object?)value); + Formatter.Format(sb, value); + + await That(result).IsEqualTo(expectedResult); + await That(objectResult).IsEqualTo(expectedResult); + await That(sb.ToString()).IsEqualTo(expectedResult); + } + // ReSharper disable once UnusedTypeParameter private class NestedGenericType; + // ReSharper disable once UnusedParameter.Local + private static void DummyMethodToGetSpecialTypes(TParameter value) + { + // This method is only used to get a void return type and generic parameter types. + } + public static TheoryData SimpleTypes => new() { @@ -306,7 +352,7 @@ public static TheoryData SimpleTypes }, { typeof(void), "void" - }, + } }; } }