Skip to content

Commit

Permalink
Reimplemented XUnit 2 AutoData attributes (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
aivascu committed Mar 25, 2024
1 parent 6e3bf9b commit 2fbe62c
Show file tree
Hide file tree
Showing 88 changed files with 3,034 additions and 1,136 deletions.
6 changes: 6 additions & 0 deletions Src/All.sln
Expand Up @@ -58,12 +58,18 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F0A01F00-72C7-4CE4-8BA4-9EB178B72329}"
ProjectSection(SolutionItems) = preProject
..\.editorconfig = ..\.editorconfig
..\.gitattributes = ..\.gitattributes
..\.gitignore = ..\.gitignore
CodeAnalysis.AutoFixture.ruleset = CodeAnalysis.AutoFixture.ruleset
CodeAnalysis.Empty.ruleset = CodeAnalysis.Empty.ruleset
CodeAnalysis.Test.ruleset = CodeAnalysis.Test.ruleset
..\CODE_OF_CONDUCT.md = ..\CODE_OF_CONDUCT.md
Common.props = Common.props
Common.Test.props = Common.Test.props
Common.Test.xUnit.props = Common.Test.xUnit.props
..\CONTRIBUTING.md = ..\CONTRIBUTING.md
..\LICENCE.txt = ..\LICENCE.txt
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutoFixture.SeedExtensions", "AutoFixture.SeedExtensions\AutoFixture.SeedExtensions.csproj", "{F1FE2173-62BD-4D3A-92C1-A3ECB2117252}"
Expand Down
200 changes: 47 additions & 153 deletions Src/AutoFixture.xUnit2.UnitTest/AutoDataAttributeTest.cs
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AutoFixture.Kernel;
using AutoFixture.Xunit2.UnitTest.TestTypes;
using TestTypeFoundation;
using Xunit;
using Xunit.Sdk;
Expand All @@ -13,9 +15,9 @@ public class AutoDataAttributeTest
[Fact]
public void SutIsDataAttribute()
{
// Arrange
// Act
// Arrange & Act
var sut = new AutoDataAttribute();

// Assert
Assert.IsAssignableFrom<DataAttribute>(sut);
}
Expand All @@ -25,16 +27,18 @@ public void InitializedWithDefaultConstructorHasCorrectFixture()
{
// Arrange
var sut = new AutoDataAttribute();

// Act
#pragma warning disable 618
IFixture result = sut.Fixture;
var result = sut.FixtureFactory();
#pragma warning restore 618

// Assert
Assert.IsAssignableFrom<Fixture>(result);
}

[Fact]
public void InitializedWithFixtureFactoryConstrucorHasCorrectFixture()
public void InitializedWithFixtureFactoryConstructorHasCorrectFixture()
{
// Arrange
var fixture = new Fixture();
Expand All @@ -44,157 +48,78 @@ public void InitializedWithFixtureFactoryConstrucorHasCorrectFixture()

// Assert
#pragma warning disable 618
Assert.Same(fixture, sut.Fixture);
Assert.Same(fixture, sut.FixtureFactory());
#pragma warning restore 618
}

[Fact]
public void InitializeWithNullFixtureThrows()
{
// Arrange
// Act & assert
Assert.Throws<ArgumentNullException>(() =>
#pragma warning disable 612
new DerivedAutoDataAttribute((IFixture)null));
#pragma warning restore 612
}

[Fact]
public void InitializeWithNullFixtureFactoryThrows()
{
// Arrange
// Act & assert
// Act & Assert
Assert.Throws<ArgumentNullException>(() =>
new DerivedAutoDataAttribute((Func<IFixture>)null));
new DerivedAutoDataAttribute(null));
}

[Fact]
public void FixtureFactoryIsNotInvokedImmediately()
public void DoesntActivateFixtureImmediately()
{
// Arrange
bool wasInvoked = false;
Func<IFixture> fixtureFactory = () =>
var wasInvoked = false;
IFixture FixtureFactory()
{
wasInvoked = true;
return null;
};
}

// Act
var sut = new DerivedAutoDataAttribute(fixtureFactory);
_ = new DerivedAutoDataAttribute(FixtureFactory);

// Assert
Assert.False(wasInvoked);
}

[Fact]
public void InitializedWithComposerHasCorrectComposer()
{
// Arrange
var expectedComposer = new DelegatingFixture();
#pragma warning disable 612
var sut = new DerivedAutoDataAttribute(expectedComposer);
#pragma warning restore 612
// Act
#pragma warning disable 618
var result = sut.Fixture;
#pragma warning restore 618
// Assert
Assert.Equal(expectedComposer, result);
}

[Fact]
[Obsolete]
public void InitializeWithNullTypeThrows()
{
// Arrange
// Act & assert
Assert.Throws<ArgumentNullException>(() =>
#pragma warning disable 618
new AutoDataAttribute((Type)null));
#pragma warning disable 618
}

[Fact]
[Obsolete]
public void InitializeWithNonComposerTypeThrows()
{
// Arrange
// Act & assert
Assert.Throws<ArgumentException>(() =>
new AutoDataAttribute(typeof(object)));
}

[Fact]
[Obsolete]
public void InitializeWithComposerTypeWithoutDefaultConstructorThrows()
{
// Arrange
// Act & assert
Assert.Throws<ArgumentException>(() =>
new AutoDataAttribute(typeof(ComposerWithoutADefaultConstructor)));
}

[Fact]
[Obsolete]
public void InitializedWithCorrectComposerTypeHasCorrectComposer()
{
// Arrange
var composerType = typeof(DelegatingFixture);
var sut = new AutoDataAttribute(composerType);
// Act
var result = sut.Fixture;
// Assert
Assert.IsAssignableFrom(composerType, result);
}

[Fact]
[Obsolete]
public void FixtureTypeIsCorrect()
{
// Arrange
var composerType = typeof(DelegatingFixture);
var sut = new AutoDataAttribute(composerType);
// Act
var result = sut.FixtureType;
// Assert
Assert.Equal(composerType, result);
}

[Fact]
public void GetDataWithNullMethodThrows()
{
// Arrange
var sut = new AutoDataAttribute();
var dummyTypes = Type.EmptyTypes;

// Act & assert
Assert.Throws<ArgumentNullException>(() =>
sut.GetData(null));
Assert.Throws<ArgumentNullException>(() => sut.GetData(null).ToArray());
}

[Fact]
public void GetDataReturnsCorrectResult()
{
// Arrange
var method = typeof(TypeWithOverloadedMembers).GetMethod("DoSomething", new[] { typeof(object) });
var parameters = method.GetParameters();

var method = typeof(TypeWithOverloadedMembers)
.GetMethod("DoSomething", new[] { typeof(object) });
var parameters = method!.GetParameters();
var expectedResult = new object();

object actualParameter = null;
ISpecimenContext actualContext = null;
var builder = new DelegatingSpecimenBuilder
{
OnCreate = (r, c) =>
{
Assert.Equal(parameters.Single(), r);
Assert.NotNull(c);
return expectedResult;
}
{
actualParameter = r;
actualContext = c;
return expectedResult;
}
};
var composer = new DelegatingFixture { OnCreate = builder.OnCreate };

var sut = new DerivedAutoDataAttribute(() => composer);

// Act
var result = sut.GetData(method);
var result = sut.GetData(method).ToArray();

// Assert
Assert.True(new[] { expectedResult }.SequenceEqual(result.Single()));
Assert.NotNull(actualContext);
Assert.Single(parameters);
Assert.Equal(parameters[0], actualParameter);
Assert.Equal(new[] { expectedResult }, result.Single());
}

[Theory]
Expand All @@ -213,7 +138,8 @@ public void GetDataReturnsCorrectResult()
public void GetDataOrdersCustomizationAttributes(string methodName)
{
// Arrange
var method = typeof(TypeWithCustomizationAttributes).GetMethod(methodName, new[] { typeof(ConcreteType) });
var method = typeof(TypeWithCustomizationAttributes)
.GetMethod(methodName, new[] { typeof(ConcreteType) });
var customizationLog = new List<ICustomization>();
var fixture = new DelegatingFixture();
fixture.OnCustomize = c =>
Expand All @@ -222,47 +148,14 @@ public void GetDataOrdersCustomizationAttributes(string methodName)
return fixture;
};
var sut = new DerivedAutoDataAttribute(() => fixture);
// Act
sut.GetData(method);
// Assert
Assert.False(customizationLog[0] is FreezeOnMatchCustomization);
Assert.True(customizationLog[1] is FreezeOnMatchCustomization);
}

private class DerivedAutoDataAttribute : AutoDataAttribute
{
[Obsolete]
public DerivedAutoDataAttribute(IFixture fixture)
: base(fixture)
{
}

public DerivedAutoDataAttribute(Func<IFixture> fixtureFactory)
: base(fixtureFactory)
{
}
}

private class TypeWithIParameterCustomizationSourceUsage
{
public void DecoratedMethod([CustomizationSource] int arg)
{
}

public class CustomizationSourceAttribute : Attribute, IParameterCustomizationSource
{
public ICustomization GetCustomization(ParameterInfo parameter)
{
return new Customization();
}
}
// Act
var data = sut.GetData(method).ToArray();

public class Customization : ICustomization
{
public void Customize(IFixture fixture)
{
}
}
// Assert
var composite = Assert.IsAssignableFrom<CompositeCustomization>(customizationLog[0]);
Assert.IsNotType<FreezeOnMatchCustomization>(composite.Customizations.First());
Assert.IsType<FreezeOnMatchCustomization>(composite.Customizations.Last());
}

[Fact]
Expand All @@ -282,9 +175,10 @@ public void ShouldRecognizeAttributesImplementingIParameterCustomizationSource()
var sut = new DerivedAutoDataAttribute(() => fixture);

// Act
sut.GetData(method);
sut.GetData(method).ToArray();

// Assert
Assert.True(customizationLog[0] is TypeWithIParameterCustomizationSourceUsage.Customization);
Assert.IsType<TypeWithIParameterCustomizationSourceUsage.Customization>(customizationLog[0]);
}

[Fact]
Expand Down

0 comments on commit 2fbe62c

Please sign in to comment.