From 5753c1158ffc2cb05de0b1b922fe307ac3418830 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Tue, 16 Apr 2024 23:26:12 -0500 Subject: [PATCH] Fixed remaining bugs with the readonly source generator. --- Schema Tests/readOnly/AutoInheritanceTests.cs | 219 ++++++++++++++++++ .../readOnly/BasicReadOnlyGeneratorTests.cs | 175 -------------- Schema/Schema.csproj | 2 +- Schema/src/readOnly/ReadOnlyTypeGenerator.cs | 2 +- Schema/src/util/symbols/SymbolTypeUtil.cs | 2 +- 5 files changed, 222 insertions(+), 178 deletions(-) create mode 100644 Schema Tests/readOnly/AutoInheritanceTests.cs diff --git a/Schema Tests/readOnly/AutoInheritanceTests.cs b/Schema Tests/readOnly/AutoInheritanceTests.cs new file mode 100644 index 0000000..9a2acec --- /dev/null +++ b/Schema Tests/readOnly/AutoInheritanceTests.cs @@ -0,0 +1,219 @@ +using NUnit.Framework; + +using schema.binary; + +namespace fin.data.indexable { + public interface IIndexable { + int Index { get; } + } +} + + +namespace schema.readOnly { + internal class AutoInheritanceTests { + [Test] + public void TestWeirdSituation() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + using fin.data.indexable; + + namespace foo.bar { + [GenerateReadOnly] + public partial class AlreadyConstWrapper : IIndexable; + } + """, + """ + namespace foo.bar { + public partial class AlreadyConstWrapper : IReadOnlyAlreadyConstWrapper; + + public interface IReadOnlyAlreadyConstWrapper : fin.data.indexable.IIndexable; + } + + """); + } + + [Test] + public void TestAlreadyConstInterfaceParent() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + + namespace foo.bar { + public interface IAlreadyConst { + public bool Property { get; } + } + + [GenerateReadOnly] + public partial class AlreadyConstWrapper : IAlreadyConst; + } + """, + """ + namespace foo.bar { + public partial class AlreadyConstWrapper : IReadOnlyAlreadyConstWrapper; + + public interface IReadOnlyAlreadyConstWrapper : IAlreadyConst; + } + + """); + } + + [Test] + public void TestAlreadyConstInterfaceParentFromAnotherNamespace() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + + namespace foo.bar.place1 { + public class OtherParent { + public interface IAlreadyConst { + bool Property { get; } + } + } + } + + namespace foo.bar.place2 { + public partial class Parent { + [GenerateReadOnly] + public partial class AlreadyConstWrapper : place1.OtherParent.IAlreadyConst; + } + } + """, + """ + namespace foo.bar.place2 { + public partial class Parent { + public partial class AlreadyConstWrapper : IReadOnlyAlreadyConstWrapper; + + public interface IReadOnlyAlreadyConstWrapper : place1.OtherParent.IAlreadyConst; + } + } + + """); + } + + [Test] + public void TestAutoInheritance() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + + namespace foo.bar { + [GenerateReadOnly] + public partial interface IBase {} + + [GenerateReadOnly] + public partial interface IChild : IBase {} + } + """, + """ + namespace foo.bar { + public partial interface IBase : IReadOnlyBase; + + public interface IReadOnlyBase; + } + + """, + """ + namespace foo.bar { + public partial interface IChild : IReadOnlyChild; + + public interface IReadOnlyChild : IReadOnlyBase; + } + + """); + } + + [Test] + public void TestAutoInheritanceFromAnotherNamespace() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + + namespace foo.bar.other { + public partial class OtherParent { + [GenerateReadOnly] + public partial interface IBase { + bool Foo { get; set; } + } + } + } + + namespace foo.bar { + public partial class Parent { + [GenerateReadOnly] + public partial interface IChild : other.OtherParent.IBase { + public bool Value { get; set; } + } + } + } + """, + """ + namespace foo.bar.other { + public partial class OtherParent { + public partial interface IBase : IReadOnlyBase; + + public interface IReadOnlyBase { + public bool Foo { get; } + } + } + } + + """, + """ + namespace foo.bar { + public partial class Parent { + public partial interface IChild : IReadOnlyChild; + + public interface IReadOnlyChild : other.OtherParent.IReadOnlyBase { + public bool Value { get; } + } + } + } + + """); + } + + [Test] + public void TestAutoGenericInheritance() { + ReadOnlyGeneratorTestUtil.AssertGenerated( + """ + using schema.readOnly; + + namespace foo.bar { + [GenerateReadOnly] + public partial interface IBase1 {} + + [GenerateReadOnly] + public partial interface IBase2 {} + + [GenerateReadOnly] + public partial interface IChild : IBase1, IBase2 {} + } + """, + """ + namespace foo.bar { + public partial interface IBase1 : IReadOnlyBase1; + + public interface IReadOnlyBase1; + } + + """, + """ + namespace foo.bar { + public partial interface IBase2 : IReadOnlyBase2; + + public interface IReadOnlyBase2; + } + + """, + """ + namespace foo.bar { + public partial interface IChild : IReadOnlyChild; + + public interface IReadOnlyChild : IReadOnlyBase1, IReadOnlyBase2; + } + + """); + } + } +} \ No newline at end of file diff --git a/Schema Tests/readOnly/BasicReadOnlyGeneratorTests.cs b/Schema Tests/readOnly/BasicReadOnlyGeneratorTests.cs index 1f74e2d..f6534cd 100644 --- a/Schema Tests/readOnly/BasicReadOnlyGeneratorTests.cs +++ b/Schema Tests/readOnly/BasicReadOnlyGeneratorTests.cs @@ -100,64 +100,6 @@ public interface IReadOnlySimpleGenerics { """); } - [Test] - public void TestAlreadyConstInterfaceParent() { - ReadOnlyGeneratorTestUtil.AssertGenerated( - """ - using schema.readOnly; - - namespace foo.bar { - public interface IAlreadyConst { - public bool Property { get; } - } - - [GenerateReadOnly] - public partial class AlreadyConstWrapper : IAlreadyConst; - } - """, - """ - namespace foo.bar { - public partial class AlreadyConstWrapper : IReadOnlyAlreadyConstWrapper; - - public interface IReadOnlyAlreadyConstWrapper : IAlreadyConst; - } - - """); - } - - [Test] - public void TestAlreadyConstInterfaceParentFromAnotherNamespace() { - ReadOnlyGeneratorTestUtil.AssertGenerated( - """ - using schema.readOnly; - - namespace foo.bar.other { - public class OtherParent { - public interface IAlreadyConst { - public bool Property { get; } - } - } - } - - namespace foo.bar { - public partial class Parent { - [GenerateReadOnly] - public partial class AlreadyConstWrapper : other.OtherParent.IAlreadyConst; - } - } - """, - """ - namespace foo.bar { - public partial class Parent { - public partial class AlreadyConstWrapper : IReadOnlyAlreadyConstWrapper; - - public interface IReadOnlyAlreadyConstWrapper : other.OtherParent.IAlreadyConst; - } - } - - """); - } - [Test] [TestCase("class")] [TestCase("class?")] @@ -276,122 +218,5 @@ public interface IReadOnlyvoid<@double> where @double : struct { """); } - - [Test] - public void TestAutoInheritance() { - ReadOnlyGeneratorTestUtil.AssertGenerated( - """ - using schema.readOnly; - - namespace foo.bar { - [GenerateReadOnly] - public partial interface IBase {} - - [GenerateReadOnly] - public partial interface IChild : IBase {} - } - """, - """ - namespace foo.bar { - public partial interface IBase : IReadOnlyBase; - - public interface IReadOnlyBase; - } - - """, - """ - namespace foo.bar { - public partial interface IChild : IReadOnlyChild; - - public interface IReadOnlyChild : IReadOnlyBase; - } - - """); - } - - [Test] - public void TestAutoInheritanceFromAnotherNamespace() { - ReadOnlyGeneratorTestUtil.AssertGenerated( - """ - using schema.readOnly; - - namespace foo.bar.other { - public partial class OtherParent { - [GenerateReadOnly] - public partial interface IBase {} - } - } - - namespace foo.bar { - public partial class Parent { - [GenerateReadOnly] - public partial interface IChild : other.OtherParent.IBase {} - } - } - """, - """ - namespace foo.bar.other { - public partial class OtherParent { - public partial interface IBase : IReadOnlyBase; - - public interface IReadOnlyBase; - } - } - - """, - """ - namespace foo.bar { - public partial class Parent { - public partial interface IChild : IReadOnlyChild; - - public interface IReadOnlyChild : other.OtherParent.IReadOnlyBase; - } - } - - """); - } - - [Test] - public void TestAutoGenericInheritance() { - ReadOnlyGeneratorTestUtil.AssertGenerated( - """ - using schema.readOnly; - - namespace foo.bar { - [GenerateReadOnly] - public partial interface IBase1 {} - - [GenerateReadOnly] - public partial interface IBase2 {} - - [GenerateReadOnly] - public partial interface IChild : IBase1, IBase2 {} - } - """, - """ - namespace foo.bar { - public partial interface IBase1 : IReadOnlyBase1; - - public interface IReadOnlyBase1; - } - - """, - """ - namespace foo.bar { - public partial interface IBase2 : IReadOnlyBase2; - - public interface IReadOnlyBase2; - } - - """, - """ - namespace foo.bar { - public partial interface IChild : IReadOnlyChild; - - public interface IReadOnlyChild : IReadOnlyBase1, IReadOnlyBase2; - } - - """); - } } } \ No newline at end of file diff --git a/Schema/Schema.csproj b/Schema/Schema.csproj index 3334276..92193ae 100644 --- a/Schema/Schema.csproj +++ b/Schema/Schema.csproj @@ -14,7 +14,7 @@ Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes. schema schema - 0.3.17 + 0.4.0 MeltyPlayer diff --git a/Schema/src/readOnly/ReadOnlyTypeGenerator.cs b/Schema/src/readOnly/ReadOnlyTypeGenerator.cs index 8b5bbc5..3f4455f 100644 --- a/Schema/src/readOnly/ReadOnlyTypeGenerator.cs +++ b/Schema/src/readOnly/ReadOnlyTypeGenerator.cs @@ -235,7 +235,7 @@ private static bool IsTypeAlreadyConst_(INamedTypeSymbol typeSymbol) { } if (memberSymbol is IMethodSymbol && - (memberSymbol.Name.EndsWith("_Property") || + (memberSymbol.Name.StartsWith("get_") || memberSymbol.HasAttribute())) { continue; } diff --git a/Schema/src/util/symbols/SymbolTypeUtil.cs b/Schema/src/util/symbols/SymbolTypeUtil.cs index 84f561f..9b4e054 100644 --- a/Schema/src/util/symbols/SymbolTypeUtil.cs +++ b/Schema/src/util/symbols/SymbolTypeUtil.cs @@ -346,7 +346,7 @@ public static string GetQualifiedNameFromCurrentSymbol( // TODO: Is there a built-in for this? return overrideName ?? primitiveType switch { - SchemaPrimitiveType.BOOLEAN => "boolean", + SchemaPrimitiveType.BOOLEAN => "bool", SchemaPrimitiveType.SBYTE => "sbyte", SchemaPrimitiveType.BYTE => "byte", SchemaPrimitiveType.INT16 => "short",