Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emitting readonly modifier for SUT & mock fields #254

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples/AbstractClass.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class TestClassTests
}
}

private TestTestClass _testClass;
private readonly TestTestClass _testClass;

public TestClassTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/AsyncMethod.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private readonly TestClass _testClass;

public TestClassTests()
{
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/AutoFixtureMockGeneration.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public class AutomaticMockGenerationExample
``` csharp
public class AutomaticMockGenerationExampleTests
{
private AutomaticMockGenerationExample _testClass;
private IDummyService _dummyService;
private IDummyService2 _dummyService2;
private readonly AutomaticMockGenerationExample _testClass;
private readonly IDummyService _dummyService;
private readonly IDummyService2 _dummyService2;

public AutomaticMockGenerationExampleTests()
{
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/AutomaticMockGeneration.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public class AutomaticMockGenerationExample
``` csharp
public class AutomaticMockGenerationExampleTests
{
private AutomaticMockGenerationExample _testClass;
private IDummyService _dummyService;
private IDummyService2 _dummyService2;
private readonly AutomaticMockGenerationExample _testClass;
private readonly IDummyService _dummyService;
private readonly IDummyService2 _dummyService2;

public AutomaticMockGenerationExampleTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/ConstrainedGenericType.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class TestClass<T, R>
``` csharp
public class TestClass_2Tests
{
private TestClass<T, R> _testClass;
private readonly TestClass<T, R> _testClass;
private T _insta;
private R _insta2;

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/FrameworksFluentAssertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private IDependency _dependency;
private readonly TestClass _testClass;
private readonly IDependency _dependency;

public TestClassTests()
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/FrameworksXUnitJustMock.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private IDependency _dependency;
private readonly TestClass _testClass;
private readonly IDependency _dependency;

public TestClassTests()
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/FrameworksXUnitNSubstitute.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private IDependency _dependency;
private readonly TestClass _testClass;
private readonly IDependency _dependency;

public TestClassTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/GenericMethod.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class GenericSource
``` csharp
public class GenericSourceTests
{
private GenericSource _testClass;
private readonly GenericSource _testClass;

public GenericSourceTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/IComparableTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TestComparableGeneric : IComparable<TestComparableGeneric>, ICompar
``` csharp
public class TestComparableGenericTests
{
private TestComparableGeneric _testClass;
private readonly TestComparableGeneric _testClass;
private int _value;

public TestComparableGenericTests()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/IndexerTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TypeWithIndexer
``` csharp
public class TypeWithIndexerTests
{
private TypeWithIndexer _testClass;
private readonly TypeWithIndexer _testClass;

public TypeWithIndexerTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/MappingMethod.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class MappingClass
``` csharp
public class MappingClassTests
{
private MappingClass _testClass;
private readonly MappingClass _testClass;

public MappingClassTests()
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/NullableReferenceTypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private readonly TestClass _testClass;
private string _notNullable;
private string _nullable;
private ITest _testITest;
private readonly ITest _testITest;
private string _someOtherThing;

public TestClassTests()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/OperatorOverloading.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Calculator
``` csharp
public class CalculatorTests
{
private Calculator _testClass;
private readonly Calculator _testClass;
private int _n;

public CalculatorTests()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/PocoInitialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ConsumingClass
``` csharp
public class ConsumingClassTests
{
private ConsumingClass _testClass;
private readonly ConsumingClass _testClass;
private SomePoco _poco;

public ConsumingClassTests()
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/PropertyInitializationChecks.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ExampleClass
``` csharp
public class ExampleClassTests
{
private ExampleClass _testClass;
private readonly ExampleClass _testClass;
private int _identity;
private string _description;
private Guid _uniqueCode;
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/RecordTypeInitProperties.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ record Person
``` csharp
public class PersonTests
{
private Person _testClass;
private readonly Person _testClass;
private Guid _id;
private string _firstName;
private string _middleName;
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/RecordTypesPrimaryConstructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record RecordType(string StringProperty, int IntProperty);
``` csharp
public class RecordTypeTests
{
private RecordType _testClass;
private readonly RecordType _testClass;
private string _stringProperty;
private int _intProperty;

Expand Down
2 changes: 1 addition & 1 deletion docs/examples/RefAndOutParameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private readonly TestClass _testClass;

public TestClassTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/SimplePoco.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SomePoco
``` csharp
public class SomePocoTests
{
private SomePoco _testClass;
private readonly SomePoco _testClass;

public SomePocoTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/Singleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class TestClass
``` csharp
public class TestClassTests
{
private TestClass _testClass;
private readonly TestClass _testClass;

public TestClassTests()
{
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/StyleCopCompatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class TestClass
/// </summary>
public class TestClassTests
{
private TestClass _testClass;
private IDependency _dependency;
private readonly TestClass _testClass;
private readonly IDependency _dependency;

/// <summary>
/// Sets up the dependencies required for the tests for <see cref="TestClass"/>.
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/ValueGeneration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Writer
``` csharp
public class WriterTests
{
private Writer _testClass;
private readonly Writer _testClass;

public WriterTests()
{
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/ValueGenerationWithAutoFixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Writer
``` csharp
public class WriterTests
{
private Writer _testClass;
private readonly Writer _testClass;

public WriterTests()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Unitverse.Core/Frameworks/ITestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface ITestFramework : IAssertionFramework
AttributeSyntax? SingleThreadedApartmentAttribute { get; }

string TestClassAttribute { get; }

bool SupportsReadonlyFields { get; }
}
}
2 changes: 2 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/BaseTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ protected BaseTestFramework(IUnitTestGeneratorOptions options)

protected abstract string TestCaseAttributeName { get; }

public abstract bool SupportsReadonlyFields { get; }

protected abstract BaseMethodDeclarationSyntax CreateSetupMethodSyntax(string targetTypeName);

public IUnitTestGeneratorOptions Options { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/MsTestTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public MsTestTestFramework(IUnitTestGeneratorOptions options)

public bool SkipValueTypeNotNull => false;

public override bool SupportsReadonlyFields => false;

public StatementSyntax AssertEqual(ExpressionSyntax actual, ExpressionSyntax expected, bool isReferenceType)
{
if (actual == null)
Expand Down
2 changes: 2 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/NUnitTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected NUnitTestFramework(IUnitTestGeneratorOptions options)

public bool SkipValueTypeNotNull => false;

public override bool SupportsReadonlyFields => false;

private static InvocationExpressionSyntax AssertThat => Generate.MemberInvocation("Assert", "That");

public StatementSyntax AssertEqual(ExpressionSyntax actual, ExpressionSyntax expected, bool isReferenceType)
Expand Down
2 changes: 2 additions & 0 deletions src/Unitverse.Core/Frameworks/Test/XUnitTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public XUnitTestFramework(IUnitTestGeneratorOptions options)

public bool SkipValueTypeNotNull => false;

public override bool SupportsReadonlyFields => true;

public StatementSyntax AssertEqual(ExpressionSyntax actual, ExpressionSyntax expected, bool isReferenceType)
{
if (actual == null)
Expand Down
9 changes: 4 additions & 5 deletions src/Unitverse.Core/Generation/TypeDeclarationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private static TypeDeclarationSyntax EnsureAllConstructorParametersHaveFields(IF
if (!autoFixtureFieldExists)
{
var defaultExpression = AutoFixtureHelper.GetCreationExpression(frameworkSet.Options.GenerationOptions);
updatedMethod = UpdateMethod(updatedMethod, allFields, fields, autoFixtureFieldName, AutoFixtureHelper.TypeSyntax, defaultExpression);
updatedMethod = UpdateMethod(updatedMethod, allFields, fields, autoFixtureFieldName, AutoFixtureHelper.TypeSyntax, defaultExpression, frameworkSet.TestFramework.SupportsReadonlyFields);
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ private static TypeDeclarationSyntax EnsureAllConstructorParametersHaveFields(IF
defaultExpression = AssignmentValueHelper.GetDefaultAssignmentValue(parameterModel.TypeInfo, classModel.SemanticModel, frameworkSet);
}

updatedMethod = UpdateMethod(updatedMethod, allFields, fields, fieldName, fieldTypeSyntax, defaultExpression);
updatedMethod = UpdateMethod(updatedMethod, allFields, fields, fieldName, fieldTypeSyntax, defaultExpression, frameworkSet.TestFramework.SupportsReadonlyFields);
}
}

Expand Down Expand Up @@ -137,12 +137,11 @@ private static TypeDeclarationSyntax EnsureAllConstructorParametersHaveFields(IF
return targetType;
}

private static BaseMethodDeclarationSyntax UpdateMethod(BaseMethodDeclarationSyntax updatedMethod, HashSet<string> allFields, List<FieldDeclarationSyntax> fields, string fieldName, TypeSyntax fieldTypeSyntax, ExpressionSyntax defaultExpression)
private static BaseMethodDeclarationSyntax UpdateMethod(BaseMethodDeclarationSyntax updatedMethod, HashSet<string> allFields, List<FieldDeclarationSyntax> fields, string fieldName, TypeSyntax fieldTypeSyntax, ExpressionSyntax defaultExpression, bool readonlyField)
{
var variable = SyntaxFactory.VariableDeclaration(fieldTypeSyntax)
.AddVariables(SyntaxFactory.VariableDeclarator(fieldName));
var field = SyntaxFactory.FieldDeclaration(variable)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
var field = Generate.Field(variable, readonlyField);

fields.Add(field);

Expand Down
21 changes: 17 additions & 4 deletions src/Unitverse.Core/Helpers/Generate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,24 @@ public static SectionedMethodHandler SetupMethod(ClassModel model, string target
return setupMethod;
}

public static FieldDeclarationSyntax Field(VariableDeclarationSyntax variableDeclaration, bool readonlyField)
{
var field = SyntaxFactory.FieldDeclaration(variableDeclaration);

if (readonlyField)
{
return field.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword), SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword));
}

return field.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
}

private static void AddConstructorField(ClassModel model, IFrameworkSet frameworkSet, ref ClassDeclarationSyntax classDeclaration, SectionedMethodHandler setupMethod, string fieldName, TypeInfo typeInfo)
{
var typeSyntax = typeInfo.ToTypeSyntax(frameworkSet.Context);
ExpressionSyntax defaultExpression;
if (typeInfo.ShouldUseMock())
var isMock = typeInfo.ShouldUseMock();
if (isMock)
{
typeSyntax = frameworkSet.MockingFramework.GetFieldType(typeSyntax);
frameworkSet.Context.InterfacesMocked++;
Expand All @@ -477,13 +490,13 @@ private static void AddConstructorField(ClassModel model, IFrameworkSet framewor
defaultExpression = AssignmentValueHelper.GetDefaultAssignmentValue(typeInfo, model.SemanticModel, frameworkSet);
}

AddConstructorField(frameworkSet, ref classDeclaration, setupMethod, fieldName, typeSyntax, defaultExpression);
AddConstructorField(frameworkSet, ref classDeclaration, setupMethod, fieldName, typeSyntax, defaultExpression, isMock && frameworkSet.TestFramework.SupportsReadonlyFields);
}

private static void AddConstructorField(IFrameworkSet frameworkSet, ref ClassDeclarationSyntax classDeclaration, SectionedMethodHandler setupMethod, string fieldName, TypeSyntax typeSyntax, ExpressionSyntax defaultExpression)
private static void AddConstructorField(IFrameworkSet frameworkSet, ref ClassDeclarationSyntax classDeclaration, SectionedMethodHandler setupMethod, string fieldName, TypeSyntax typeSyntax, ExpressionSyntax defaultExpression, bool readonlyField)
{
var variable = SyntaxFactory.VariableDeclaration(typeSyntax).AddVariables(SyntaxFactory.VariableDeclarator(fieldName));
var field = SyntaxFactory.FieldDeclaration(variable).AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
var field = Field(variable, readonlyField);

classDeclaration = classDeclaration.AddMembers(field);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public ClassDeclarationSyntax Create(ClassModel model)
var variableDeclaration = SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("Test" + model.ClassName))
.AddVariables(SyntaxFactory.VariableDeclarator(model.TargetFieldName));

var fieldDeclaration = SyntaxFactory.FieldDeclaration(variableDeclaration)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
var fieldDeclaration = Generate.Field(variableDeclaration, _frameworkSet.TestFramework.SupportsReadonlyFields && model.TypeSymbol.IsReferenceType);
classDeclaration = classDeclaration.AddMembers(fieldDeclaration);

if (_frameworkSet.Options.GenerationOptions.UseFieldForAutoFixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public ClassDeclarationSyntax Create(ClassModel model)
var variableDeclaration = SyntaxFactory.VariableDeclaration(model.TypeSyntax)
.AddVariables(SyntaxFactory.VariableDeclarator(model.TargetFieldName));

var fieldDeclaration = SyntaxFactory.FieldDeclaration(variableDeclaration)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PrivateKeyword));
var fieldDeclaration = Generate.Field(variableDeclaration, _frameworkSet.TestFramework.SupportsReadonlyFields && model.TypeSymbol.IsReferenceType);
classDeclaration = classDeclaration.AddMembers(fieldDeclaration);

if (_frameworkSet.Options.GenerationOptions.UseFieldForAutoFixture)
Expand Down
Loading
Loading