Skip to content

Commit

Permalink
Remove CodeDom reference (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Oct 17, 2021
1 parent 58f1236 commit 668122a
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 89 deletions.
1 change: 0 additions & 1 deletion src/Verify.Integration.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if DEBUG
using System.Linq;
using DiffEngine;
using EmptyFiles;
using VerifyTests;
Expand Down
1 change: 0 additions & 1 deletion src/Verify.Integration.Tests/Tests_Single.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if DEBUG
using System.Linq;
using DiffEngine;
using VerifyTests;
using VerifyXunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SerializationTests.MyMethodWithParameters(Int32 x, String y)
SerializationTests.MyMethodWithParameters(int x, string y)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x of SerializationTests.SerializationTests.MyMethodWithParameters(Int32 x, String y)
x of SerializationTests.SerializationTests.MyMethodWithParameters(int x, string y)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SerializationTests.set_MyProperty(String value)
SerializationTests.set_MyProperty(string value)
2 changes: 1 addition & 1 deletion src/Verify.Tests/TypeNameConverterTests.Array.verified.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Int32[]
int[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int[,]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int?[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dictionary<string, int>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dictionary<IEnumerable<TargetWithNamespace>, IEnumerable<TargetWithNamespace>>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IEnumerable<TargetWithNamespace>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IEnumerable<T>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Nullable<Int32>
int?
Original file line number Diff line number Diff line change
@@ -1 +1 @@
String
string
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ConcurrentDictionary<string, string>
Dictionary<string, ConcurrentDictionary<string, string>>
37 changes: 36 additions & 1 deletion src/Verify.Tests/TypeNameConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TypeNameConverterTests
[Fact]
public Task WithOneGeneric()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(StringDictionaryWrapper<string, ConcurrentDictionary<string, string>>)));
return Verifier.Verify(TypeNameConverter.GetName(typeof(Dictionary<string, ConcurrentDictionary<string, string>>)));
}

[Fact]
Expand All @@ -18,6 +18,12 @@ public Task Simple()
return Verifier.Verify(TypeNameConverter.GetName(typeof(string)));
}

[Fact]
public Task GenericTypeDefinition()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(IEnumerable<>)));
}

[Fact]
public Task GenericArguments()
{
Expand Down Expand Up @@ -45,6 +51,18 @@ public Task Array()
return Verifier.Verify(TypeNameConverter.GetName(typeof(int[])));
}

[Fact]
public Task ArrayMulti()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(int[,])));
}

[Fact]
public Task ArrayNullable()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(int?[])));
}

[Fact]
public Task List()
{
Expand All @@ -57,6 +75,23 @@ public Task Enumerable()
return Verifier.Verify(TypeNameConverter.GetName(typeof(IEnumerable<TargetWithNamespace>)));
}

[Fact]
public Task Dictionary()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(Dictionary<string,int>)));
}
[Fact]
public Task Dictionary2()
{
return Verifier.Verify(TypeNameConverter.GetName(typeof(Dictionary<IEnumerable<TargetWithNamespace>,IEnumerable<TargetWithNamespace>>)));
}

[Fact]
public Task DictionaryWrapper()
{
return Verifier.Verify(typeof(DictionaryWrapper<IEnumerable<TargetWithNamespace>,IEnumerable<TargetWithNamespace>>));
}

[Fact]
public Task Dynamic()
{
Expand Down
151 changes: 73 additions & 78 deletions src/Verify/Serialization/Converters/TypeNameConverter.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
using System.CodeDom;
using Microsoft.CSharp;

namespace VerifyTests;
namespace VerifyTests;

public static class TypeNameConverter
{
static ConcurrentDictionary<Type, string> cacheDictionary = new(
new List<KeyValuePair<Type, string>>
{
new(typeof(char), "Char"),
new(typeof(char?), "Nullable<Char>"),
new(typeof(string), "String"),
new(typeof(sbyte), "SByte"),
new(typeof(sbyte?), "Nullable<SByte>"),
new(typeof(byte), "Byte"),
new(typeof(byte?), "Nullable<Byte>"),
new(typeof(bool), "Boolean"),
new(typeof(bool?), "Nullable<Boolean>"),
new(typeof(short), "Int16"),
new(typeof(short?), "Nullable<Int16>"),
new(typeof(ushort), "UInt16"),
new(typeof(ushort?), "Nullable<UInt16>"),
new(typeof(int), "Int32"),
new(typeof(int?), "Nullable<Int32>"),
new(typeof(uint), "UInt32"),
new(typeof(uint?), "Nullable<UInt32>"),
new(typeof(long), "Int64"),
new(typeof(long?), "Nullable<Int64>"),
new(typeof(nint), "IntPtr"),
new(typeof(nint?), "Nullable<IntPtr>"),
new(typeof(nuint), "UIntPtr"),
new(typeof(nuint?), "Nullable<UIntPtr>"),
new(typeof(decimal), "Decimal"),
new(typeof(decimal?), "Nullable<Decimal>"),
new(typeof(float), "Single"),
new(typeof(float?), "Nullable<Single>"),
new(typeof(double), "Double"),
new(typeof(double?), "Nullable<Double>"),
new(typeof(object), "object"),
new(typeof(char), "char"),
new(typeof(char?), "char?"),
new(typeof(string), "string"),
new(typeof(sbyte), "sbyte"),
new(typeof(sbyte?), "sbyte?"),
new(typeof(byte), "byte"),
new(typeof(byte?), "byte?"),
new(typeof(bool), "bool"),
new(typeof(bool?), "bool?"),
new(typeof(short), "short"),
new(typeof(short?), "short?"),
new(typeof(ushort), "ushort"),
new(typeof(ushort?), "ushort?"),
new(typeof(int), "int"),
new(typeof(int?), "int?"),
new(typeof(uint), "uint"),
new(typeof(uint?), "uint?"),
new(typeof(long), "long"),
new(typeof(long?), "long?"),
new(typeof(nint), "nint"),
new(typeof(nint?), "nint"),
new(typeof(nuint), "nuint"),
new(typeof(nuint?), "nuint?"),
new(typeof(decimal), "decimal"),
new(typeof(decimal?), "decimal?"),
new(typeof(float), "float"),
new(typeof(float?), "float?"),
new(typeof(double), "double"),
new(typeof(double?), "double?"),
new(typeof(Guid), "Guid"),
new(typeof(Guid?), "Nullable<Guid>"),
new(typeof(Guid?), "Guid?"),
new(typeof(DateTime), "DateTime"),
new(typeof(DateTime?), "Nullable<DateTime>"),
new(typeof(DateTime?), "DateTime?"),
new(typeof(DateTimeOffset), "DateTimeOffset"),
new(typeof(DateTimeOffset?), "Nullable<DateTimeOffset>"),
new(typeof(DateTimeOffset?), "DateTimeOffset?"),
new(typeof(TimeSpan), "TimeSpan"),
new(typeof(TimeSpan?), "Nullable<TimeSpan>"),
new(typeof(TimeSpan?), "TimeSpan?"),
#if NET5_0_OR_GREATER
new(typeof(Half), "Half"),
new(typeof(Half?), "Nullable<Half>"),
new(typeof(Half?), "Half?"),
#endif
#if NET6_0_OR_GREATER
new(typeof(DateOnly), "DateOnly"),
new(typeof(DateOnly?), "Nullable<DateOnly>"),
new(typeof(TimeOnly), "TimeOnly"),
new(typeof(TimeOnly?), "Nullable<TimeOnly>"),
new(typeof(DateOnly), "DateOnly"),
new(typeof(DateOnly?), "DateOnly?"),
new(typeof(TimeOnly), "TimeOnly"),
new(typeof(TimeOnly?), "TimeOnly?"),
#endif
});

static ConcurrentDictionary<ICustomAttributeProvider, string> infoCache = new();

static CSharpCodeProvider codeDomProvider = new();

public static string GetName(Type type)
{
return cacheDictionary.GetOrAdd(type, Inner);
Expand Down Expand Up @@ -122,7 +118,7 @@ public static string GetName(ConstructorInfo constructor)
return infoCache.GetOrAdd(constructor, _ =>
{
var declaringType = GetName(constructor.DeclaringType!);
StringBuilder builder = new($"{declaringType}");
var builder = new StringBuilder($"{declaringType}");
if (constructor.IsStatic)
{
builder.Append(".cctor(");
Expand Down Expand Up @@ -179,54 +175,53 @@ static string Inner(Type type)

if (typeof(IDictionaryWrapper).IsAssignableFrom(type))
{
type = type.GetGenericArguments().Last();
return GetName(type.GetGenericArguments().Last());
}

var typeName = GetTypeName(type);
CodeTypeReference reference = new(typeName);
var name = codeDomProvider.GetTypeOutput(reference);
var list = new List<string>();
AllGenericArgumentNamespace(type, list);
foreach (var ns in list.Distinct())
{
name = name.Replace($"<{ns}.", "<");
name = name.Replace($", {ns}.", ", ");
}

return name;
return InnerGetName(type);
}

static string GetTypeName(Type type)
static string InnerGetName(Type type)
{
if (type.FullName is null)
if (type.IsGenericParameter)
{
return type.Name;
}
return type.FullName.Replace(type.Namespace + ".", "");
}

static bool IsAnonType(this Type type)
{
return type.Name.Contains("AnonymousType");
}

static void AllGenericArgumentNamespace(Type type, List<string> list)
{
if (type.Namespace is not null)
if (type.IsArray)
{
list.Add(type.Namespace);
}
var elementType = type.GetElementType()!;
var rank = type.GetArrayRank();

var elementType = type.GetElementType();
return $"{GetName(elementType)}[{new string(',',rank-1)}]";
}

if (elementType is not null)
var typeName = type.Name;
if (type.IsGenericType)
{
AllGenericArgumentNamespace(elementType,list);
}
var tick = typeName.IndexOf('`');
var builder = new StringBuilder(typeName.Substring(0, tick));
builder.Append("<");
foreach (var argument in type.GetGenericArguments())
{
builder.Append(GetName(argument) + ", ");
}

foreach (var generic in type.GenericTypeArguments)
builder.Length -= 2;
builder.Append(">");
return builder.ToString();
}

if (type.IsNested)
{
AllGenericArgumentNamespace(generic, list);
return $"{GetName(type.DeclaringType!)}.{typeName}";
}

return typeName;
}

static bool IsAnonType(this Type type)
{
return type.Name.Contains("AnonymousType");
}
}
1 change: 0 additions & 1 deletion src/Verify/Verify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" Condition="$(TargetFramework) == 'netstandard2.0' OR $(TargetFramework) == 'netstandard2.1' OR $(TargetFramework) == 'net461' OR $(TargetFramework) == 'net472'" />
<PackageReference Include="TextCopy" Version="4.3.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.CodeDom" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<PackageReference Include="ProjectDefaults" Version="1.0.61" PrivateAssets="All" />
<PackageReference Include="IndexRange" Version="1.0.0" Condition="$(TargetFramework) == 'netstandard2.0' OR $(TargetFramework) == 'net461' OR $(TargetFramework) == 'net472'" PrivateAssets="all" />
Expand Down

0 comments on commit 668122a

Please sign in to comment.