-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding generator for generating extension method when IOptions is ref…
…erenced
- Loading branch information
Showing
9 changed files
with
146 additions
and
2 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
19 changes: 19 additions & 0 deletions
19
GeneratorTests/Moq.AutoMock.Generator.Example.MSTest/ControllerWithOptionsTests.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,19 @@ | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Moq.AutoMock.Generator.Example.MSUnit; | ||
[TestClass] | ||
public class ControllerWithOptionsTests | ||
{ | ||
[TestMethod] | ||
public void CreateInstance_WithOptions_EmbedsOptions() | ||
{ | ||
AutoMocker mocker = new(); | ||
|
||
mocker.WithOptions<TestsOptions>(options => options.Number = 42); | ||
|
||
ControllerWithOptions controller = mocker.CreateInstance<ControllerWithOptions>(); | ||
|
||
Assert.AreEqual(42, controller.Options.Value.Number); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
GeneratorTests/Moq.AutoMock.Generator.Example.NUnit/ControllerWithOptionsTests.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,18 @@ | ||
using Microsoft.Extensions.Options; | ||
using NUnit.Framework; | ||
|
||
namespace Moq.AutoMock.Generator.Example.NUnit; | ||
public class ControllerWithOptionsTests | ||
{ | ||
[Test] | ||
public void CreateInstance_WithOptions_EmbedsOptions() | ||
{ | ||
AutoMocker mocker = new(); | ||
|
||
mocker.WithOptions<TestsOptions>(options => options.Number = 42); | ||
|
||
ControllerWithOptions controller = mocker.CreateInstance<ControllerWithOptions>(); | ||
|
||
Assert.That(42 == controller.Options.Value.Number); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
GeneratorTests/Moq.AutoMock.Generator.Example.xUnit/ControllerWithOptionsTests.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,18 @@ | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
namespace Moq.AutoMock.Generator.Example.xUnit; | ||
public class ControllerWithOptionsTests | ||
{ | ||
[Fact] | ||
public void CreateInstance_WithOptions_EmbedsOptions() | ||
{ | ||
AutoMocker mocker = new(); | ||
|
||
mocker.WithOptions<TestsOptions>(options => options.Number = 42); | ||
|
||
ControllerWithOptions controller = mocker.CreateInstance<ControllerWithOptions>(); | ||
|
||
Assert.Equal(42, controller.Options.Value.Number); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
GeneratorTests/Moq.AutoMocker.Generator.Example/ControllerWithOptions.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,12 @@ | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Moq.AutoMock.Generator.Example; | ||
public class ControllerWithOptions | ||
{ | ||
public IOptions<TestsOptions> Options { get; } | ||
|
||
public ControllerWithOptions(IOptions<TestsOptions> options) | ||
{ | ||
Options = options; | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
GeneratorTests/Moq.AutoMocker.Generator.Example/TestsOptions.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,6 @@ | ||
namespace Moq.AutoMock.Generator.Example; | ||
|
||
public class TestsOptions | ||
{ | ||
public int Number { get; set; } | ||
} |
66 changes: 66 additions & 0 deletions
66
Moq.AutoMocker.Generators/OptionsExtensionSourceGenerator.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,66 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Moq.AutoMocker.Generators; | ||
|
||
[Generator] | ||
public class OptionsExtensionSourceGenerator : ISourceGenerator | ||
{ | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
if (!ReferencesOptions(context.Compilation.ReferencedAssemblyNames)) | ||
{ | ||
return; | ||
} | ||
|
||
context.AddSource("AutoMocker.Options.cs", OptionsExtensionContent); | ||
} | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ } | ||
|
||
private static bool ReferencesOptions(IEnumerable<AssemblyIdentity> assemblies) | ||
{ | ||
foreach (AssemblyIdentity assembly in assemblies) | ||
{ | ||
if (assembly.Name.StartsWith("Microsoft.Extensions.Options")) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private const string OptionsExtensionContent = | ||
""" | ||
namespace Moq.AutoMock | ||
{ | ||
using Microsoft.Extensions.Options; | ||
|
||
public static class AutoMockerOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// This method sets up <see cref="AutoMocker"/> with various option related services for Microsoft's Option pattern, and allows their interception and manipulation in testing scenarios. | ||
/// </summary> | ||
/// <param name="mocker">The <see cref="AutoMocker"/> instance</param> | ||
/// <param name="configure">A delegate that can be used to configure an option instance of type TClass.</param> | ||
/// <typeparam name="TClass">The type of Options being configured.</typeparam> | ||
/// <returns>The same <see cref="AutoMocker"/> instance passed as parameter, allowing chained calls.</returns> | ||
public static AutoMocker WithOptions<TClass>(this AutoMocker mocker, Action<TClass>? configure = null) | ||
where TClass : class, new() | ||
{ | ||
if (mocker == null) throw new ArgumentNullException(nameof(mocker)); | ||
|
||
mocker.Use<IEnumerable<IConfigureOptions<TClass>>>(new[] { new ConfigureOptions<TClass>(configure) }); | ||
mocker.With<IOptionsMonitorCache<TClass>, OptionsCache<TClass>>(); | ||
mocker.With<IOptionsFactory<TClass>, OptionsFactory<TClass>>(); | ||
mocker.With<IOptionsMonitor<TClass>, OptionsMonitor<TClass>>(); | ||
mocker.With<IOptionsSnapshot<TClass>, OptionsManager<TClass>>(); | ||
TClass options = mocker.Get<IOptionsFactory<TClass>>().Create(string.Empty); | ||
mocker.Use(Options.Create(options)); | ||
mocker.Use(options); | ||
return mocker; | ||
} | ||
} | ||
} | ||
"""; | ||
} |