From a4a78365637efd7152dfc52f0c7153e2037932fa Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 24 Nov 2023 02:14:01 +0100 Subject: [PATCH] Remove leftover unit tests for [AutoConstructor] --- .../AutoConstructorGeneratorTests.cs | 114 ------------------ .../AutoConstructorAttributeTests.cs | 89 -------------- 2 files changed, 203 deletions(-) delete mode 100644 tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs delete mode 100644 tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs diff --git a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs b/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs deleted file mode 100644 index 7d897463..00000000 --- a/tests/ComputeSharp.Tests.SourceGenerators/AutoConstructorGeneratorTests.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using ComputeSharp.Core.SourceGenerators; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.Tests.SourceGenerators; - -[TestClass] -[TestCategory("AutoConstructorGenerator")] -public class AutoConstructorGeneratorTests -{ - [TestMethod] - public void GenerateConstructor() - { - const string source = """ - using System.Numerics; - using ComputeSharp; - - namespace ComputeSharp - { - public class ReadWriteBuffer { } - } - - namespace MyFancyApp.Sample - { - public partial class IFoo : IFluff - where T : notnull, new() - { - public partial class Foo - { - [AutoConstructor] - public readonly partial struct Test - { - private readonly float a; - private readonly Vector2 b; - public readonly ReadWriteBuffer c; - public readonly ReadWriteBuffer d; - } - } - } - } - """; - - string expected = $$""" - // - #pragma warning disable - namespace MyFancyApp.Sample; - partial class IFoo - { - partial class Foo - { - partial struct Test - { - [global::System.CodeDom.Compiler.GeneratedCode("ComputeSharp.Core.SourceGenerators.AutoConstructorGenerator", "{{typeof(AutoConstructorGenerator).Assembly.GetName().Version}}")] - [global::System.Diagnostics.DebuggerNonUserCode] - [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] - public Test(float a, global::System.Numerics.Vector2 b, global::ComputeSharp.ReadWriteBuffer c, global::ComputeSharp.ReadWriteBuffer d) - { - this.a = a; - this.b = b; - this.c = c; - this.d = d; - } - } - } - } - """; - - VerifyGeneratedMethodLines(source, 0, expected); - } - - /// - /// Verifies the output of a source generator. - /// - /// The generator type to use. - /// The input source to process. - /// The target index to check in the resulting output. - /// The expected body to compare with the generated code. - private static void VerifyGeneratedMethodLines(string source, int index, string expectedBody) - where TGenerator : class, IIncrementalGenerator, new() - { - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); - - IEnumerable references = - from assembly in AppDomain.CurrentDomain.GetAssemblies() - where !assembly.IsDynamic - let reference = MetadataReference.CreateFromFile(assembly.Location) - select reference; - - CSharpCompilation compilation = CSharpCompilation.Create("original", new SyntaxTree[] { syntaxTree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - - IIncrementalGenerator generator = new TGenerator(); - - CSharpGeneratorDriver driver = CSharpGeneratorDriver.Create(generator); - - _ = driver.RunGeneratorsAndUpdateCompilation(compilation, out Compilation outputCompilation, out ImmutableArray diagnostics); - - string outputBody = outputCompilation.SyntaxTrees.Skip(index + 1).First().ToString(); - - string[] outputLines = outputBody.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - string[] expectedLines = expectedBody.Replace("\r", "").Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - - Assert.AreEqual(expectedLines.Length, outputLines.Length); - - for (int i = 0; i < outputLines.Length; i++) - { - Assert.AreEqual(expectedLines[i], outputLines[i]); - } - } -} \ No newline at end of file diff --git a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs deleted file mode 100644 index 88254195..00000000 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ /dev/null @@ -1,89 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace ComputeSharp.Tests; - -[TestClass] -[TestCategory("AutoConstructorAttribute")] -public unsafe partial class AutoConstructorAttributeTests -{ - [AutoConstructor] - public partial struct StructWithFixedField - { - public int a; - public float b; - public byte* c; - public fixed byte d[16]; - } - - [TestMethod] - public void GenerateConstructorWithInheritedUnsafeContextAndIgnoredFixedField() - { - byte c = 123; - - StructWithFixedField instance = new(42, 3.14f, &c); - - Assert.AreEqual(instance.a, 42); - Assert.AreEqual(instance.b, 3.14f); - Assert.IsTrue(instance.c == &c); - } - - public partial interface ISomeInterface - { - public partial record SomeRecord - { - public partial struct SomeStruct - { - [AutoConstructor] - public partial struct AnotherStruct - { - public int a; - public float b; - } - } - } - } - - [TestMethod] - public void GenerateConstructorWithNestedTypes() - { - int a = 42; - float b = 3.14f; - - ISomeInterface.SomeRecord.SomeStruct.AnotherStruct instance = new(a, b); - - Assert.AreEqual(instance.a, a); - Assert.AreEqual(instance.b, b); - } - - public partial interface IAnotherInterface - { - public int B { get; } - - public void Foo(); - } - - [AutoConstructor] - public partial struct AnotherStruct : IAnotherInterface - { - public int a; - public float b; - - int IAnotherInterface.B => 100; - - void IAnotherInterface.Foo() - { - } - } - - [TestMethod] - public void GenerateConstructorWithExplicitInterfaceMembers() - { - int a = 42; - float b = 3.14f; - - AnotherStruct instance = new(a, b); - - Assert.AreEqual(instance.a, a); - Assert.AreEqual(instance.b, b); - } -} \ No newline at end of file