Skip to content

Commit f518d67

Browse files
committed
Add CustomPlugins folder with a sample generator plugin
1 parent d326d79 commit f518d67

18 files changed

+322
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace CalculatorApp;
2+
3+
public class Calculator(ICalculatorConfiguration _calculatorConfiguration) : ICalculator
4+
{
5+
private readonly Stack<int> _numbers = new();
6+
7+
public void Enter(int number)
8+
{
9+
_numbers.Push(number);
10+
}
11+
12+
public void Reset()
13+
{
14+
_numbers.Clear();
15+
}
16+
17+
public int GetResult()
18+
{
19+
return _numbers.Peek();
20+
}
21+
22+
public void Add()
23+
{
24+
_numbers.Push(_numbers.Pop() + _numbers.Pop());
25+
}
26+
27+
public void Multiply()
28+
{
29+
if (!_calculatorConfiguration.AllowMultiply)
30+
throw new InvalidOperationException("Multiplication is not allowed.");
31+
32+
_numbers.Push(_numbers.Pop() * _numbers.Pop());
33+
}
34+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>latest</LangVersion>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CalculatorApp;
2+
3+
public interface ICalculator
4+
{
5+
void Enter(int number);
6+
void Reset();
7+
int GetResult();
8+
void Add();
9+
void Multiply();
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace CalculatorApp;
2+
public interface ICalculatorConfiguration
3+
{
4+
bool AllowMultiply { get; }
5+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using CustomPlugins.TagDecoratorGeneratorPlugin.ReqnrollPlugin;
2+
using Reqnroll.Generator.Plugins;
3+
using Reqnroll.Generator.UnitTestConverter;
4+
using Reqnroll.Infrastructure;
5+
using Reqnroll.UnitTestProvider;
6+
7+
[assembly: GeneratorPlugin(typeof(CustomGeneratorPlugin))]
8+
9+
namespace CustomPlugins.TagDecoratorGeneratorPlugin.ReqnrollPlugin;
10+
11+
public class CustomGeneratorPlugin : IGeneratorPlugin
12+
{
13+
public void Initialize(GeneratorPluginEvents generatorPluginEvents, GeneratorPluginParameters generatorPluginParameters,
14+
UnitTestProviderConfiguration unitTestProviderConfiguration)
15+
{
16+
generatorPluginEvents.RegisterDependencies += RegisterDependencies;
17+
}
18+
19+
private void RegisterDependencies(object sender, RegisterDependenciesEventArgs eventArgs)
20+
{
21+
eventArgs.ObjectContainer.RegisterTypeAs<StaApartmentStateTagDecorator, ITestMethodTagDecorator>(StaApartmentStateTagDecorator.TAG_NAME);
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
7+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
8+
<NuspecFile>$(MSBuildThisFileDirectory)CustomPlugins.TagDecoratorGeneratorPlugin.nuspec</NuspecFile>
9+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Reqnroll.CustomPlugin" Version="2.1.1-local" />
14+
</ItemGroup>
15+
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
2+
<metadata>
3+
<id>CustomPlugins.TagDecoratorGeneratorPlugin</id>
4+
<version>1.0.0-local</version>
5+
<title>CustomPlugins.TagDecoratorGeneratorPlugin</title>
6+
<authors>Gaspar Nagy</authors>
7+
<owners>Gaspar Nagy</owners>
8+
<description>Package to test custom generator plugins</description>
9+
<summary>Package to test custom generator plugins</summary>
10+
<language>en-US</language>
11+
<projectUrl>https://www.reqnroll.net</projectUrl>
12+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
13+
<license type="expression">BSD-3-Clause</license>
14+
<tags>reqnroll plugin</tags>
15+
<dependencies>
16+
<dependency id="Reqnroll" version="2.1.1-local" />
17+
</dependencies>
18+
</metadata>
19+
<files>
20+
<file src="build\**\*" target="build"/>
21+
22+
<file src="bin\Debug\netstandard2.0\*" target="build\netstandard2.0"/>
23+
</files>
24+
</package>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.CodeDom;
2+
using Reqnroll.Generator;
3+
using Reqnroll.Generator.UnitTestConverter;
4+
5+
namespace CustomPlugins.TagDecoratorGeneratorPlugin.ReqnrollPlugin;
6+
public class StaApartmentStateTagDecorator : ITestMethodTagDecorator
7+
{
8+
public static readonly string TAG_NAME = "staApartmentState";
9+
private readonly ITagFilterMatcher _tagFilterMatcher;
10+
11+
public StaApartmentStateTagDecorator(ITagFilterMatcher tagFilterMatcher)
12+
{
13+
_tagFilterMatcher = tagFilterMatcher;
14+
}
15+
16+
public bool CanDecorateFrom(string tagName, TestClassGenerationContext generationContext, CodeMemberMethod testMethod)
17+
{
18+
return _tagFilterMatcher.Match(TAG_NAME, tagName);
19+
}
20+
21+
public void DecorateFrom(string tagName, TestClassGenerationContext generationContext, CodeMemberMethod testMethod)
22+
{
23+
var attribute = new CodeAttributeDeclaration(
24+
"NUnit.Framework.ApartmentAttribute",
25+
new CodeAttributeArgument(
26+
new CodeFieldReferenceExpression(
27+
new CodeTypeReferenceExpression(typeof(System.Threading.ApartmentState)),
28+
"STA")));
29+
30+
testMethod.CustomAttributes.Add(attribute);
31+
}
32+
33+
public int Priority => 0;
34+
public bool RemoveProcessedTags => false;
35+
public bool ApplyOtherDecoratorsForProcessedTags => false;
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" TreatAsLocalProperty="TaskFolder;TaskAssembly">
2+
<ItemGroup>
3+
<ReqnrollGeneratorPlugins Include="$(_TagDecoratorGeneratorPluginPath)" />
4+
</ItemGroup>
5+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<PropertyGroup>
3+
<_TagDecoratorGeneratorPluginFramework>netstandard2.0</_TagDecoratorGeneratorPluginFramework>
4+
5+
<_TagDecoratorGeneratorPluginPath>$(MSBuildThisFileDirectory)$(_TagDecoratorGeneratorPluginFramework)\CustomPlugins.TagDecoratorGeneratorPlugin.ReqnrollPlugin.dll</_TagDecoratorGeneratorPluginPath>
6+
</PropertyGroup>
7+
</Project>

0 commit comments

Comments
 (0)