diff --git a/DapperAutoData/AutoMoqDataAttribute.cs b/DapperAutoData/AutoMoqDataAttribute.cs index 77b7426..df264ad 100644 --- a/DapperAutoData/AutoMoqDataAttribute.cs +++ b/DapperAutoData/AutoMoqDataAttribute.cs @@ -3,48 +3,33 @@ using AutoFixture.AutoMoq; using AutoFixture.Xunit2; -namespace DapperAutoData.ContentFiles; +namespace DapperAutoData; /// /// Attribute used to mark a test method for AutoFixture data generation. /// -public class DapperAutoDataAttribute : InlineAutoDataAttribute +public class DapperAutoDataAttribute(params object[] values) + : InlineAutoDataAttribute(new AutoFixtureMoqDataAttribute(), values) { - public DapperAutoDataAttribute(params object[] values) - : base(new AutoFixtureMoqDataAttribute(), values) + public class AutoFixtureMoqDataAttribute() : AutoDataAttribute(() => { - } + var fixture = new Fixture().Customize(new DefaultCustomizations()); - 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); - } + var customizationsTypes = Assembly.GetExecutingAssembly() + .GetTypes() + .Where(t => typeof(IDapperProjectCustomization).IsAssignableFrom(t) && !t.IsInterface); - return fixture; - }) + 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 })) - { - } -} \ No newline at end of file +public class AutoDomainDataAttribute() + : AutoDataAttribute(() => new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true })); \ No newline at end of file diff --git a/DapperAutoData/DapperAutoData.csproj b/DapperAutoData/DapperAutoData.csproj index 315ce09..3190c72 100644 --- a/DapperAutoData/DapperAutoData.csproj +++ b/DapperAutoData/DapperAutoData.csproj @@ -1,7 +1,7 @@  - net6.0;net7.0;net8.0 + net8.0 enable enable Library @@ -14,7 +14,7 @@ README.md xunit;autofixture;c#;.net;unittesting DapperAutoData - 1.0.7 + 1.0.8 @@ -36,12 +36,7 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - diff --git a/DapperAutoData/DataGeneratorInstaller.cs b/DapperAutoData/DataGeneratorInstaller.cs index 7ecbc02..5b4a348 100644 --- a/DapperAutoData/DataGeneratorInstaller.cs +++ b/DapperAutoData/DataGeneratorInstaller.cs @@ -15,9 +15,7 @@ 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 + && type is { IsInterface: false, IsAbstract: false, ContainsGenericParameters: false } && !IsSubclassOfRawGeneric(typeof(DateTimeGenerator<>), type) && !IsSubclassOfRawGeneric(typeof(DateTimeOffsetGenerator<>), type)) .ToList(); diff --git a/DapperAutoData/Generators/DateTimeGenerator.cs b/DapperAutoData/Generators/DateTimeGenerator.cs index c5773cc..9b1a816 100644 --- a/DapperAutoData/Generators/DateTimeGenerator.cs +++ b/DapperAutoData/Generators/DateTimeGenerator.cs @@ -7,21 +7,15 @@ namespace DapperAutoData.Generators; /// Set of classes that generate DateTime and DateTimeOffset values. /// /// -public class DateTimeGenerator : IDataGenerator +public class DateTimeGenerator(DateTime value) : IDataGenerator { - public DateTime Value { get; } + public DateTime Value { get; } = value; private readonly Func _generator = null!; - public DateTimeGenerator(DateTime value) - { - Value = value; - } - - public DateTimeGenerator(Func generator) + public DateTimeGenerator(Func generator) : this(generator(new Faker())) { _generator = generator; - Value = generator(new Faker()); } public static implicit operator DateTimeGenerator(DateTime y) => new(y); @@ -37,21 +31,15 @@ public DateTimeGenerator(Func generator) public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); } -public class DateTimeOffsetGenerator : IDataGenerator +public class DateTimeOffsetGenerator(DateTimeOffset value) : IDataGenerator { - public DateTimeOffset Value { get; } + public DateTimeOffset Value { get; } = value; private readonly Func _generator; - public DateTimeOffsetGenerator(DateTimeOffset value) - { - Value = value; - } - - public DateTimeOffsetGenerator(Func generator) + public DateTimeOffsetGenerator(Func generator) : this(generator(new Faker())) { _generator = generator; - Value = generator(new Faker()); } public static implicit operator DateTimeOffsetGenerator(DateTimeOffset y) => new(y); @@ -67,50 +55,29 @@ public DateTimeOffsetGenerator(Func generator) public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); } -public class DateTimeFuture : DateTimeGenerator -{ - public DateTimeFuture() : base(faker => faker.Date.Future()) { } -} +public class DateTimeFuture() : DateTimeGenerator(faker => faker.Date.Future()); -public class DateTimeOffsetFuture : DateTimeOffsetGenerator -{ - public DateTimeOffsetFuture() : base(faker => faker.Date.FutureOffset()) { } -} +public class DateTimeOffsetFuture() : DateTimeOffsetGenerator(faker => faker.Date.FutureOffset()); -public class DateTimeOfBirth : DateTimeGenerator -{ - public DateTimeOfBirth() : base(faker => faker.Date.Between(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { } -} +public class DateTimeOfBirth() : DateTimeGenerator(faker => + faker.Date.Between(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))); -public class DateTimeOffsetOfBirth : DateTimeOffsetGenerator -{ - public DateTimeOffsetOfBirth() : base(faker => faker.Date.BetweenOffset(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))) { } -} +public class DateTimeOffsetOfBirth() : DateTimeOffsetGenerator(faker => + faker.Date.BetweenOffset(DateTime.Now.AddYears(-80), DateTime.Now.AddYears(-18))); -public class DateTimePast : DateTimeGenerator -{ - public DateTimePast() : base(faker => faker.Date.Past()) { } -} +public class DateTimePast() : DateTimeGenerator(faker => faker.Date.Past()); -public class DateTimeOffsetPast : DateTimeOffsetGenerator -{ - public DateTimeOffsetPast() : base(faker => faker.Date.PastOffset()) { } -} +public class DateTimeOffsetPast() : DateTimeOffsetGenerator(faker => faker.Date.PastOffset()); -public class CurrentDate : DateTimeGenerator -{ - public CurrentDate() : base(DateTime.Now) { } -} +public class CurrentDate() : DateTimeGenerator(DateTime.Now); -public class DateSpecialEvent : DateTimeGenerator +public class DateSpecialEvent() : DateTimeGenerator(new DateTime(2023, 12, 25)) { - public DateSpecialEvent() : base(new DateTime(2023, 12, 25)) { } // Example: Christmas + // Example: Christmas } -public class DateWeekday : DateTimeGenerator +public class DateWeekday() : DateTimeGenerator(GenerateWeekdayDate) { - public DateWeekday() : base(GenerateWeekdayDate) { } - private static DateTime GenerateWeekdayDate(Faker faker) { DateTime date; @@ -122,10 +89,8 @@ private static DateTime GenerateWeekdayDate(Faker faker) } } -public class DateWeekend : DateTimeGenerator +public class DateWeekend() : DateTimeGenerator(GenerateWeekendDate) { - public DateWeekend() : base(GenerateWeekendDate) { } - private static DateTime GenerateWeekendDate(Faker faker) { DateTime date; @@ -137,12 +102,10 @@ private static DateTime GenerateWeekendDate(Faker faker) } } -public class DateLeapYear : DateTimeGenerator +public class DateLeapYear() : DateTimeGenerator(new DateTime(2024, 2, 29)) { - public DateLeapYear() : base(new DateTime(2024, 2, 29)) { } // Example: Leap year date + // Example: Leap year date } -public class DateCustomFormat : DateTimeGenerator -{ - public DateCustomFormat() : base(faker => DateTime.Parse(faker.Date.Recent(7).ToShortDateString())) { } -} \ No newline at end of file +public class DateCustomFormat() + : DateTimeGenerator(faker => DateTime.Parse(faker.Date.Recent(7).ToShortDateString())); \ No newline at end of file diff --git a/DapperAutoData/Generators/NumberGenerator.cs b/DapperAutoData/Generators/NumberGenerator.cs index 173ad5d..df870b2 100644 --- a/DapperAutoData/Generators/NumberGenerator.cs +++ b/DapperAutoData/Generators/NumberGenerator.cs @@ -26,7 +26,9 @@ public class NumberGenerator : IDataGenerator public void RegisterGenerators(IFixture fixture) => fixture.Register(() => Generate(fixture, _generator)); } -public class NumberPositive : NumberGenerator { public NumberPositive() : base(faker => faker.Random.Decimal(1, decimal.MaxValue)) { } } -public class NumberNegative : NumberGenerator { public NumberNegative() : base(faker => faker.Random.Decimal(decimal.MinValue, -1)) { } } -public class NumberMoney : NumberGenerator { public NumberMoney() : base(GenerateFakeMoney) { } private static decimal GenerateFakeMoney(Faker faker) => faker.Random.Decimal(1, 1000) + faker.Random.Decimal(0.01m, 0.99m); } -public class NumberFraction : NumberGenerator { public NumberFraction() : base(faker => Math.Round(faker.Random.Decimal(0.01m, 0.99m), 2)) { } } \ No newline at end of file +public class NumberPositive() : NumberGenerator(faker => faker.Random.Decimal(1, decimal.MaxValue)); +public class NumberNegative() : NumberGenerator(faker => faker.Random.Decimal(decimal.MinValue, -1)); +public class NumberMoney() : NumberGenerator(GenerateFakeMoney) +{ private static decimal GenerateFakeMoney(Faker faker) => faker.Random.Decimal(1, 1000) + faker.Random.Decimal(0.01m, 0.99m); } +public class NumberFraction() + : NumberGenerator(faker => Math.Round(faker.Random.Decimal(0.01m, 0.99m), 2)); \ No newline at end of file diff --git a/DapperAutoData/Generators/StringGenerator.cs b/DapperAutoData/Generators/StringGenerator.cs index ebaddcd..ee5b6ca 100644 --- a/DapperAutoData/Generators/StringGenerator.cs +++ b/DapperAutoData/Generators/StringGenerator.cs @@ -21,30 +21,30 @@ public class StringGenerator : IDataGenerator public void RegisterGenerators(IFixture fixture) => fixture.Register(() => new StringGenerator(_generator)); } -public class StringParagraph : StringGenerator { public StringParagraph() : base(faker => faker.Lorem.Paragraph()) { } } -public class StringPersonFullName : StringGenerator { public StringPersonFullName() : base(faker => faker.Name.FullName()) { } } -public class StringPhoneNumber : StringGenerator { public StringPhoneNumber() : base(faker => faker.Phone.PhoneNumber()) { } } -public class StringSentence : StringGenerator { public StringSentence() : base(faker => faker.Lorem.Sentence()) { } } -public class StringSsn : StringGenerator { public StringSsn() : base(faker => $"{faker.Random.Number(100, 999)}-{faker.Random.Number(10, 99)}-{faker.Random.Number(1000, 9999)}") { } } -public class StringWord : StringGenerator { public StringWord() : base(faker => faker.Lorem.Word()) { } } -public class StringCompanyName : StringGenerator { public StringCompanyName() : base(faker => faker.Company.CompanyName()) { } } -public class StringEmailTest : StringGenerator { public StringEmailTest() : base(faker => $"{faker.Internet.DomainName()}@FakeEmailAddress.com") { } } -public class StringFirstName : StringGenerator { public StringFirstName() : base(faker => faker.Name.FirstName()) { } } -public class StringInternetUrl : StringGenerator { public StringInternetUrl() : base(faker => faker.Internet.Url()) { } } -public class StringInternetUsername : StringGenerator { public StringInternetUsername() : base(faker => faker.Internet.UserName()) { } } +public class StringParagraph() : StringGenerator(faker => faker.Lorem.Paragraph()); +public class StringPersonFullName() : StringGenerator(faker => faker.Name.FullName()); +public class StringPhoneNumber() : StringGenerator(faker => faker.Phone.PhoneNumber()); +public class StringSentence() : StringGenerator(faker => faker.Lorem.Sentence()); +public class StringSsn() : StringGenerator(faker => $"{faker.Random.Number(100, 999)}-{faker.Random.Number(10, 99)}-{faker.Random.Number(1000, 9999)}"); +public class StringWord() : StringGenerator(faker => faker.Lorem.Word()); +public class StringCompanyName() : StringGenerator(faker => faker.Company.CompanyName()); +public class StringEmailTest() : StringGenerator(faker => $"{faker.Internet.DomainName()}@FakeEmailAddress.com"); +public class StringFirstName() : StringGenerator(faker => faker.Name.FirstName()); +public class StringInternetUrl() : StringGenerator(faker => faker.Internet.Url()); +public class StringInternetUsername() : StringGenerator(faker => faker.Internet.UserName()); -public class StringCityName : StringGenerator { public StringCityName() : base(faker => faker.Address.City()) { } } -public class StringCountryName : StringGenerator { public StringCountryName() : base(faker => faker.Address.Country()) { } } -public class StringPostalCode : StringGenerator { public StringPostalCode() : base(faker => faker.Address.ZipCode()) { } } -public class StringStateAbbreviation : StringGenerator { public StringStateAbbreviation() : base(faker => faker.Address.StateAbbr()) { } } -public class StringStateFullName : StringGenerator { public StringStateFullName() : base(faker => faker.Address.State()) { } } -public class StringStreetAddress : StringGenerator { public StringStreetAddress() : base(faker => faker.Address.StreetAddress()) { } } -public class StringJobTitle : StringGenerator { public StringJobTitle() : base(faker => faker.Name.JobTitle()) { } } -public class StringProductCategory : StringGenerator { public StringProductCategory() : base(faker => faker.Commerce.Categories(1)[0]) { } } -public class StringProductDescription : StringGenerator { public StringProductDescription() : base(faker => faker.Commerce.ProductDescription()) { } } -public class StringCurrency : StringGenerator { public StringCurrency() : base(faker => faker.Finance.Currency().Code) { } } // Extracting the currency code as a string -public class StringFileExtension : StringGenerator { public StringFileExtension() : base(faker => faker.System.CommonFileExt()) { } } -public class StringIPAddress : StringGenerator { public StringIPAddress() : base(faker => faker.Internet.Ip()) { } } -public class StringHtmlTag : StringGenerator { public StringHtmlTag() : base(faker => faker.Random.AlphaNumeric(5)) { } } -public class StringPassword : StringGenerator { public StringPassword() : base(faker => faker.Internet.Password()) { } } -public class StringGuid : StringGenerator { public StringGuid() : base(faker => faker.Random.Guid().ToString()) { } } \ No newline at end of file +public class StringCityName() : StringGenerator(faker => faker.Address.City()); +public class StringCountryName() : StringGenerator(faker => faker.Address.Country()); +public class StringPostalCode() : StringGenerator(faker => faker.Address.ZipCode()); +public class StringStateAbbreviation() : StringGenerator(faker => faker.Address.StateAbbr()); +public class StringStateFullName() : StringGenerator(faker => faker.Address.State()); +public class StringStreetAddress() : StringGenerator(faker => faker.Address.StreetAddress()); +public class StringJobTitle() : StringGenerator(faker => faker.Name.JobTitle()); +public class StringProductCategory() : StringGenerator(faker => faker.Commerce.Categories(1)[0]); +public class StringProductDescription() : StringGenerator(faker => faker.Commerce.ProductDescription()); +public class StringCurrency() : StringGenerator(faker => faker.Finance.Currency().Code); // Extracting the currency code as a string +public class StringFileExtension() : StringGenerator(faker => faker.System.CommonFileExt()); +public class StringIPAddress() : StringGenerator(faker => faker.Internet.Ip()); +public class StringHtmlTag() : StringGenerator(faker => faker.Random.AlphaNumeric(5)); +public class StringPassword() : StringGenerator(faker => faker.Internet.Password()); +public class StringGuid() : StringGenerator(faker => faker.Random.Guid().ToString()); \ No newline at end of file