Skip to content
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
6 changes: 5 additions & 1 deletion .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Publish NuGet Package

on:
workflow_dispatch:
release:
types:
- published

permissions:
contents: read
Expand All @@ -12,12 +14,14 @@ concurrency:

jobs:
publish:
if: ${{ github.event.release.target_commitish == 'main' && startsWith(github.event.release.tag_name, 'v') }}
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v6
with:
ref: ${{ github.event.release.tag_name }}
fetch-depth: 0

- name: Setup .NET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
<PackageId>ConstructorCustomization.AutoFixture.Tests</PackageId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net10.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" />
</ItemGroup>
Expand Down
38 changes: 19 additions & 19 deletions ConstructorCustomization.AutoFixture/ConstructorCustomization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq.Expressions;
using System.Linq.Expressions;
using System.Reflection;

using AutoFixture;
Expand Down Expand Up @@ -167,8 +167,8 @@ protected virtual void Configure() { }
/// </summary>
protected void UsePlugin(Func<Type, bool> predicate, Func<Type, IFixture, IValueCreationService, object?> factory)
{
ArgumentNullException.ThrowIfNull(predicate);
ArgumentNullException.ThrowIfNull(factory);
ThrowIfNull(predicate);
ThrowIfNull(factory);
Plugins.Add(new DelegateValueCreationPlugin(predicate, factory));
}

Expand All @@ -178,7 +178,7 @@ protected void UsePlugin(Func<Type, bool> predicate, Func<Type, IFixture, IValue
/// </summary>
protected void UsePlugin(IValueCreationPlugin plugin)
{
ArgumentNullException.ThrowIfNull(plugin);
ThrowIfNull(plugin);
Plugins.Add(plugin);
}

Expand All @@ -189,7 +189,7 @@ protected void UsePlugin(IValueCreationPlugin plugin)
/// <typeparam name="TType">The exact type to handle.</typeparam>
protected void UseValueFor<TType>(Func<IFixture, TType?> factory)
{
ArgumentNullException.ThrowIfNull(factory);
ThrowIfNull(factory);
Plugins.Add(new DelegateValueCreationPlugin(
t => t == typeof(TType),
(type, fixture, svc) => factory(fixture)));
Expand All @@ -203,7 +203,7 @@ protected void UseValueFor<TType>(Func<IFixture, TType?> factory)
/// <typeparam name="TType">The exact type to handle.</typeparam>
protected void UseValueFor<TType>(Func<IFixture, IValueCreationService, TType?> factory)
{
ArgumentNullException.ThrowIfNull(factory);
ThrowIfNull(factory);
Plugins.Add(new DelegateValueCreationPlugin(
t => t == typeof(TType),
(type, fixture, svc) => factory(fixture, svc)));
Expand All @@ -215,7 +215,7 @@ protected void UseValueFor<TType>(Func<IFixture, IValueCreationService, TType?>
/// </summary>
protected void UseStrategy(ISpecimenBuilderStrategy strategy)
{
ArgumentNullException.ThrowIfNull(strategy);
ThrowIfNull(strategy);
UserStrategies.Add(strategy);
}

Expand All @@ -226,7 +226,7 @@ protected void UseStrategy(ISpecimenBuilderStrategy strategy)
/// </summary>
protected void UseConstructorSelector(IConstructorSelector selector)
{
ArgumentNullException.ThrowIfNull(selector);
ThrowIfNull(selector);
RegisteredConstructorSelector = selector;
}

Expand All @@ -237,7 +237,7 @@ protected void UseConstructorSelector(IConstructorSelector selector)
/// </summary>
protected void UseParameterPropertyMatcher(IParameterPropertyMatcher matcher)
{
ArgumentNullException.ThrowIfNull(matcher);
ThrowIfNull(matcher);
RegisteredParameterPropertyMatcher = matcher;
}

Expand All @@ -251,8 +251,8 @@ protected void UseParameterPropertyMatcher(IParameterPropertyMatcher matcher)
/// <param name="propertyExpression">The target property expression.</param>
protected void MatchParameterToProperty<TProperty>(string parameterName, Expression<Func<T, TProperty>> propertyExpression)
{
ArgumentException.ThrowIfNullOrWhiteSpace(parameterName);
ArgumentNullException.ThrowIfNull(propertyExpression);
ThrowIfNullOrWhiteSpace(parameterName);
ThrowIfNull(propertyExpression);

var propertyName = PropertyExpressionParser.GetPropertyName(propertyExpression);
ExplicitParameterPropertyMappings[parameterName.Trim()] = propertyName;
Expand All @@ -266,7 +266,7 @@ protected void MatchParameterToProperty<TProperty>(string parameterName, Express
/// </summary>
protected void UsePropertyExpressionParser(IPropertyExpressionParser parser)
{
ArgumentNullException.ThrowIfNull(parser);
ThrowIfNull(parser);
RegisteredPropertyExpressionParser = parser;
}

Expand All @@ -279,7 +279,7 @@ protected void UsePropertyExpressionParser(IPropertyExpressionParser parser)
/// </summary>
protected void UsePropertyValueStore(Func<IPropertyValueStore> factory)
{
ArgumentNullException.ThrowIfNull(factory);
ThrowIfNull(factory);
RegisteredValueStoreFactory = factory;
}

Expand All @@ -292,7 +292,7 @@ protected void UsePropertyValueStore(Func<IPropertyValueStore> factory)
/// </summary>
protected void UseValueCreationService(IValueCreationService service)
{
ArgumentNullException.ThrowIfNull(service);
ThrowIfNull(service);
RegisteredValueCreationService = service;
}

Expand All @@ -318,7 +318,7 @@ protected void SetDefault<TProperty>(Expression<Func<T, TProperty>> propertyExpr
/// </summary>
protected void SetDefault<TProperty>(Expression<Func<T, TProperty>> propertyExpression, Func<TProperty> valueFactory)
{
ArgumentNullException.ThrowIfNull(valueFactory);
ThrowIfNull(valueFactory);
var propertyName = PropertyExpressionParser.GetPropertyName(propertyExpression);
DefaultPropertyValueStore.SetValue(propertyName, new ConfiguredValueFactory(_ => valueFactory()));
}
Expand All @@ -329,7 +329,7 @@ protected void SetDefault<TProperty>(Expression<Func<T, TProperty>> propertyExpr
/// </summary>
protected void SetDefault<TProperty>(Expression<Func<T, TProperty>> propertyExpression, Func<IFixture, TProperty> valueFactory)
{
ArgumentNullException.ThrowIfNull(valueFactory);
ThrowIfNull(valueFactory);
var propertyName = PropertyExpressionParser.GetPropertyName(propertyExpression);
DefaultPropertyValueStore.SetValue(propertyName, new ConfiguredValueFactory(fixture => valueFactory(fixture)));
}
Expand All @@ -356,7 +356,7 @@ public TSelf With<TProperty>(Expression<Func<T, TProperty>> propertyExpression,
/// </summary>
public TSelf With<TProperty>(Expression<Func<T, TProperty>> propertyExpression, Func<TProperty> valueFactory)
{
ArgumentNullException.ThrowIfNull(valueFactory);
ThrowIfNull(valueFactory);
var propertyName = PropertyExpressionParser.GetPropertyName(propertyExpression);
OverridePropertyValueStore.SetValue(propertyName, new ConfiguredValueFactory(_ => valueFactory()));
return (TSelf)this;
Expand All @@ -368,7 +368,7 @@ public TSelf With<TProperty>(Expression<Func<T, TProperty>> propertyExpression,
/// </summary>
public TSelf With<TProperty>(Expression<Func<T, TProperty>> propertyExpression, Func<IFixture, TProperty> valueFactory)
{
ArgumentNullException.ThrowIfNull(valueFactory);
ThrowIfNull(valueFactory);
var propertyName = PropertyExpressionParser.GetPropertyName(propertyExpression);
OverridePropertyValueStore.SetValue(propertyName, new ConfiguredValueFactory(fixture => valueFactory(fixture)));
return (TSelf)this;
Expand Down Expand Up @@ -521,7 +521,7 @@ protected virtual T CreateInstance(IFixture fixture)

private bool TryGetMappedPropertyName(ParameterInfo parameter, out string propertyName)
{
ArgumentNullException.ThrowIfNull(parameter);
ThrowIfNull(parameter);

var parameterName = parameter.Name;
if (string.IsNullOrWhiteSpace(parameterName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;

using ConstructorCustomization.AutoFixture.Customization.Application.Ports;

Expand All @@ -17,15 +17,15 @@ internal sealed class CaseInsensitiveParameterPropertyMatcher : IParameterProper
/// <param name="comparer">The comparer used when evaluating parameter and property names.</param>
public CaseInsensitiveParameterPropertyMatcher(StringComparer comparer)
{
ArgumentNullException.ThrowIfNull(comparer);
ThrowIfNull(comparer);
this.comparer = comparer;
}

/// <inheritdoc />
public bool TryGetPropertyName(ParameterInfo parameter, IEnumerable<string> configuredPropertyNames, out string propertyName)
{
ArgumentNullException.ThrowIfNull(parameter);
ArgumentNullException.ThrowIfNull(configuredPropertyNames);
ThrowIfNull(parameter);
ThrowIfNull(configuredPropertyNames);

var parameterName = parameter.Name;
if (string.IsNullOrWhiteSpace(parameterName))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;

using ConstructorCustomization.AutoFixture.Customization.Application.Ports;

Expand All @@ -12,8 +12,8 @@ internal sealed class LargestConstructorSelector : IConstructorSelector
/// <inheritdoc />
public ConstructorInfo SelectConstructor(Type targetType, ConstructorInfo[] constructors)
{
ArgumentNullException.ThrowIfNull(targetType);
ArgumentNullException.ThrowIfNull(constructors);
ThrowIfNull(targetType);
ThrowIfNull(constructors);

if (constructors.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq.Expressions;
using System.Linq.Expressions;

using ConstructorCustomization.AutoFixture.Customization.Application.Ports;

Expand All @@ -12,7 +12,7 @@ internal sealed class PropertyExpressionParser : IPropertyExpressionParser
/// <inheritdoc />
public string GetPropertyName(LambdaExpression propertyExpression)
{
ArgumentNullException.ThrowIfNull(propertyExpression);
ThrowIfNull(propertyExpression);

var body = propertyExpression.Body;
if (body is UnaryExpression unaryExpression && unaryExpression.NodeType == ExpressionType.Convert)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ConstructorCustomization.AutoFixture.Customization.Application.Ports;
using ConstructorCustomization.AutoFixture.Customization.Application.Ports;

namespace ConstructorCustomization.AutoFixture.Customization.Adapters;

Expand All @@ -15,7 +15,7 @@ internal sealed class PropertyValueStore : IPropertyValueStore
/// <param name="comparer">The comparer used to match property names.</param>
public PropertyValueStore(StringComparer comparer)
{
ArgumentNullException.ThrowIfNull(comparer);
ThrowIfNull(comparer);
values = new Dictionary<string, object?>(comparer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ConstructorCustomization.AutoFixture.Customization.Domain;
namespace ConstructorCustomization.AutoFixture.Customization.Domain;

internal sealed class CustomizationDomainOptions
{
Expand All @@ -14,7 +14,7 @@ private CustomizationDomainOptions(int collectionItemCount, StringComparer prope

public static CustomizationDomainOptions From(ConstructorCustomizationOptions options)
{
ArgumentNullException.ThrowIfNull(options);
ThrowIfNull(options);

if (options.CollectionItemCount < 0)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoFixture;
using AutoFixture;

using ConstructorCustomization.AutoFixture.Customization.Application.Ports;

Expand All @@ -15,11 +15,11 @@ public static bool TryResolveConfiguredValue(
out object? resolvedValue,
out PropertyValueSource valueSource)
{
ArgumentException.ThrowIfNullOrWhiteSpace(propertyName);
ArgumentNullException.ThrowIfNull(overrideStore);
ArgumentNullException.ThrowIfNull(defaultStore);
ArgumentNullException.ThrowIfNull(fixture);
ArgumentNullException.ThrowIfNull(configuredValueResolver);
ThrowIfNullOrWhiteSpace(propertyName);
ThrowIfNull(overrideStore);
ThrowIfNull(defaultStore);
ThrowIfNull(fixture);
ThrowIfNull(configuredValueResolver);

if (overrideStore.TryGetValue(propertyName, out var overrideValue))
{
Expand Down
1 change: 1 addition & 0 deletions ConstructorCustomization.AutoFixture/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using static ConstructorCustomization.AutoFixture.Internal.ThrowHelper;
31 changes: 31 additions & 0 deletions ConstructorCustomization.AutoFixture/Internal/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace ConstructorCustomization.AutoFixture.Internal;

internal static class ThrowHelper
{
public static void ThrowIfNull(
[NotNull] object? argument,
[CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET6_0_OR_GREATER
global::System.ArgumentNullException.ThrowIfNull(argument, paramName);
#else
if (argument is null)
throw new global::System.ArgumentNullException(paramName);
#endif
}

public static void ThrowIfNullOrWhiteSpace(
[NotNull] string? argument,
[CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET7_0_OR_GREATER
global::System.ArgumentException.ThrowIfNullOrWhiteSpace(argument, paramName);
#else
if (string.IsNullOrWhiteSpace(argument))
throw new global::System.ArgumentException("The value cannot be null or whitespace.", paramName);
#endif
}
}
7 changes: 7 additions & 0 deletions ConstructorCustomization.AutoFixture/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Required for 'init' property accessors on netstandard2.1 (.NET 5+ already includes this type).
#if !NET5_0_OR_GREATER
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Polyfill: CallerArgumentExpressionAttribute was added to the BCL in .NET 6.
// The C# compiler uses it at compile time; defining it here enables the feature on older targets.
#if !NET6_0_OR_GREATER
namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
{
public CallerArgumentExpressionAttribute(string parameterName)
{
ParameterName = parameterName;
}

public string ParameterName { get; }
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoFixture;
using AutoFixture;

using ConstructorCustomization.AutoFixture.SpecimenGeneration.Ports;
using ConstructorCustomization.AutoFixture.ValueGeneration.Ports;
Expand All @@ -16,9 +16,9 @@ internal sealed class ArraySpecimenBuilderStrategy : ISpecimenBuilderStrategy
/// <inheritdoc />
public object? Build(Type type, IFixture fixture, IValueCreationService valueCreationService, ConstructorCustomizationOptions options)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(valueCreationService);
ArgumentNullException.ThrowIfNull(options);
ThrowIfNull(type);
ThrowIfNull(valueCreationService);
ThrowIfNull(options);

var elementType = type.GetElementType() ?? throw new InvalidOperationException($"Unable to resolve array element type for {type.FullName}.");
var array = Array.CreateInstance(elementType, options.CollectionItemCount);
Expand Down
Loading
Loading