Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Source/aweXpect.Core/Formatting/ValueFormatters.Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("[]");
Expand Down
54 changes: 50 additions & 4 deletions Tests/aweXpect.Core.Tests/Formatting/ValueFormatters.TypeTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
{
Expand All @@ -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<T>;

// ReSharper disable once UnusedParameter.Local
private static void DummyMethodToGetSpecialTypes<TParameter>(TParameter value)
{
// This method is only used to get a void return type and generic parameter types.
}

public static TheoryData<Type, string> SimpleTypes
=> new()
{
Expand Down Expand Up @@ -306,7 +352,7 @@ public static TheoryData<Type, string> SimpleTypes
},
{
typeof(void), "void"
},
}
};
}
}
Loading