Skip to content

Commit

Permalink
Emit more Generator Trivia
Browse files Browse the repository at this point in the history
  • Loading branch information
justindbaur committed Dec 24, 2023
1 parent 4b5f80d commit 347dd0c
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 46 deletions.
14 changes: 9 additions & 5 deletions src/Pretender.SourceGenerator/Emitter/GrandEmitter.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -31,6 +26,15 @@ public string Emit(CancellationToken cancellationToken)
{
var writer = new IndentedTextWriter();

writer.WriteLine("// <auto-generated>");
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();
Expand Down
1 change: 0 additions & 1 deletion src/Pretender.SourceGenerator/PretenderSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Pretender.SourceGenerator.Emitter;
using Pretender.SourceGenerator.Invocation;
Expand Down
3 changes: 3 additions & 0 deletions src/Pretender/IPretendSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public interface IPretendSetup<T>

[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<T, TResult> : IPretendSetup<T>
Expand Down
2 changes: 1 addition & 1 deletion src/Pretender/Internals/BaseCompiledSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Pretend<T> pretend,
MethodInfo methodInfo,
Expand Down
3 changes: 2 additions & 1 deletion src/Pretender/Internals/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Pretender/Internals/ReturningCompiledSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, TResult>(Pretend<T> pretend, MethodInfo methodInfo, Matcher matcher, object? target, TResult defaultValue)
: BaseCompiledSetup<T>(pretend, methodInfo, matcher, target), IPretendSetup<T, TResult>
{
Expand Down
33 changes: 0 additions & 33 deletions src/Pretender/Internals/SetupWrapper.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/Pretender/Internals/VoidCompiledSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(Pretend<T> pretend, MethodInfo methodInfo, Matcher matcher, object? target)
: BaseCompiledSetup<T>(pretend, methodInfo, matcher, target), IPretendSetup<T>
{
Expand Down
8 changes: 4 additions & 4 deletions src/Pretender/Pretend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void Verify<TReturn>(Func<T, TReturn> 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<T> pretendSetup, Called called)
{
Expand All @@ -63,7 +63,7 @@ public void Verify(IPretendSetup<T> 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 ??= [];
Expand All @@ -87,7 +87,7 @@ private string DebuggerToString()
private List<IPretendSetup<T>>? _setups;

[EditorBrowsable(EditorBrowsableState.Never)]
// TODO: Make Obsolete
[Obsolete("This method is only meant to be used by source generators")]
public IPretendSetup<T> GetOrCreateSetup(int hashCode, Func<Pretend<T>, Action<T>, IPretendSetup<T>> setupCreator, Action<T> setupExpression)
{
_setups ??= [];
Expand All @@ -97,7 +97,7 @@ public IPretendSetup<T> GetOrCreateSetup(int hashCode, Func<Pretend<T>, Action<T
}

[EditorBrowsable(EditorBrowsableState.Never)]
// TODO: Make Obsolete
[Obsolete("This method is only meant to be used by source generators")]
public IPretendSetup<T, TResult> GetOrCreateSetup<TResult>(int hashCode, Func<Pretend<T>, Func<T, TResult>, IPretendSetup<T, TResult>> setupCreator, Func<T, TResult> setupExpression)
{
_setups ??= [];
Expand Down
11 changes: 10 additions & 1 deletion test/SourceGeneratorTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 347dd0c

Please sign in to comment.