Skip to content

Commit

Permalink
Fixed some bugs where variance wasn't properly printed on generics in…
Browse files Browse the repository at this point in the history
… the other partial definitions.
  • Loading branch information
MeltyPlayer committed Apr 23, 2024
1 parent e1df143 commit 4dee591
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Schema Tests/binary/generator/VarianceGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using NUnit.Framework;


namespace schema.binary.text {
internal class VarianceGeneratorTests {
[Test]
[TestCase("in")]
[TestCase("out")]
public void TestSupportsEachTypeOfVarianceInType(string variance) {
BinarySchemaTestUtil.AssertGenerated(
$$"""
using schema.binary;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper<{{variance}} T>;
}
""",
$$"""
using System;
using schema.binary;
namespace foo.bar {
public partial class Wrapper<{{variance}} T> {
public void Read(IBinaryReader br) {
}
}
}
""",
$$"""
using System;
using schema.binary;
namespace foo.bar {
public partial class Wrapper<{{variance}} T> {
public void Write(IBinaryWriter bw) {
}
}
}
""");
}
}
}
3 changes: 1 addition & 2 deletions Schema Tests/readOnly/VarianceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NUnit.Framework;

using schema.binary;

namespace schema.readOnly {
Expand All @@ -19,7 +18,7 @@ public partial interface IWrapper<{{variance}} T>;
""",
$$"""
namespace foo.bar {
public partial interface IWrapper<T> : IReadOnlyWrapper<T>;
public partial interface IWrapper<{{variance}} T> : IReadOnlyWrapper<T>;
public interface IReadOnlyWrapper<{{variance}} T>;
}
Expand Down
36 changes: 35 additions & 1 deletion Schema/src/util/symbols/NamedTypeSymbolUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -54,7 +55,8 @@ public static StringBuilder AppendQualifiersAndNameAndGenericParametersFor(
string? replacementName = null)
=> sb.AppendSymbolQualifiers(namedTypeSymbol)
.Append(" ")
.AppendNameAndGenericParametersFor(namedTypeSymbol, replacementName);
.Append(replacementName ?? namedTypeSymbol.Name.EscapeKeyword())
.AppendGenericParametersWithVariance(namedTypeSymbol);

public static string GetNameAndGenericParametersFor(
this INamedTypeSymbol namedTypeSymbol,
Expand Down Expand Up @@ -90,5 +92,37 @@ public static StringBuilder AppendGenericParametersFor(
yield return (typeParams[i], typeArgs[i]);
}
}

public static string GetGenericParametersWithVariance(
this INamedTypeSymbol symbol)
=> new StringBuilder().AppendGenericParametersWithVariance(symbol)
.ToString();

public static StringBuilder AppendGenericParametersWithVariance(
this StringBuilder sb,
INamedTypeSymbol symbol) {
var typeParameters = symbol.TypeParameters;
if (typeParameters.Length == 0) {
return sb;
}

sb.Append("<");
for (var i = 0; i < typeParameters.Length; ++i) {
if (i > 0) {
sb.Append(", ");
}

var typeParameter = typeParameters[i];
sb.Append(typeParameter.Variance switch {
VarianceKind.In => "in ",
VarianceKind.Out => "out ",
VarianceKind.None => "",
})
.Append(typeParameter.Name.EscapeKeyword());
}

sb.Append(">");
return sb;
}
}
}

0 comments on commit 4dee591

Please sign in to comment.