Skip to content

Commit

Permalink
Rename the source generators
Browse files Browse the repository at this point in the history
Adding generator for generating extension method when IOptions is referenced

Bumping TFM to net8 for the tests
  • Loading branch information
Keboo committed Feb 8, 2024
1 parent 9b1cf1b commit f2a9247
Show file tree
Hide file tree
Showing 29 changed files with 511 additions and 89 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="coverlet.collector" Version="6.0.0"/>
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
<PackageVersion Include="Microsoft.CodeAnalysis" Version="4.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" Version="1.1.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Moq" Version="4.20.70" />
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj">
<ProjectReference Include="..\..\Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -12,14 +12,14 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="coverlet.collector" >
<PackageReference Include="coverlet.collector">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj">
<ProjectReference Include="..\..\Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
Expand All @@ -20,13 +20,9 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<!--<ItemGroup>
<PackageReference Include="Moq.AutoMock" Version="3.3.0-ci0233" />
</ItemGroup>-->


<ItemGroup>
<ProjectReference Include="..\..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj">
<ProjectReference Include="..\..\Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" />
</ItemGroup>

</Project>
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; }
}
2 changes: 1 addition & 1 deletion Moq.AutoMock.Tests/Moq.AutoMock.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Copyright>Copyright © 2020</Copyright>

<IsPackable>false</IsPackable>
Expand Down
25 changes: 13 additions & 12 deletions Moq.AutoMock.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Packages.props = Directory.Packages.props
global.json = global.json
LICENSE.md = LICENSE.md
NuGet.config = NuGet.config
README.md = README.md
EndProjectSection
EndProject
Expand All @@ -30,16 +31,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMock.Generator.Exam
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMock.Generator.Example.xUnit", "GeneratorTests\Moq.AutoMock.Generator.Example.xUnit\Moq.AutoMock.Generator.Example.xUnit.csproj", "{586D1D6A-3EE2-4AB8-988A-CEB810EF7787}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMocker.TestGenerator", "Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj", "{710AF8FE-BB9E-4FE9-ABF7-0CE8BD212BB4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMocker.TestGenerator.Tests", "Moq.AutoMocker.TestGenerator.Tests\Moq.AutoMocker.TestGenerator.Tests.csproj", "{F3663663-0123-4DA3-9BDD-95C91AFA4596}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{820CF7FE-8E92-4500-BE62-55AF67007838}"
ProjectSection(SolutionItems) = preProject
.github\dependabot.yml = .github\dependabot.yml
.github\workflows\dotnetcore.yml = .github\workflows\dotnetcore.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMocker.Generators", "Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj", "{FE1C406B-D16B-4037-9D67-ECE2B6847AEB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Moq.AutoMocker.Generators.Tests", "Moq.AutoMocker.Generators.Tests\Moq.AutoMocker.Generators.Tests.csproj", "{7820E7C8-7169-4A28-9D1B-BB38FC4E2923}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,14 +75,14 @@ Global
{586D1D6A-3EE2-4AB8-988A-CEB810EF7787}.Debug|Any CPU.Build.0 = Debug|Any CPU
{586D1D6A-3EE2-4AB8-988A-CEB810EF7787}.Release|Any CPU.ActiveCfg = Release|Any CPU
{586D1D6A-3EE2-4AB8-988A-CEB810EF7787}.Release|Any CPU.Build.0 = Release|Any CPU
{710AF8FE-BB9E-4FE9-ABF7-0CE8BD212BB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{710AF8FE-BB9E-4FE9-ABF7-0CE8BD212BB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{710AF8FE-BB9E-4FE9-ABF7-0CE8BD212BB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{710AF8FE-BB9E-4FE9-ABF7-0CE8BD212BB4}.Release|Any CPU.Build.0 = Release|Any CPU
{F3663663-0123-4DA3-9BDD-95C91AFA4596}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3663663-0123-4DA3-9BDD-95C91AFA4596}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3663663-0123-4DA3-9BDD-95C91AFA4596}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3663663-0123-4DA3-9BDD-95C91AFA4596}.Release|Any CPU.Build.0 = Release|Any CPU
{FE1C406B-D16B-4037-9D67-ECE2B6847AEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE1C406B-D16B-4037-9D67-ECE2B6847AEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE1C406B-D16B-4037-9D67-ECE2B6847AEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE1C406B-D16B-4037-9D67-ECE2B6847AEB}.Release|Any CPU.Build.0 = Release|Any CPU
{7820E7C8-7169-4A28-9D1B-BB38FC4E2923}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7820E7C8-7169-4A28-9D1B-BB38FC4E2923}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7820E7C8-7169-4A28-9D1B-BB38FC4E2923}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7820E7C8-7169-4A28-9D1B-BB38FC4E2923}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion Moq.AutoMock/ConstructorTestsAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Moq.AutoMock;
/// <summary>
/// An attribute used by Moq.AutoMock.TestGenerator to generate unit tests for null constructor arguments.
/// An attribute used by Moq.AutoMock.Generators to generate unit tests for null constructor arguments.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ConstructorTestsAttribute : Attribute
Expand Down
6 changes: 3 additions & 3 deletions Moq.AutoMock/Moq.AutoMock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\assets\NuGetIcon.png" Pack="true" PackagePath="\" />
<None Include="..\LICENSE" Pack="true" PackagePath="" />
<Compile Include="..\System.Diagnostics.CodeAnalysis.cs" />
<None Include="..\Moq.AutoMocker.TestGenerator\bin\$(Configuration)\netstandard2.0\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
<None Include="..\Moq.AutoMocker.Generators\bin\$(Configuration)\netstandard2.0\*.dll" Pack="True" PackagePath="analyzers\dotnet\cs" />
</ItemGroup>

<ItemGroup>
<None Remove="..\Moq.AutoMocker.TestGenerator\bin\Debug\netstandard2.0\Moq.AutoMocker.TestGenerator.dll" />
<None Remove="..\Moq.AutoMocker.Generators\bin\Debug\netstandard2.0\Moq.AutoMocker.Generators.dll" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

namespace Moq.AutoMocker.TestGenerator.Tests;
namespace Moq.AutoMocker.Generators.Tests;

public static class CSharpSourceGeneratorVerifier<TSourceGenerator>
where TSourceGenerator : ISourceGenerator, new()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Moq.AutoMocker.TestGenerator\Moq.AutoMocker.TestGenerator.csproj" />
<ProjectReference Include="..\Moq.AutoMock\Moq.AutoMock.csproj" />
</ItemGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>

<IsPackable>false</IsPackable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="Microsoft.CodeAnalysis" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.MSTest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Moq.AutoMocker.Generators\Moq.AutoMocker.Generators.csproj" />
<ProjectReference Include="..\Moq.AutoMock\Moq.AutoMock.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit f2a9247

Please sign in to comment.