Skip to content

Commit 7b4d609

Browse files
authored
chore: extract nunit3 docs tests to separete test-project (#357)
1 parent 69cd0c0 commit 7b4d609

12 files changed

+330
-255
lines changed

FluentAssertions.Analyzers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentAssertions.Analyzers.
3232
EndProject
3333
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4", "src\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4.csproj", "{A819A8A5-C8F7-46AD-B0F2-44868070A188}"
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3", "src\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3.csproj", "{FBCDB423-7729-42CD-9279-2D0BECE37907}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -66,6 +68,10 @@ Global
6668
{A819A8A5-C8F7-46AD-B0F2-44868070A188}.Debug|Any CPU.Build.0 = Debug|Any CPU
6769
{A819A8A5-C8F7-46AD-B0F2-44868070A188}.Release|Any CPU.ActiveCfg = Release|Any CPU
6870
{A819A8A5-C8F7-46AD-B0F2-44868070A188}.Release|Any CPU.Build.0 = Release|Any CPU
71+
{FBCDB423-7729-42CD-9279-2D0BECE37907}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72+
{FBCDB423-7729-42CD-9279-2D0BECE37907}.Debug|Any CPU.Build.0 = Debug|Any CPU
73+
{FBCDB423-7729-42CD-9279-2D0BECE37907}.Release|Any CPU.ActiveCfg = Release|Any CPU
74+
{FBCDB423-7729-42CD-9279-2D0BECE37907}.Release|Any CPU.Build.0 = Release|Any CPU
6975
EndGlobalSection
7076
GlobalSection(SolutionProperties) = preSolution
7177
HideSolutionNode = FALSE
@@ -78,6 +84,7 @@ Global
7884
{2F84FE09-8CB4-48AE-A119-671C509213CF} = {72192514-FA22-4699-8EE1-39D34CFE1025}
7985
{2871B22C-AEFC-4C33-9BBF-695699E61B57} = {72192514-FA22-4699-8EE1-39D34CFE1025}
8086
{A819A8A5-C8F7-46AD-B0F2-44868070A188} = {8EFE7955-E63C-4055-A9FF-76C7CE0A1151}
87+
{FBCDB423-7729-42CD-9279-2D0BECE37907} = {8EFE7955-E63C-4055-A9FF-76C7CE0A1151}
8188
EndGlobalSection
8289
GlobalSection(ExtensibilityGlobals) = postSolution
8390
SolutionGuid = {4BF3D005-625C-4CEC-B3FB-298B956402BE}

scripts/generate-docs.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ function GenerateDocs {
2727
}
2828

2929
GenerateDocs -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs
30-
GenerateDocs -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4
30+
GenerateDocs -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4
31+
GenerateDocs -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3

scripts/run-docs-tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ function RunTestsAndValidate {
4242
}
4343

4444
RunTestsAndValidate -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs
45-
RunTestsAndValidate -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4
45+
RunTestsAndValidate -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit4
46+
RunTestsAndValidate -project FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs.Nunit3
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using NUnit.Framework;
3+
using NUnit.Framework.Interfaces;
4+
using NUnit.Framework.Internal;
5+
using NUnit.Framework.Internal.Commands;
6+
7+
namespace FluentAssertions.Analyzers.FluentAssertionAnalyzerDocs;
8+
9+
/// <summary>
10+
/// Based on https://github.com/nunit/nunit-csharp-samples/blob/master/ExpectedExceptionExample/ExpectedExceptionAttribute.cs
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
13+
public class ExpectedAssertionExceptionAttribute : NUnitAttribute, IWrapTestMethod
14+
{
15+
public TestCommand Wrap(TestCommand command)
16+
{
17+
return new ExpectedExceptionCommand(command, typeof(AssertionException));
18+
}
19+
20+
private class ExpectedExceptionCommand : DelegatingTestCommand
21+
{
22+
private readonly Type _expectedType;
23+
24+
public ExpectedExceptionCommand(TestCommand innerCommand, Type expectedType)
25+
: base(innerCommand)
26+
{
27+
_expectedType = expectedType;
28+
}
29+
30+
public override TestResult Execute(TestExecutionContext context)
31+
{
32+
Type caughtType = null;
33+
34+
try
35+
{
36+
innerCommand.Execute(context);
37+
}
38+
catch (Exception ex)
39+
{
40+
if (ex is NUnitException)
41+
ex = ex.InnerException;
42+
caughtType = ex.GetType();
43+
}
44+
45+
if (caughtType == _expectedType)
46+
context.CurrentResult.SetResult(ResultState.Success);
47+
else if (caughtType != null)
48+
context.CurrentResult.SetResult(ResultState.Failure,
49+
$"Expected {_expectedType.Name} but got {caughtType.Name}");
50+
else
51+
context.CurrentResult.SetResult(ResultState.Failure,
52+
$"Expected {_expectedType.Name} but no exception was thrown");
53+
54+
return context.CurrentResult;
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
<IsTestProject>true</IsTestProject>
8+
<GenerateProgramFile>false</GenerateProgramFile>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
13+
<PackageReference Include="NUnit" Version="3.14.0" />
14+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
15+
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
16+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
17+
<PackageReference Include="coverlet.collector" Version="6.0.0" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator\FluentAssertions.Analyzers.FluentAssertionAnalyzerDocsGenerator.csproj" />
22+
<ProjectReference Include="..\FluentAssertions.Analyzers\FluentAssertions.Analyzers.csproj" />
23+
<Analyzer Include="..\FluentAssertions.Analyzers\bin\Debug\netstandard2.0\FluentAssertions.Analyzers.dll" />
24+
</ItemGroup>
25+
26+
</Project>

0 commit comments

Comments
 (0)