|
| 1 | +// SPDX-FileCopyrightText: 2025 smdn <smdn@smdn.jp> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +#nullable enable |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | + |
| 9 | +using NUnit.Framework; |
| 10 | + |
| 11 | +using Smdn.IO; |
| 12 | + |
| 13 | +namespace Smdn.Reflection.ReverseGenerating.ListApi; |
| 14 | + |
| 15 | +#pragma warning disable IDE0040 |
| 16 | +partial class ApiListWriterTests { |
| 17 | +#pragma warning restore IDE0040 |
| 18 | + private void WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 19 | + string sourceCode, |
| 20 | + string expectedMemberDeclaration, |
| 21 | + bool enableRecordTypes, |
| 22 | + bool omitCompilerGeneratedRecordEqualityMethods, |
| 23 | + bool shouldWritten |
| 24 | + ) |
| 25 | + { |
| 26 | + var options = new ApiListWriterOptions(); |
| 27 | + |
| 28 | + options.TypeDeclaration.EnableRecordTypes = enableRecordTypes; |
| 29 | + options.Writer.OmitCompilerGeneratedRecordEqualityMethods = omitCompilerGeneratedRecordEqualityMethods; |
| 30 | + options.Writer.WriteHeader = false; |
| 31 | + options.Writer.WriteFooter = false; |
| 32 | + options.AttributeDeclaration.TypeFilter = static (attrType, _) => { |
| 33 | + return string.Equals(attrType.Name, nameof(CompilerGeneratedAttribute), StringComparison.Ordinal); |
| 34 | + }; |
| 35 | + options.Indent = string.Empty; |
| 36 | + |
| 37 | + var output = new StringReader( |
| 38 | + WriteApiListFromSourceCode( |
| 39 | + sourceCode, |
| 40 | + options, |
| 41 | + referenceAssemblyFileNames: [ |
| 42 | + typeof(CompilerGeneratedAttribute).Assembly.GetName().Name + ".dll", |
| 43 | + typeof(IEquatable<>).Assembly.GetName().Name + ".dll", |
| 44 | + ] |
| 45 | + ) |
| 46 | + ).ReadAllLines(); |
| 47 | + |
| 48 | + var joinedOutput = string.Join("\n", output); |
| 49 | + |
| 50 | + //Console.WriteLine(joinedOutput); |
| 51 | + |
| 52 | + if (shouldWritten) |
| 53 | + Assert.That(joinedOutput, Does.Contain(expectedMemberDeclaration)); |
| 54 | + else |
| 55 | + Assert.That(joinedOutput, Does.Not.Contain(expectedMemberDeclaration)); |
| 56 | + } |
| 57 | + |
| 58 | + [TestCase("public int X { [CompilerGenerated] get; [CompilerGenerated] init; }", true, true)] |
| 59 | + [TestCase("public int X { [CompilerGenerated] get; [CompilerGenerated] init; }", false, true)] |
| 60 | + [TestCase("public R(int X) {}", true, true)] |
| 61 | + [TestCase("public R(int X) {}", false, true)] |
| 62 | + [TestCase("[CompilerGenerated]\npublic override bool Equals(object? obj) {}", true, false)] |
| 63 | + [TestCase("[CompilerGenerated]\npublic override bool Equals(object? obj) {}", false, true)] |
| 64 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(R? other) {}", true, false)] |
| 65 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(R? other) {}", false, true)] |
| 66 | + [TestCase("[CompilerGenerated]\npublic override int GetHashCode() {}", true, false)] |
| 67 | + [TestCase("[CompilerGenerated]\npublic override int GetHashCode() {}", false, true)] |
| 68 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (R? left, R? right) {}", false, true)] |
| 69 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (R? left, R? right) {}", true, false)] |
| 70 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (R? left, R? right) {}", false, true)] |
| 71 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (R? left, R? right) {}", true, false)] |
| 72 | + public void WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 73 | + string expectedMemberDeclaration, |
| 74 | + bool omitCompilerGeneratedRecordEqualityMethods, |
| 75 | + bool shouldWritten |
| 76 | + ) |
| 77 | + => WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 78 | + sourceCode: @"public record R(int X) {}", |
| 79 | + expectedMemberDeclaration: expectedMemberDeclaration, |
| 80 | + enableRecordTypes: true, |
| 81 | + omitCompilerGeneratedRecordEqualityMethods: omitCompilerGeneratedRecordEqualityMethods, |
| 82 | + shouldWritten: shouldWritten |
| 83 | + ); |
| 84 | + |
| 85 | + [TestCase("public int X { [CompilerGenerated] get; [CompilerGenerated] init; }", true, true)] |
| 86 | + [TestCase("public int X { [CompilerGenerated] get; [CompilerGenerated] init; }", false, true)] |
| 87 | + [TestCase("public R(int X) {}", true, true)] |
| 88 | + [TestCase("public R(int X) {}", false, true)] |
| 89 | + [TestCase("[CompilerGenerated]\npublic override bool Equals(object? obj) {}", true, true)] |
| 90 | + [TestCase("[CompilerGenerated]\npublic override bool Equals(object? obj) {}", false, true)] |
| 91 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(R? other) {}", true, true)] |
| 92 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(R? other) {}", false, true)] |
| 93 | + [TestCase("[CompilerGenerated]\npublic override int GetHashCode() {}", true, true)] |
| 94 | + [TestCase("[CompilerGenerated]\npublic override int GetHashCode() {}", false, true)] |
| 95 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (R? left, R? right) {}", false, true)] |
| 96 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (R? left, R? right) {}", true, true)] |
| 97 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (R? left, R? right) {}", false, true)] |
| 98 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (R? left, R? right) {}", true, true)] |
| 99 | + public void WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods_DisableRecordTypes( |
| 100 | + string expectedMemberDeclaration, |
| 101 | + bool omitCompilerGeneratedRecordEqualityMethods, |
| 102 | + bool shouldWritten |
| 103 | + ) |
| 104 | + => WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 105 | + sourceCode: @"public record R(int X) {}", |
| 106 | + expectedMemberDeclaration: expectedMemberDeclaration, |
| 107 | + enableRecordTypes: false, |
| 108 | + omitCompilerGeneratedRecordEqualityMethods: omitCompilerGeneratedRecordEqualityMethods, |
| 109 | + shouldWritten: shouldWritten |
| 110 | + ); |
| 111 | + |
| 112 | + [TestCase("public virtual bool Equals(R? other) {}", true, true)] |
| 113 | + [TestCase("public virtual bool Equals(R? other) {}", false, true)] |
| 114 | + [TestCase("public override int GetHashCode() {}", true, true)] |
| 115 | + [TestCase("public override int GetHashCode() {}", false, true)] |
| 116 | + public void WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods_ExplicitlyImplemented( |
| 117 | + string expectedMemberDeclaration, |
| 118 | + bool omitCompilerGeneratedRecordEqualityMethods, |
| 119 | + bool shouldWritten |
| 120 | + ) |
| 121 | + => WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 122 | + sourceCode: @"#nullable enable |
| 123 | +using System; |
| 124 | +
|
| 125 | +public record R(int X) { |
| 126 | + public virtual bool Equals(R? other) => throw new NotImplementedException(); |
| 127 | + public override int GetHashCode() => throw new NotImplementedException(); |
| 128 | +}", |
| 129 | + expectedMemberDeclaration: expectedMemberDeclaration, |
| 130 | + enableRecordTypes: true, |
| 131 | + omitCompilerGeneratedRecordEqualityMethods: omitCompilerGeneratedRecordEqualityMethods, |
| 132 | + shouldWritten: shouldWritten |
| 133 | + ); |
| 134 | + |
| 135 | + [TestCase("public int Y { [CompilerGenerated] get; [CompilerGenerated] init; }", true, true)] |
| 136 | + [TestCase("public int Y { [CompilerGenerated] get; [CompilerGenerated] init; }", false, true)] |
| 137 | + [TestCase("public RX(int X, int Y) {}", true, true)] |
| 138 | + [TestCase("public RX(int X, int Y) {}", false, true)] |
| 139 | + [TestCase("[CompilerGenerated]\npublic sealed override bool Equals(R? other) {}", true, false)] |
| 140 | + [TestCase("[CompilerGenerated]\npublic sealed override bool Equals(R? other) {}", false, true)] |
| 141 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(RX? other) {}", true, false)] |
| 142 | + [TestCase("[CompilerGenerated]\npublic virtual bool Equals(RX? other) {}", false, true)] |
| 143 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (RX? left, RX? right) {}", false, true)] |
| 144 | + [TestCase("[CompilerGenerated]\npublic static bool operator == (RX? left, RX? right) {}", true, false)] |
| 145 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (RX? left, RX? right) {}", false, true)] |
| 146 | + [TestCase("[CompilerGenerated]\npublic static bool operator != (RX? left, RX? right) {}", true, false)] |
| 147 | + public void WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods_Derived( |
| 148 | + string expectedMemberDeclaration, |
| 149 | + bool omitCompilerGeneratedRecordEqualityMethods, |
| 150 | + bool shouldWritten |
| 151 | + ) |
| 152 | + => WriteExportedTypes_RecordTypes_OmitCompilerGeneratedRecordEqualityMethods( |
| 153 | + sourceCode: @" |
| 154 | +public record R(int X) {} |
| 155 | +public record RX(int X, int Y) : R(X) {}", |
| 156 | + expectedMemberDeclaration: expectedMemberDeclaration, |
| 157 | + enableRecordTypes: true, |
| 158 | + omitCompilerGeneratedRecordEqualityMethods: omitCompilerGeneratedRecordEqualityMethods, |
| 159 | + shouldWritten: shouldWritten |
| 160 | + ); |
| 161 | +} |
0 commit comments