-
Notifications
You must be signed in to change notification settings - Fork 0
feat: support ref and out parameter migration #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| using System.Text.RegularExpressions; | ||
| using Moq; | ||
| using Range = Moq.Range; | ||
|
|
||
| namespace Mockolate.Migration.Example.Tests; | ||
|
|
||
| public class MoqMigrationExamples | ||
| { | ||
| [Fact] | ||
| public async Task ExpectedMigrationResult() | ||
| { | ||
| IFoo mock = IFoo.CreateMock(); | ||
|
|
||
| mock.Mock.Setup.DoSomething("ping").Returns(true); | ||
|
vbreuss marked this conversation as resolved.
|
||
|
|
||
| // out arguments | ||
| string outString = "ack"; | ||
| // TryParse will return true, and the out argument will return "ack", lazy evaluated | ||
| mock.Mock.Setup.TryParse("ping", It.IsOut(() => outString)).Returns(true); | ||
|
|
||
| // ref arguments | ||
| Bar instance = new(); | ||
| // Only matches if the ref argument to the invocation is the same instance | ||
| mock.Mock.Setup.Submit(It.IsRef<Bar>(_ => instance)).Returns(true); | ||
|
|
||
| // access invocation arguments when returning a value | ||
| mock.Mock.Setup.DoSomethingStringy(It.IsAny<string>()) | ||
| .Returns(s => s.ToLower()); | ||
| // Multiple parameters overloads available | ||
|
|
||
| // throwing when invoked with specific parameters | ||
| mock.Mock.Setup.DoSomething("reset").Throws<InvalidOperationException>(); | ||
| mock.Mock.Setup.DoSomething("").Throws(new ArgumentException("command")); | ||
|
|
||
| // lazy evaluating return value | ||
| int count = 1; | ||
| mock.Mock.Setup.GetCount().Returns(() => count); | ||
|
|
||
|
|
||
| /* ------ Matching Argument ------ */ | ||
| // any value | ||
| mock.Mock.Setup.DoSomething(It.IsAny<string>()).Returns(true); | ||
|
|
||
| // any value passed in a `ref` parameter (requires Moq 4.8 or later): | ||
| mock.Mock.Setup.Submit(It.IsAnyRef<Bar>()).Returns(true); | ||
|
|
||
| // matching Func<int>, lazy evaluated | ||
| mock.Mock.Setup.Add(It.Satisfies<int>(i => i % 2 == 0)).Returns(true); | ||
|
|
||
| // matching ranges | ||
| mock.Mock.Setup.Add(It.IsInRange(0, 10)).Returns(true); | ||
|
|
||
| // matching regex | ||
| mock.Mock.Setup.DoSomethingStringy(It.Matches("[a-d]+").AsRegex(RegexOptions.IgnoreCase)).Returns("foo"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <see href="https://github.com/devlooped/moq/wiki/Quickstart" /> | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task MoqCreation() | ||
| { | ||
| #pragma warning disable MockolateM001 | ||
| Mock<IFoo> mock = new(); | ||
| #pragma warning restore MockolateM001 | ||
|
|
||
|
vbreuss marked this conversation as resolved.
|
||
| mock.Setup(foo => foo.DoSomething("ping")).Returns(true); | ||
|
|
||
| // out arguments | ||
| string outString = "ack"; | ||
| // TryParse will return true, and the out argument will return "ack", lazy evaluated | ||
| mock.Setup(foo => foo.TryParse("ping", out outString)).Returns(true); | ||
|
|
||
| // ref arguments | ||
| Bar instance = new(); | ||
| // Only matches if the ref argument to the invocation is the same instance | ||
| mock.Setup(foo => foo.Submit(ref instance)).Returns(true); | ||
|
|
||
| // access invocation arguments when returning a value | ||
| mock.Setup(x => x.DoSomethingStringy(Moq.It.IsAny<string>())) | ||
| .Returns((string s) => s.ToLower()); | ||
| // Multiple parameters overloads available | ||
|
|
||
| // throwing when invoked with specific parameters | ||
| mock.Setup(foo => foo.DoSomething("reset")).Throws<InvalidOperationException>(); | ||
| mock.Setup(foo => foo.DoSomething("")).Throws(new ArgumentException("command")); | ||
|
|
||
| // lazy evaluating return value | ||
| int count = 1; | ||
| mock.Setup(foo => foo.GetCount()).Returns(() => count); | ||
|
|
||
|
|
||
| /* ------ Matching Argument ------ */ | ||
| // any value | ||
| mock.Setup(foo => foo.DoSomething(Moq.It.IsAny<string>())).Returns(true); | ||
|
|
||
| // any value passed in a `ref` parameter (requires Moq 4.8 or later): | ||
| mock.Setup(foo => foo.Submit(ref Moq.It.Ref<Bar>.IsAny)).Returns(true); | ||
|
|
||
| // matching Func<int>, lazy evaluated | ||
| mock.Setup(foo => foo.Add(Moq.It.Is<int>(i => i % 2 == 0))).Returns(true); | ||
|
|
||
| // matching ranges | ||
| mock.Setup(foo => foo.Add(Moq.It.IsInRange(0, 10, Range.Inclusive))).Returns(true); | ||
|
|
||
| // matching regex | ||
| mock.Setup(x => x.DoSomethingStringy(Moq.It.IsRegex("[a-d]+", RegexOptions.IgnoreCase))).Returns("foo"); | ||
| } | ||
|
|
||
| public interface IFoo | ||
| { | ||
| Bar Bar { get; set; } | ||
| string Name { get; set; } | ||
| int Value { get; set; } | ||
| bool DoSomething(string value); | ||
| bool DoSomething(int number, string value); | ||
| Task<bool> DoSomethingAsync(); | ||
| string DoSomethingStringy(string value); | ||
| bool TryParse(string value, out string outputValue); | ||
| bool Submit(ref Bar bar); | ||
| int GetCount(); | ||
| bool Add(int value); | ||
| } | ||
|
|
||
| public class Bar | ||
| { | ||
| public virtual Baz Baz { get; set; } | ||
|
Check warning on line 127 in Tests/Mockolate.Migration.Example.Tests/MoqMigrationExamples.cs
|
||
| public virtual bool Submit() => false; | ||
| } | ||
|
|
||
| public class Baz | ||
| { | ||
| public virtual string Name { get; set; } | ||
|
Check warning on line 133 in Tests/Mockolate.Migration.Example.Tests/MoqMigrationExamples.cs
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.