diff --git a/src/Pretender.SourceGenerator/Emitter/GrandEmitter.cs b/src/Pretender.SourceGenerator/Emitter/GrandEmitter.cs index fe7b2a3..aac58a4 100644 --- a/src/Pretender.SourceGenerator/Emitter/GrandEmitter.cs +++ b/src/Pretender.SourceGenerator/Emitter/GrandEmitter.cs @@ -1,10 +1,5 @@ using System.Collections.Immutable; -using System.Security.Cryptography.X509Certificates; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; using Pretender.SourceGenerator.Writing; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Pretender.SourceGenerator.Emitter { @@ -31,6 +26,15 @@ public string Emit(CancellationToken cancellationToken) { var writer = new IndentedTextWriter(); + writer.WriteLine("// "); + writer.WriteLine(); + writer.WriteLine("#nullable enable annotations"); + writer.WriteLine("#nullable disable warnings"); + writer.WriteLine(); + writer.WriteLine("// Suppress warnings about [Obsolete] member usage in generated code."); + writer.WriteLine("#pragma warning disable CS0612, CS0618"); + writer.WriteLine(); + // InceptsLocationAttribute writer.Write(KnownBlocks.InterceptsLocationAttribute, isMultiline: true); writer.WriteLine(); diff --git a/src/Pretender.SourceGenerator/PretenderSourceGenerator.cs b/src/Pretender.SourceGenerator/PretenderSourceGenerator.cs index 4b82dda..0349e62 100644 --- a/src/Pretender.SourceGenerator/PretenderSourceGenerator.cs +++ b/src/Pretender.SourceGenerator/PretenderSourceGenerator.cs @@ -1,5 +1,4 @@ using System.Collections.Immutable; -using System.Text; using Microsoft.CodeAnalysis; using Pretender.SourceGenerator.Emitter; using Pretender.SourceGenerator.Invocation; diff --git a/src/Pretender/IPretendSetup.cs b/src/Pretender/IPretendSetup.cs index df7517d..4223b20 100644 --- a/src/Pretender/IPretendSetup.cs +++ b/src/Pretender/IPretendSetup.cs @@ -11,7 +11,10 @@ public interface IPretendSetup [EditorBrowsable(EditorBrowsableState.Advanced)] void SetBehavior(Behavior behavior); + +#pragma warning disable CS0618 void Verify(Called called) => Pretend.Verify(pretendSetup: this, called); +#pragma warning restore CS0618 } public interface IPretendSetup : IPretendSetup diff --git a/src/Pretender/Internals/BaseCompiledSetup.cs b/src/Pretender/Internals/BaseCompiledSetup.cs index f0079be..7d0c1c4 100644 --- a/src/Pretender/Internals/BaseCompiledSetup.cs +++ b/src/Pretender/Internals/BaseCompiledSetup.cs @@ -4,7 +4,7 @@ namespace Pretender.Internals { [EditorBrowsable(EditorBrowsableState.Never)] - // TODO: Obsolete this + [Obsolete("This method is only meant to be used by source generators")] public abstract class BaseCompiledSetup( Pretend pretend, MethodInfo methodInfo, diff --git a/src/Pretender/Internals/Cache.cs b/src/Pretender/Internals/Cache.cs index 2682df4..af9e148 100644 --- a/src/Pretender/Internals/Cache.cs +++ b/src/Pretender/Internals/Cache.cs @@ -3,10 +3,11 @@ namespace Pretender.Internals { [EditorBrowsable(EditorBrowsableState.Never)] - // TODO: Obsolete + [Obsolete("This method is only meant to be used by source generators")] public static class Cache { [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is only meant to be used by source generators")] public static readonly Matcher NoOpMatcher = delegate (CallInfo callInfo, object? target) { return true; diff --git a/src/Pretender/Internals/ReturningCompiledSetup.cs b/src/Pretender/Internals/ReturningCompiledSetup.cs index fd7b5af..ff71c32 100644 --- a/src/Pretender/Internals/ReturningCompiledSetup.cs +++ b/src/Pretender/Internals/ReturningCompiledSetup.cs @@ -7,6 +7,7 @@ namespace Pretender.Internals [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is only meant to be used by source generators")] public class ReturningCompiledSetup(Pretend pretend, MethodInfo methodInfo, Matcher matcher, object? target, TResult defaultValue) : BaseCompiledSetup(pretend, methodInfo, matcher, target), IPretendSetup { diff --git a/src/Pretender/Internals/SetupWrapper.cs b/src/Pretender/Internals/SetupWrapper.cs deleted file mode 100644 index 8fc7f25..0000000 --- a/src/Pretender/Internals/SetupWrapper.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Diagnostics.CodeAnalysis; - -namespace Pretender.Internals -{ - public readonly struct SetupWrapper : IEquatable> - { - private readonly IPretendSetup _setup; - private readonly int _hashCode; - - public SetupWrapper(IPretendSetup setup, int hashCode) - { - _setup = setup; - _hashCode = hashCode; - } - - public readonly IPretendSetup Setup => _setup; - - public bool Equals(SetupWrapper other) - { - return _hashCode == other._hashCode; - } - - public override bool Equals([NotNullWhen(true)] object? obj) - { - return obj is SetupWrapper otherWrapper && Equals(otherWrapper); - } - - public override int GetHashCode() - { - return _hashCode; - } - } -} diff --git a/src/Pretender/Internals/VoidCompiledSetup.cs b/src/Pretender/Internals/VoidCompiledSetup.cs index 158cf9f..fa974cf 100644 --- a/src/Pretender/Internals/VoidCompiledSetup.cs +++ b/src/Pretender/Internals/VoidCompiledSetup.cs @@ -5,6 +5,7 @@ namespace Pretender.Internals { [EditorBrowsable(EditorBrowsableState.Never)] + [Obsolete("This method is only meant to be used by source generators")] public class VoidCompiledSetup(Pretend pretend, MethodInfo methodInfo, Matcher matcher, object? target) : BaseCompiledSetup(pretend, methodInfo, matcher, target), IPretendSetup { diff --git a/src/Pretender/Pretend.cs b/src/Pretender/Pretend.cs index 10d9404..7913449 100644 --- a/src/Pretender/Pretend.cs +++ b/src/Pretender/Pretend.cs @@ -41,7 +41,7 @@ public void Verify(Func verifyExpression, Called called) // TODO: VerifySet? [EditorBrowsable(EditorBrowsableState.Never)] - // TODO: Make this obsolete + [Obsolete("This method is only meant to be used by source generators")] [StackTraceHidden] public void Verify(IPretendSetup pretendSetup, Called called) { @@ -63,7 +63,7 @@ public void Verify(IPretendSetup pretendSetup, Called called) } [EditorBrowsable(EditorBrowsableState.Never)] - // TODO: Make this obsolete + [Obsolete("This method is only meant to be used by source generators")] public void Handle(CallInfo callInfo) { _calls ??= []; @@ -87,7 +87,7 @@ private string DebuggerToString() private List>? _setups; [EditorBrowsable(EditorBrowsableState.Never)] - // TODO: Make Obsolete + [Obsolete("This method is only meant to be used by source generators")] public IPretendSetup GetOrCreateSetup(int hashCode, Func, Action, IPretendSetup> setupCreator, Action setupExpression) { _setups ??= []; @@ -97,7 +97,7 @@ public IPretendSetup GetOrCreateSetup(int hashCode, Func, Action GetOrCreateSetup(int hashCode, Func, Func, IPretendSetup> setupCreator, Func setupExpression) { _setups ??= []; diff --git a/test/SourceGeneratorTests/TestBase.cs b/test/SourceGeneratorTests/TestBase.cs index 9528f4f..5108881 100644 --- a/test/SourceGeneratorTests/TestBase.cs +++ b/test/SourceGeneratorTests/TestBase.cs @@ -109,7 +109,16 @@ public async Task RunAndComparePartialAsync(string source, [CallerMemberName] st }); } - private void CompareAgainstBaseline(GeneratedSourceResult result, string testMethodName = null!) + public async Task RunAndCompareAsync(string source, [CallerMemberName] string? testMethodName = null) + { + var (result, _) = await RunGeneratorAsync(source); + Assert.All(result.GeneratedSources, s => + { + CompareAgainstBaseline(s, testMethodName); + }); + } + + private void CompareAgainstBaseline(GeneratedSourceResult result, string? testMethodName = null) { var normalizedName = result.HintName[..^3].Replace('.', '_') + ".cs"; #if !GENERATE_SOURCE