Skip to content

Commit

Permalink
## 8.2.1.0 - 2024-10-30
Browse files Browse the repository at this point in the history
### Changed
- Fixed the default nullable strategy - it should inherit the option from the project the builder is in.
   - *Warning* It may be a breaking change if someone is accustomed to previous behavior, but it was a bug.
- Changed the format of versioning: The biggest number is only to reflect minimum version of .net. The rest is like in the Semantic Versioning
  • Loading branch information
pmrogala committed Oct 30, 2024
1 parent bba5a3e commit 0b7ee45
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Buildenator/Buildenator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<!-- Do not include the generator as a lib dependency -->
<Version>8.2.0</Version>
<Version>8.2.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 11 additions & 5 deletions Buildenator/BuildersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
#if DEBUG
// Debugger.Launch();
#endif

var nullableOptions = context.CompilationProvider
.Select(static (compilation, _) => compilation.Options.NullableContextOptions);
var syntaxTrees = context.CompilationProvider
.SelectMany(static (compilation, _) => compilation.SyntaxTrees);

Expand Down Expand Up @@ -82,13 +85,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return (globalFixtureProperties, mockingConfigurationBuilder, globalBuilderProperties);
});

var generators = classSymbols.Combine(configurationBuilders)
.Select(static (leftRight, _) =>
var generators = classSymbols
.Combine(configurationBuilders)
.Combine(nullableOptions)
.Select(static (leftRightAndNullable, _) =>
{
var (builderNamedTypeSymbol, attribute) = leftRight.Left;
var (builderNamedTypeSymbol, attribute) = leftRightAndNullable.Left.Left;
var (globalFixtureProperties,
globalMockingConfiguration,
globalBuilderProperties) = leftRight.Right;
globalBuilderProperties) = leftRightAndNullable.Left.Right;
var nullableOptions = leftRightAndNullable.Right;
var builderAttributes = builderNamedTypeSymbol.GetAttributes();
var mockingProperties = MockingProperties.CreateOrDefault(
Expand All @@ -101,7 +107,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
GetLocalFixturePropertiesOrDefault(builderAttributes));
var builderProperties =
BuilderProperties.Create(builderNamedTypeSymbol, attribute, globalBuilderProperties);
BuilderProperties.Create(builderNamedTypeSymbol, attribute, globalBuilderProperties, nullableOptions.AnnotationsEnabled());
return (fixtureProperties, mockingConfiguration: mockingProperties, builderProperties,
attribute.TypeForBuilder);
Expand Down
17 changes: 14 additions & 3 deletions Buildenator/Configuration/BuilderProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ namespace Buildenator.Configuration;
private readonly Dictionary<string, IFieldSymbol> _fields;
private readonly List<BuildenatorDiagnostic> _diagnostics = [];

public static BuilderProperties Create(INamespaceOrTypeSymbol builderSymbol,
MakeBuilderAttributeInternal builderAttribute, ImmutableArray<TypedConstant>? globalAttributes)
public static BuilderProperties Create(
INamespaceOrTypeSymbol builderSymbol,
MakeBuilderAttributeInternal builderAttribute,
ImmutableArray<TypedConstant>? globalAttributes,
bool nullableAnnotaionEnabled)
{
string? defaultNameWith = null;
bool? defaultStaticBuilder = null;
Expand All @@ -34,12 +37,20 @@ public static BuilderProperties Create(INamespaceOrTypeSymbol builderSymbol,
implicitCast = globalAttributes.Value.GetOrThrow<bool>(4, nameof(MakeBuilderAttributeInternal.ImplicitCast));
}

nullableStrategy = builderAttribute.NullableStrategy is null ? nullableStrategy: builderAttribute.NullableStrategy;

if ((nullableStrategy is null || nullableStrategy == NullableStrategy.Default) && nullableAnnotaionEnabled)
{
nullableStrategy = NullableStrategy.Enabled;
}


return new BuilderProperties(builderSymbol,
new MakeBuilderAttributeInternal(
builderAttribute.TypeForBuilder,
builderAttribute.BuildingMethodsPrefix ?? defaultNameWith,
builderAttribute.DefaultStaticCreator ?? defaultStaticBuilder,
builderAttribute.NullableStrategy ?? nullableStrategy,
nullableStrategy,
builderAttribute.GenerateMethodsForUnreachableProperties ??
generateMethodsForUnreachableProperties,
builderAttribute.ImplicitCast ?? implicitCast));
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [x.x.x]
## [MinDotNetVersion.x.x.x]

### Added
### Changed
### Removed

## 8.2.1.0 - 2024-10-30

### Changed
- Fixed the default nullable strategy - it should inherit the option from the project the builder is in.
- *Warning* It may be a breaking change if someone is accustomed to previous behavior, but it was a bug.
- Changed the format of versioning: The biggest number is only to reflect minimum version of .net. The rest is like in the Semantic Versioning

## 8.2.0 - 2024-10-23

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Buildenator.IntegrationTests.SourceNullable.Builders
{
[MakeBuilder(typeof(ChildEntity))]
[MakeBuilder(typeof(ChildEntity), nullableStrategy: NullableStrategy.Default)]
[MoqConfiguration(MockingInterfacesStrategy.All)]
public partial class ChildEntityBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using Xunit;
using PostBuildEntityBuilder = Buildenator.IntegrationTests.Source.Builders.PostBuildEntityBuilder;
using Newtonsoft.Json.Linq;

namespace Buildenator.IntegrationTests;

Expand Down
52 changes: 42 additions & 10 deletions Tests/Buildenator.UnitTests/Configuration/BuilderPropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,7 @@ public void Constructor_ShouldThrowArgumentNullException_WhenBuildingMethodsPref
public void Constructor_ShouldInitializePropertiesAndCollections_WhenValidParametersAreProvided()
{
// Arrange
var typeSymbolMock = new Mock<INamedTypeSymbol>();
var attributeDataMock = new MakeBuilderAttributeInternal(typeSymbolMock.Object, "Build", false,
NullableStrategy.Enabled, false, true);
var methodSymbolMock = new Mock<IMethodSymbol>();
_ = methodSymbolMock.SetupGet(x => x.Name).Returns("BuildMethod");
_ = methodSymbolMock.SetupGet(x => x.MethodKind).Returns(MethodKind.Ordinary);
_ = _builderSymbolMock.Setup(x => x.GetMembers()).Returns([methodSymbolMock.Object]);
var (attributeDataMock, methodSymbolMock) = ArrangeBasicAttirbutesAndMethods(NullableStrategy.Enabled);

// Act
var properties = Create(attributeDataMock);
Expand All @@ -72,9 +66,47 @@ public void Constructor_ShouldInitializePropertiesAndCollections_WhenValidParame
_ = properties.Fields.Should().BeEmpty();
}

private BuilderProperties Create(MakeBuilderAttributeInternal attributeDataMock)
[Theory]
[InlineData(NullableStrategy.Default, true, NullableStrategy.Enabled)]
[InlineData(null, true, NullableStrategy.Enabled)]
[InlineData(NullableStrategy.Disabled, true, NullableStrategy.Disabled)]
[InlineData(NullableStrategy.Enabled, true, NullableStrategy.Enabled)]
[InlineData(NullableStrategy.Default, false, NullableStrategy.Default)]
[InlineData(null, false, NullableStrategy.Default)]
[InlineData(NullableStrategy.Disabled, false, NullableStrategy.Disabled)]
[InlineData(NullableStrategy.Enabled, false, NullableStrategy.Enabled)]
public void Constructor_ShouldSetProperNullableStragy_WhenDifferentSetupOfAttributesAndProject(
NullableStrategy? builderStategy,
bool annonationContextEnabled,
NullableStrategy? result)
{
// Arrange
var (attributeDataMock, _) = ArrangeBasicAttirbutesAndMethods(builderStategy);

// Act
var properties = Create(attributeDataMock, nullableAnnotationEnabled: annonationContextEnabled);

// Assert
_ = properties.NullableStrategy.Should().Be(result);
}

private (MakeBuilderAttributeInternal attributeDataMock, Mock<IMethodSymbol> methodSymbolMock) ArrangeBasicAttirbutesAndMethods(
NullableStrategy? nullableStrategy = NullableStrategy.Default)
{
var typeSymbolMock = new Mock<INamedTypeSymbol>();
var attributeDataMock = new MakeBuilderAttributeInternal(typeSymbolMock.Object, "Build", false,
nullableStrategy, false, true);
var methodSymbolMock = new Mock<IMethodSymbol>();
_ = methodSymbolMock.SetupGet(x => x.Name).Returns("BuildMethod");
_ = methodSymbolMock.SetupGet(x => x.MethodKind).Returns(MethodKind.Ordinary);
_ = _builderSymbolMock.Setup(x => x.GetMembers()).Returns([methodSymbolMock.Object]);

return (attributeDataMock, methodSymbolMock);
}

private BuilderProperties Create(MakeBuilderAttributeInternal attributeDataMock, TypedConstant[]? typedConstants = null, bool nullableAnnotationEnabled = false)
{
return BuilderProperties.Create(_builderSymbolMock.Object, attributeDataMock, null);
return BuilderProperties.Create(_builderSymbolMock.Object, attributeDataMock, typedConstants?.ToImmutableArray(), nullableAnnotationEnabled);
}

[Theory]
Expand Down Expand Up @@ -111,7 +143,7 @@ public void Constructor_ShouldOnlyPopulateBuildingMethodsDictionary_WhenOrdinary
_ = _builderSymbolMock.Setup(x => x.GetMembers()).Returns([methodSymbolMock.Object]);

// Act
var properties = BuilderProperties.Create(_builderSymbolMock.Object, attributeDataMock, null);
var properties = BuilderProperties.Create(_builderSymbolMock.Object, attributeDataMock, null, false);

// Assert
_ = properties.BuildingMethods.Should().ContainKey(methodSymbolMock.Object.Name).And.ContainValue(methodSymbolMock.Object);
Expand Down

0 comments on commit 0b7ee45

Please sign in to comment.