Skip to content

Commit

Permalink
Refine Matcher Use
Browse files Browse the repository at this point in the history
  • Loading branch information
justindbaur committed Apr 22, 2024
1 parent 78ef8f6 commit 645d9a8
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 50 deletions.
75 changes: 75 additions & 0 deletions example/MatcherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Pretender;

namespace Example.Tests;

public class MatcherTests
{
[Fact]
public void OneAnyMatcher_OneAnonymousMatcher()
{
var testInterfacePretend = Pretend.That<ITestInterface>();

testInterfacePretend
.Setup(i => i.Test(It.IsAny<string>(), It.Is<string>(s => s == "Hi")))
.Returns(1);

var testInterface = testInterfacePretend.Create();

var returnValue = testInterface.Test("Yo", "Hi");
Assert.Equal(1, returnValue);
}

[Fact]
public void TwoAnonymousMatchers()
{
var testInterfacePretend = Pretend.That<ITestInterface>();

testInterfacePretend
.Setup(i => i.Test(It.Is<string>(s => s == "Yo"), It.Is<string>(s => s == "Hi")))
.Returns(1);

var testInterface = testInterfacePretend.Create();

var returnValue = testInterface.Test("Yo", "Hi");
Assert.Equal(1, returnValue);
}

[Fact]
public void AnonymousMatcherThatCapturesLocal()
{
var testInterfacePretend = Pretend.That<ITestInterface>();

string local = "Yo";

testInterfacePretend
.Setup(i => i.Test(It.Is<string>(s => s == local), It.Is<string>(s => s == "Hi")))
.Returns(1);

var testInterface = testInterfacePretend.Create();

var returnValue = testInterface.Test("Yo", "Hi");
Assert.Equal(1, returnValue);
}

public string TestProperty { get; set; } = "Yo";

[Fact]
public void AnonymousMatcherThatCapturesProperty()
{
var testInterfacePretend = Pretend.That<ITestInterface>();

testInterfacePretend
.Setup(i => i.Test(It.Is<string>(s => s == TestProperty), It.Is<string>(s => s == "Hi")))
.Returns(1);

var testInterface = testInterfacePretend.Create();

var returnValue = testInterface.Test("Yo", "Hi");
Assert.Equal(1, returnValue);
}
}

public interface ITestInterface
{
int Test(string arg1, string arg2);
}
22 changes: 0 additions & 22 deletions example/Random.cs

This file was deleted.

29 changes: 6 additions & 23 deletions example/UnitTest1.cs → example/SimpleTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Pretender;

namespace Example;
namespace Example.Tests;

public class UnitTest1
public class SimpleTests
{
[Fact]
public void Test1()
public void OneLiteral_OneAnyMatcher()
{
var pretendMyInterface = Pretend.That<IInterface>()
.Setup(i => i.Greeting("Mike", It.IsAny<int>()))
Expand All @@ -17,7 +17,7 @@ public void Test1()
}

[Fact]
public async Task Test2()
public async Task OneCapturedLocal()
{
var pretend = Pretend.That<IMyInterface>();

Expand All @@ -37,7 +37,7 @@ public async Task Test2()
}

[Fact]
public void Test3()
public void OneLiteral_OneFunctionMatcher()
{
var pretend = Pretend.That<IInterface>();

Expand All @@ -52,35 +52,18 @@ public void Test3()
Assert.Equal("2", response);

setup.Verify(1);
setup.Verify(..2);
}
}

public interface IMyInterface
{
/// <summary>
/// Hello
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
Task<string> Greeting(string? name);

void Something();
}

public interface IMyOtherInterface
{
void Greeting();
}

/// <summary>
/// My Information
/// </summary>
public interface IInterface
{
string? Greeting(string name, int num);
}

public sealed class TestClass
{

}
15 changes: 13 additions & 2 deletions src/Pretender.SourceGenerator/Emitter/SetupActionEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,24 @@ public void Emit(IndentedTextWriter writer, CancellationToken cancellationToken)
if (_setupArgumentEmitters.Any(a => a.NeedsCapturer))
{
// TODO: Create single use call handler
writer.WriteLine("var singleUseCallHandler = new SingleUseCallHandler();");
if (returnType is null)
{
writer.WriteLine("var singleUseCallHandler = new SingleUseCallHandler();");
}
else
{
writer.WriteLine($"var singleUseCallHandler = new SingleUseCallHandler<{returnType.ToFullDisplayString()}>();");
}

writer.WriteLine($"var fake = new {_knownTypeSymbols.GetPretendName(PretendType)}(singleUseCallHandler);");
// Emit and run capturer

writer.WriteLine();
writer.WriteLine("var listener = MatcherListener.StartListening();");
writer.WriteLine("setup.Method.Invoke(setup.Target, [fake]);");

writer.WriteLine("listener.Dispose();");
writer.WriteLine();

writer.WriteLine("var capturedArguments = singleUseCallHandler.Arguments;");
writer.WriteLine();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pretender.SourceGenerator/Parser/SetupActionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void TraverseOperation(IOperation operation, ImmutableArray<InvocationCa
default:
#if DEBUG
// TODO: Figure out what operation caused this, it's not ideal to "randomly" support operations
Debugger.Launch();
// Debugger.Launch();
#endif
// Absolute fallback, most of our operations can be supported this way but it's nicer to be explicit
TraverseOperationList(operation.ChildOperations, invocationCandidates, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private static void Visit(IOperation? operation, ArgumentTracker tracker)
default:
#if DEBUG
// TODO: Figure out what operation this is
Debugger.Launch();
// Debugger.Launch();
// TODO: Report diagnostic?
// TODO: Do fallback support? by looping over ChildOperations?
#endif
Expand Down
19 changes: 18 additions & 1 deletion src/Pretender/Internals/SingleUseCallHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ namespace Pretender.Internals
/// <summary>
/// **FOR INTERNAL USE ONLY**
/// </summary>
public class SingleUseCallHandler : ICallHandler
public class SingleUseCallHandler<T> : ICallHandler
{
public object?[] Arguments { get; private set; } = null!;

/// <summary>
/// **FOR INTERNAL USE ONLY** Method signature subject to change.
/// </summary>
/// <param name="callInfo"></param>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This method is only meant to be used by source generators")]
public void Handle(CallInfo callInfo)
{
Arguments = callInfo.Arguments;
callInfo.ReturnValue = default(T);
}
}

public class SingleUserCallHandler : ICallHandler
{
public object?[] Arguments { get; private set; } = null!;

Expand Down

0 comments on commit 645d9a8

Please sign in to comment.