Skip to content

Commit

Permalink
AutoDataWithCustomizationAttribute: Configure composite last (#13)
Browse files Browse the repository at this point in the history
# What
- Configuration of `composite `in `AutoDataWithCustomizationAttribute` must be done last
- Configured `InlineAutoDataWithCustomizationAttribute` as well to also could handle `DateOnly`

# Why
- Otherwise the above configuration isn't applied
- This had the same problem
  • Loading branch information
christian-guldbaek authored Sep 5, 2023
1 parent 5f96150 commit 26ad7ea
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/actions/publish-nuget/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ runs:
using: "composite"
steps:
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ inputs.dotnet-version }}

- name: Download NuGet package artifact
uses: actions/download-artifact@v1.0.0
uses: actions/download-artifact@v3
with:
name: nugetPackage

Expand Down
2 changes: 1 addition & 1 deletion .github/actions/upload-nuget/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ runs:
shell: pwsh

- name: Upload NuGet package back to GitHub Packages
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: nugetPackage
path: ${{ inputs.package-path }}
11 changes: 6 additions & 5 deletions .github/workflows/publish-nuget-packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ env:
jobs:
build:
runs-on: ubuntu-latest
name: Build

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -46,13 +47,13 @@ jobs:
uses: ./.github/actions/upload-nuget
with:
nuget-pack-command: dotnet pack -c Release -o release/beta --no-restore --version-suffix beta-${{github.run_id}}
path: release
package-path: release

- name: Upload prod NuGet to Packages
uses: ./.github/actions/upload-nuget
with:
nuget-pack-command: dotnet pack -c Release -o release --no-restore
path: release
package-path: release

Publish-Beta:
name: Publish beta package
Expand All @@ -67,7 +68,7 @@ jobs:
- name: Publish NuGet package
uses: ./.github/actions/publish-nuget
with:
package-path: nugetPackage\beta\*.nupkg
package-path: beta\*.nupkg
token: ${{ secrets.GITHUB_TOKEN }}
dotnet-version: ${{ env.DOTNET_VERSION }}

Expand All @@ -84,7 +85,7 @@ jobs:
- name: Publish NuGet package
uses: ./.github/actions/publish-nuget
with:
package-path: nugetPackage\*.nupkg
package-path: '*.nupkg'
token: ${{ secrets.GITHUB_TOKEN }}
dotnet-version: ${{ env.DOTNET_VERSION }}

Expand All @@ -102,6 +103,6 @@ jobs:
uses: ./.github/actions/publish-nuget
with:
package-source: https://api.nuget.org/v3/index.json
package-path: nugetPackage\*.nupkg
package-path: '*.nupkg'
token: ${{ secrets.NUGET_ORG_TOKEN }}
dotnet-version: ${{ env.DOTNET_VERSION }}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public AutoDataWithCustomizationAttribute(params Type[] customizations)
customizations.Select(customization =>
CustomizationBuilder.CreateCustomization(customization)));

var fixture = new Fixture().Customize(composite);
var fixture = new Fixture();

fixture.Customize<DateOnly>(c => c.FromFactory<DateTime>(DateOnly.FromDateTime));

fixture.Customize(composite);

return fixture;
})
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public InlineAutoDataWithCustomizationAttribute(
otherCustomizations,
arguments));

return new Fixture().Customize(compositeCustomization);
var fixture = new Fixture();

fixture.Customize<DateOnly>(c => c.FromFactory<DateTime>(DateOnly.FromDateTime));

fixture.Customize(compositeCustomization);

return fixture;
}),
arguments)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>1.3.0</VersionPrefix>
<VersionPrefix>1.3.1</VersionPrefix>
<Authors>Service Delivery</Authors>
<Company>A.P. Møller - Mærsk</Company>
<PackageProjectUrl>https://github.com/MaerskTech/maersk-test-autofixture-extensions</PackageProjectUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Maersk.Test.AutoFixtureExtensions.Tests;
public sealed class AutoDataWithCustomizationAttributeTest
{
public const string ExpectedStringValue = "sample";
private static DateOnly _expectedDateOnly = new DateOnly(2023, 9, 4);

[Trait(nameof(Category), Category.Unit)]
public sealed class Constructor
Expand All @@ -30,6 +31,13 @@ public void Given_a_sample_customization_with_the_AutoDataWithCustomization_attr
{
value.Should().Be(ExpectedStringValue);
}

[Theory]
[AutoDataWithCustomization(typeof(DateOnlyCustomization))]
public void Given_a_DateOnly_value_When_testing_Then_it_can_generate_the_value(ClassWithDateOnly value)
{
value.DateOnly1.Should().Be(_expectedDateOnly);
}
}

private class SampleCustomization : ICustomization
Expand All @@ -39,4 +47,16 @@ public void Customize(IFixture fixture)
fixture.Register(() => ExpectedStringValue);
}
}

private class DateOnlyCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
var classWithDateOnly = fixture.Create<ClassWithDateOnly>();

classWithDateOnly = classWithDateOnly with { DateOnly1 = _expectedDateOnly };

fixture.Register(() => classWithDateOnly);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Maersk. All rights reserved.
// Licensed under the Apache License. See LICENSE in the project root for license information.

namespace Maersk.Test.AutoFixtureExtensions.Tests;

using System;

public record ClassWithDateOnly(
DateOnly DateOnly1);
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Maersk.Test.AutoFixtureExtensions.Tests;

using System;
using System.Drawing;
using AutoFixture;
using AutoFixture.Xunit2;
Expand All @@ -18,6 +19,7 @@ public sealed class Constructor
{
private const string ExpectedArgument1 = "ExpectedArgument1";
private const double ExpectedArgument2 = 123.4;
private const int ExpectedDateOnlyYear = 2023;

[Theory]
[AutoData]
Expand Down Expand Up @@ -144,6 +146,17 @@ public void Given_arguments_with_other_customizations_When_creating_Then_argumen
argument2.Should().Be(ExpectedArgument2);
}

[Theory]
[InlineAutoDataWithCustomization(
typeof(SampleCustomizationWithDateOnlyArguments),
new Type[] { typeof(OtherSampleCustomizationWithDateOnly) },
ExpectedDateOnlyYear)]
public void Given_arguments_with_DateOnly_inline_customizations_When_creating_Then_arguments_are_transferred_to_the_test_method(
int year)
{
year.Should().Be(ExpectedDateOnlyYear);
}

public class SampleCustomizationWithArgumentsAndVerification : ICustomization
{
public SampleCustomizationWithArgumentsAndVerification(string argument1, double argument2)
Expand Down Expand Up @@ -183,5 +196,29 @@ public void Customize(IFixture fixture)
{
}
}

private class OtherSampleCustomizationWithDateOnly : ICustomization
{
public OtherSampleCustomizationWithDateOnly()
{
}

public void Customize(IFixture fixture)
{
_ = fixture.Create<ClassWithDateOnly>();
}
}

private class SampleCustomizationWithDateOnlyArguments : ICustomization
{
public SampleCustomizationWithDateOnlyArguments(int year)
{
_ = year;
}

public void Customize(IFixture fixture)
{
}
}
}
}

0 comments on commit 26ad7ea

Please sign in to comment.