Skip to content

Commit

Permalink
Fixture life cycle and ability to omit test class attributes (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwhitfield authored Dec 13, 2023
1 parent 59729bb commit 377bdef
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/Unitverse.Core.Tests/Resources/OmitTestClassAttribute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// # OmitTestClassAttribute=true
namespace TestNamespace
{
public class TestClass
{
public static string ParseTheThing(string input)
{
return string.Empty;
}
}
}
11 changes: 11 additions & 0 deletions src/Unitverse.Core.Tests/Resources/UseConstructorForClassSetup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// # UseConstructorForTestClassSetUp=true
namespace TestNamespace
{
public class TestClass
{
public static string ParseTheThing(string input)
{
return string.Empty;
}
}
}
41 changes: 40 additions & 1 deletion src/Unitverse.Core.Tests/TestClasses.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Unitverse.Core.Tests/TestClasses.resx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@
<data name="OldStyleConstructorFieldTesting" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\OldStyleConstructorFieldTesting.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="OmitTestClassAttribute" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\OmitTestClassAttribute.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="OperatorOverloading" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\OperatorOverloading.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
Expand Down Expand Up @@ -343,6 +346,9 @@
<data name="TypeGenericDisambiguation" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\TypeGenericDisambiguation.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="UseConstructorForClassSetup" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\UseConstructorForClassSetup.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="UsingSort-Natural" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>Resources\UsingSort-Natural.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/MsTestTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public StatementSyntax AssertThrowsAsync(TypeSyntax exceptionType, ExpressionSyn

protected override BaseMethodDeclarationSyntax CreateSetupMethodSyntax(string targetTypeName)
{
if (Options.GenerationOptions.UseConstructorForTestClassSetUp)
{
return SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(targetTypeName)).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
}

return Generate.Method("SetUp", false, false).AddAttributeLists(Generate.Attribute("TestInitialize").AsList());
}

Expand Down
5 changes: 5 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/NUnitTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public StatementSyntax AssertThrowsAsync(TypeSyntax exceptionType, ExpressionSyn

protected override BaseMethodDeclarationSyntax CreateSetupMethodSyntax(string targetTypeName)
{
if (Options.GenerationOptions.UseConstructorForTestClassSetUp)
{
return SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(targetTypeName)).AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
}

return Generate.Method("SetUp", false, false).AddAttributeLists(Generate.Attribute("SetUp").AsList());
}

Expand Down
4 changes: 4 additions & 0 deletions src/Unitverse.Core/Helpers/DetectedGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,9 @@ public DetectedGenerationOptions(IGenerationOptions baseOptions, bool? usefluent
public bool EmitMultilinePocoInitializers => _baseOptions.EmitMultilinePocoInitializers;

public bool UseFieldsForConstructorParameterTests => _baseOptions.UseFieldsForConstructorParameterTests;

public bool UseConstructorForTestClassSetUp => _baseOptions.UseConstructorForTestClassSetUp;

public bool OmitTestClassAttribute => _baseOptions.OmitTestClassAttribute;
}
}
12 changes: 12 additions & 0 deletions src/Unitverse.Core/Helpers/FrameworkSetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,17 @@ public static ExpressionSyntax QualifyFieldReference(this IFrameworkSet framewor

return nameSyntax;
}

public static ClassDeclarationSyntax ApplyTestClassAttribute(this IFrameworkSet frameworkSet, ClassDeclarationSyntax classDeclarationSyntax)
{
if (!frameworkSet.Options.GenerationOptions.OmitTestClassAttribute &&
!string.IsNullOrWhiteSpace(frameworkSet.TestFramework.TestClassAttribute))
{
var testFixtureAtt = Generate.Attribute(frameworkSet.TestFramework.TestClassAttribute);
return classDeclarationSyntax.AddAttributeLists(testFixtureAtt.AsList());
}

return classDeclarationSyntax;
}
}
}
4 changes: 4 additions & 0 deletions src/Unitverse.Core/Options/IGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@ public interface IGenerationOptions
bool EmitMultilinePocoInitializers { get; }

bool UseFieldsForConstructorParameterTests { get; }

bool UseConstructorForTestClassSetUp { get; }

bool OmitTestClassAttribute { get; }
}
}
4 changes: 4 additions & 0 deletions src/Unitverse.Core/Options/MutableGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,9 @@ public MutableGenerationOptions(IGenerationOptions options)
public bool EmitMultilinePocoInitializers { get; set; }

public bool UseFieldsForConstructorParameterTests { get; set; }

public bool UseConstructorForTestClassSetUp { get; set; }

public bool OmitTestClassAttribute { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ public ClassDeclarationSyntax Create(ClassModel model)
var classDeclaration = SyntaxFactory.ClassDeclaration(targetTypeName);

classDeclaration = classDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
if (!string.IsNullOrWhiteSpace(_frameworkSet.TestFramework.TestClassAttribute))
{
var testFixtureAtt = Generate.Attribute(_frameworkSet.TestFramework.TestClassAttribute);
classDeclaration = classDeclaration.AddAttributeLists(testFixtureAtt.AsList());
}
classDeclaration = _frameworkSet.ApplyTestClassAttribute(classDeclaration);

classDeclaration = classDeclaration.AddMembers(GenerateConcreteInheritor(model));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ public ClassDeclarationSyntax Create(ClassModel model)
var classDeclaration = SyntaxFactory.ClassDeclaration(targetTypeName);

classDeclaration = classDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
if (!string.IsNullOrWhiteSpace(_frameworkSet.TestFramework.TestClassAttribute))
{
var testFixtureAtt = Generate.Attribute(_frameworkSet.TestFramework.TestClassAttribute);
classDeclaration = classDeclaration.AddAttributeLists(testFixtureAtt.AsList());
}
classDeclaration = _frameworkSet.ApplyTestClassAttribute(classDeclaration);

var variableDeclaration = SyntaxFactory.VariableDeclaration(model.TypeSyntax)
.AddVariables(SyntaxFactory.VariableDeclarator(model.TargetFieldName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ public ClassDeclarationSyntax Create(ClassModel model)
classDeclaration = classDeclaration.AddMembers(field);
}

if (!string.IsNullOrWhiteSpace(_frameworkSet.TestFramework.TestClassAttribute))
{
var testFixtureAtt = Generate.Attribute(_frameworkSet.TestFramework.TestClassAttribute);
classDeclaration = classDeclaration.AddAttributeLists(testFixtureAtt.AsList());
}
classDeclaration = _frameworkSet.ApplyTestClassAttribute(classDeclaration);

return classDeclaration;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Unitverse.Tests.Common/DefaultGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@ public class DefaultGenerationOptions : IGenerationOptions
public bool EmitMultilinePocoInitializers { get; set; } = true;

public bool UseFieldsForConstructorParameterTests { get; set; } = true;

public bool UseConstructorForTestClassSetUp { get; set; } = false;

public bool OmitTestClassAttribute { get; set; } = false;
}
}
10 changes: 10 additions & 0 deletions src/Unitverse/Options/GenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,15 @@ public class GenerationOptions : DialogPage, IGenerationOptions
[DisplayName("Use fields for constructor parameter tests")]
[Description("Whether to use fields for constructor parameter tests, or to create a new value for every test")]
public bool UseFieldsForConstructorParameterTests { get; set; } = true;

[Category("Generation")]
[DisplayName("Use constructor for test class set up")]
[Description("Whether to use constructor for test class set up instead of the framework set up method")]
public bool UseConstructorForTestClassSetUp { get; set; } = false;

[Category("Generation")]
[DisplayName("Omit test class attribute")]
[Description("Whether to omit the attribute used to decorate test classes")]
public bool OmitTestClassAttribute { get; set; } = false;
}
}

0 comments on commit 377bdef

Please sign in to comment.