Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
riplikash committed Apr 25, 2024
2 parents df83474 + 734c2a5 commit 0d4db3d
Show file tree
Hide file tree
Showing 25 changed files with 406 additions and 345 deletions.
50 changes: 50 additions & 0 deletions DapperAutoData/AutoMoqDataAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Reflection;
using AutoFixture;
using AutoFixture.AutoMoq;
using AutoFixture.Xunit2;

namespace DapperAutoData.ContentFiles;

/// <summary>
/// Attribute used to mark a test method for AutoFixture data generation.
/// </summary>
public class DapperAutoDataAttribute : InlineAutoDataAttribute
{
public DapperAutoDataAttribute(params object[] values)
: base(new AutoFixtureMoqDataAttribute(), values)
{
}

public class AutoFixtureMoqDataAttribute : AutoDataAttribute
{
public AutoFixtureMoqDataAttribute()
: base(() =>
{
var fixture = new Fixture().Customize(new DefaultCustomizations());

var customizationsTypes = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => typeof(IDapperProjectCustomization).IsAssignableFrom(t) && !t.IsInterface);

foreach (var customizationsType in customizationsTypes)
{
var customizations = (IDapperProjectCustomization)Activator.CreateInstance(customizationsType);
fixture.Customize(customizations);
}

return fixture;
})
{
}
}

}

[AttributeUsage(AttributeTargets.Method)]
public class AutoDomainDataAttribute : AutoDataAttribute
{
public AutoDomainDataAttribute()
: base(() => new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true }))
{
}
}
46 changes: 0 additions & 46 deletions DapperAutoData/ContentFiles/AutoMoqDataAttribute.cs

This file was deleted.

16 changes: 0 additions & 16 deletions DapperAutoData/ContentFiles/DataGeneratorInstaller.cs

This file was deleted.

17 changes: 8 additions & 9 deletions DapperAutoData/DapperAutoData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Bogus" Version="35.0.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Faker.Net" Version="2.0.154" />
<PackageReference Include="AutoFixture" Version="4.17.0" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.17.0" />
<PackageReference Include="AutoFixture.Xunit2" Version="4.17.0" />
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand All @@ -54,8 +53,8 @@
<ItemGroup>
<None Include="..\README.md" Pack="True" PackagePath="\" />
<None Include="DapperAutoData.props" Pack="True" PackagePath="build\" />
<None Include="ContentFiles\AutoMoqDataAttribute.cs" Pack="True" PackagePath="contentFiles\cs\netstandard2.0\" />
<None Include="ContentFiles\DataGeneratorInstaller.cs" Pack="True" PackagePath="contentFiles\cs\netstandard2.0\" />
<None Include="AutoMoqDataAttribute.cs" Pack="True" PackagePath="contentFiles\cs\netstandard2.0\" />
<None Include="DataGeneratorInstaller.cs" Pack="True" PackagePath="contentFiles\cs\netstandard2.0\" />
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions DapperAutoData/DapperAutoData.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@
<licenseUrl>https://opensource.org/licenses/MIT</licenseUrl>
<dependencies>
<group targetFramework="net6.0">
<dependency id="Faker.Net" version="2.0.154" />
<dependency id="Bogus" version="35.0.1" />
<dependency id="xunit" version="2.4.2" />
<dependency id="xunit.runner.visualstudio" version="2.5.5" />
<dependency id="AutoFixture" version="4.17.0" />
<dependency id="AutoFixture.AutoMoq" version="4.17.0" />
<dependency id="AutoFixture.Xunit2" version="4.17.0" />
<dependency id="FluentAssertions" version="6.7.0" />
<dependency id="Microsoft.NET.Test.Sdk" version="17.2.0" />
<dependency id="Moq" version="4.18.2" />
<dependency id="coverlet.collector" version="3.2.0" />
</group>
<group targetFramework="net7.0">
<dependency id="Faker.Net" version="2.0.154" />
<dependency id="Bogus" version="35.0.1" />
<dependency id="xunit" version="2.4.2" />
<dependency id="xunit.runner.visualstudio" version="2.5.5" />
<dependency id="AutoFixture" version="4.17.0" />
<dependency id="AutoFixture.AutoMoq" version="4.17.0" />
<dependency id="AutoFixture.Xunit2" version="4.17.0" />
<dependency id="FluentAssertions" version="6.7.0" />
<dependency id="Microsoft.NET.Test.Sdk" version="17.2.0" />
<dependency id="Moq" version="4.18.2" />
<dependency id="coverlet.collector" version="3.2.0" />
</group>
</dependencies>
</metadata>
Expand Down
42 changes: 42 additions & 0 deletions DapperAutoData/DataGeneratorInstaller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using AutoFixture;
using DapperAutoData.Generators;

namespace DapperAutoData;

/// <summary>
/// This class is responsible for registering all the data generators in the assembly.
/// It uses reflection to find all the classes that implement the IDataGenerator interface and registers them.
/// It also filters out the DateTimeGenerator and DateTimeOffsetGenerator classes as they are generic and should not be registered.
/// </summary>
public static class DataGeneratorInstaller
{
public static void Run(IFixture fixture)
{
var list = System.Reflection.Assembly.GetCallingAssembly()
.GetTypes()
.Where(type => typeof(IDataGenerator).IsAssignableFrom(type)
&& !type.IsInterface
&& !type.IsAbstract
&& !type.ContainsGenericParameters
&& !IsSubclassOfRawGeneric(typeof(DateTimeGenerator<>), type)
&& !IsSubclassOfRawGeneric(typeof(DateTimeOffsetGenerator<>), type))
.ToList();
list.ForEach(type =>
(Activator.CreateInstance(type) as IDataGenerator)?.RegisterGenerators(fixture));
}

private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}

}
17 changes: 17 additions & 0 deletions DapperAutoData/DefaultCustomizations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AutoFixture;
using AutoFixture.AutoMoq;

namespace DapperAutoData;

/// <summary>
/// Class that registers the default customizations for the AutoFixture instance.
/// Sets up AutoMoqCustomization and registers the data generators.
/// </summary>
public class DefaultCustomizations : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize(new AutoMoqCustomization() { ConfigureMembers = true });
DataGeneratorInstaller.Run(fixture);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AutoFixture;

namespace DapperAutoData.Lib.Generators;
namespace DapperAutoData.Generators;

public class BooleanGenerators : IDataGenerator
{
Expand Down
Loading

0 comments on commit 0d4db3d

Please sign in to comment.