-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f84bf08
commit 58066b5
Showing
21 changed files
with
267 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Pretender; | ||
|
||
namespace Example.Tests | ||
{ | ||
public static class FieldConstants | ||
{ | ||
public const string MyConstant = "something"; | ||
} | ||
|
||
public interface IFieldTest | ||
{ | ||
int MyMethod(string myArg); | ||
} | ||
|
||
|
||
public class FieldTests | ||
{ | ||
[Theory] | ||
[InlineData("something", true)] | ||
[InlineData("something_else", false)] | ||
public void ReferenceFieldConstant(string actualArg, bool shouldMatch) | ||
{ | ||
var pretend = Pretend.That<IFieldTest>(); | ||
|
||
pretend | ||
.Setup(i => i.MyMethod(FieldConstants.MyConstant)) | ||
.Returns(1); | ||
|
||
var test = pretend.Create(); | ||
|
||
if (false) | ||
{ | ||
throw new Exception("something"); | ||
} | ||
|
||
var result = test.MyMethod(actualArg); | ||
|
||
Assert.Equal(shouldMatch, result == 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Pretender.SourceGenerator/Emitter/CapturedArgumentEmitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Pretender.SourceGenerator.SetupArguments; | ||
using Pretender.SourceGenerator.Writing; | ||
|
||
namespace Pretender.SourceGenerator.Emitter | ||
{ | ||
internal class CapturedArgumentEmitter : SetupArgumentEmitter | ||
{ | ||
public CapturedArgumentEmitter(SetupArgumentSpec argumentSpec) : base(argumentSpec) | ||
{ | ||
|
||
} | ||
|
||
public override bool NeedsCapturer => true; | ||
|
||
public override void EmitArgumentMatcher(IndentedTextWriter writer, CancellationToken cancellationToken) | ||
{ | ||
EmitArgumentAccessor(writer); | ||
writer.WriteLine($"var {Parameter.Name}_capturedArg = ({Parameter.Type.ToUnknownTypeString()})capturedArguments[{Parameter.Ordinal}];"); | ||
writer.WriteLine($"if ({Parameter.Name}_arg != {Parameter.Name}_capturedArg)"); | ||
using (writer.WriteBlock()) | ||
{ | ||
writer.WriteLine("return false;"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
test/SourceGeneratorTests/Baselines/MainTests/FieldReference/Pretender_g.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// <auto-generated> | ||
|
||
#nullable enable annotations | ||
#nullable disable warnings | ||
|
||
// Suppress warnings about [Obsolete] member usage in generated code. | ||
#pragma warning disable CS0612, CS0618 | ||
|
||
namespace System.Runtime.CompilerServices | ||
{ | ||
using System; | ||
using System.CodeDom.Compiler; | ||
|
||
[GeneratedCode("Pretender.SourceGenerator", "1.0.0.0")] | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
file sealed class InterceptsLocationAttribute : Attribute | ||
{ | ||
public InterceptsLocationAttribute(string filePath, int line, int column) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
namespace Pretender.SourceGeneration | ||
{ | ||
using System; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Tasks; | ||
using Pretender; | ||
using Pretender.Internals; | ||
|
||
file class PretendITest : global::FieldReference.ITest | ||
{ | ||
public static readonly MethodInfo Method_MethodInfo = typeof(PretendITest).GetMethod(nameof(Method))!; | ||
|
||
private readonly ICallHandler _callHandler; | ||
|
||
public PretendITest(ICallHandler callHandler) | ||
{ | ||
_callHandler = callHandler; | ||
} | ||
|
||
public void Method(string arg) | ||
{ | ||
object?[] __arguments__ = [arg]; | ||
var __callInfo__ = new CallInfo(Method_MethodInfo, __arguments__); | ||
_callHandler.Handle(__callInfo__); | ||
} | ||
} | ||
|
||
file static class SetupInterceptors | ||
{ | ||
[InterceptsLocation(@"MyTest.cs", 22, 17)] | ||
internal static IPretendSetup<global::FieldReference.ITest> Setup0(this Pretend<global::FieldReference.ITest> pretend, Action<global::FieldReference.ITest> setupExpression) | ||
{ | ||
return pretend.GetOrCreateSetup(0, static (pretend, expr) => | ||
{ | ||
Matcher matchCall = (callInfo, setup) => | ||
{ | ||
var singleUseCallHandler = new SingleUseCallHandler(); | ||
var fake = new PretendITest(singleUseCallHandler); | ||
|
||
var listener = MatcherListener.StartListening(); | ||
try | ||
{ | ||
setup.Method.Invoke(setup.Target, [fake]); | ||
} | ||
finally | ||
{ | ||
listener.Dispose(); | ||
} | ||
|
||
var capturedArguments = singleUseCallHandler.Arguments; | ||
|
||
var arg_arg = (string)callInfo.Arguments[0]; | ||
var arg_capturedArg = (string)capturedArguments[0]; | ||
if (arg_arg != arg_capturedArg) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
}; | ||
return new VoidCompiledSetup<global::FieldReference.ITest>(); | ||
}, setupExpression); | ||
} | ||
} | ||
|
||
file static class VerifyInterceptors | ||
{ | ||
} | ||
|
||
file static class CreateInterceptors | ||
{ | ||
} | ||
} |
Oops, something went wrong.