Skip to content

Commit

Permalink
Now supports Half, BigIntegr, Exception
Browse files Browse the repository at this point in the history
Tests for record struct, recursive enumerable constructor
  • Loading branch information
soenneker committed Apr 29, 2024
1 parent 7784ebb commit 52ee1f2
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/Generators/Types/BigIntegerGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Numerics;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Generators.Abstract;

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class BigIntegerGenerator : IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
return new BigInteger(context.Faker.Random.Int());
}
}
4 changes: 1 addition & 3 deletions src/Generators/Types/EnumGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class EnumGenerator<TType>
: IAutoFakerGenerator
where TType: struct, Enum
internal sealed class EnumGenerator<TType>: IAutoFakerGenerator where TType: struct, Enum
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Generators/Types/ExceptionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Generators.Abstract;

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class ExceptionGenerator: IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
return new Exception(context.Faker.Lorem.Sentence());
}
}
3 changes: 1 addition & 2 deletions src/Generators/Types/FloatGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class FloatGenerator
: IAutoFakerGenerator
internal sealed class FloatGenerator: IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Generators/Types/HalfGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using Soenneker.Utils.AutoBogus.Context;
using Soenneker.Utils.AutoBogus.Generators.Abstract;

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class HalfGenerator : IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
return (Half)context.Faker.Random.Float(-65504, 65504);
}
}
3 changes: 1 addition & 2 deletions src/Generators/Types/IntGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

namespace Soenneker.Utils.AutoBogus.Generators.Types;

internal sealed class IntGenerator
: IAutoFakerGenerator
internal sealed class IntGenerator: IAutoFakerGenerator
{
object IAutoFakerGenerator.Generate(AutoFakerContext context)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Services/GeneratorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Numerics;

namespace Soenneker.Utils.AutoBogus.Services;

Expand Down Expand Up @@ -35,11 +36,14 @@ internal sealed class GeneratorService
{typeof(ulong), new Lazy<IAutoFakerGenerator>(() => new ULongGenerator())},
{typeof(Uri), new Lazy<IAutoFakerGenerator>(() => new UriGenerator())},
{typeof(ushort), new Lazy<IAutoFakerGenerator>(() => new UShortGenerator())},
{typeof(Half), new Lazy<IAutoFakerGenerator>(() => new HalfGenerator())},
{typeof(BigInteger), new Lazy<IAutoFakerGenerator>(() => new BigIntegerGenerator())},
{typeof(DateTimeOffset), new Lazy<IAutoFakerGenerator>(() => new DateTimeOffsetGenerator())},
{typeof(DateOnly), new Lazy<IAutoFakerGenerator>(() => new DateOnlyGenerator())},
{typeof(TimeOnly), new Lazy<IAutoFakerGenerator>(() => new TimeOnlyGenerator())},
{typeof(IPAddress), new Lazy<IAutoFakerGenerator>(() => new IpAddressGenerator())},
{typeof(MemoryStream), new Lazy<IAutoFakerGenerator>(() => new MemoryStreamGenerator())},
{typeof(Exception), new Lazy<IAutoFakerGenerator>(() => new ExceptionGenerator())},
};

private readonly Lazy<Dictionary<int, Lazy<IAutoFakerGenerator>>> _cachedFundamentalGeneratorsByInt;
Expand Down
1 change: 1 addition & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/AutoFakerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Soenneker.Utils.AutoBogus.Tests.Dtos;
using Soenneker.Utils.AutoBogus.Tests.Dtos.Complex;
using Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;
using Soenneker.Utils.AutoBogus.Tests.Extensions;
using Soenneker.Utils.AutoBogus.Tests.TestData;
using Xunit;

Expand Down
58 changes: 56 additions & 2 deletions test/Soenneker.Utils.AutoBogus.Tests/AutoFakerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,50 @@ public void Generate_record_should_generate()
record.Name.Should().NotBeNullOrEmpty();
}

[Fact]
public void Generate_record_struct_should_generate()
{
var faker = new AutoFaker();

var record = faker.Generate<TestRecordStruct>();

record.Should().NotBeNull();
record.Name.Should().NotBeNullOrEmpty();
}

[Fact]
public void Generate_TestClassWithNumber_should_generate()
{
var faker = new AutoFaker();

var testClass = faker.Generate<TestClassWithNumber<int>>();

testClass.Should().NotBeNull();
testClass.Number.Should().NotBe(0);
}

[Fact]
public void Generate_BigInteger_should_generate()
{
var faker = new AutoFaker();

var testClass = faker.Generate<TestClassWithBigInteger>();

testClass.Should().NotBeNull();
testClass.BigInteger.Should().NotBeNull();
}

[Fact]
public void Generate_Exception_should_generate()
{
var faker = new AutoFaker();

var testClass = faker.Generate<TestClassWithException>();

testClass.Should().NotBeNull();
testClass.Exception.Should().NotBeNull();
}

[Fact]
public void Generate_TestRecordWithRecursiveConstructor_should_generate()
{
Expand All @@ -71,6 +115,16 @@ public void Generate_TestClassWithRecursiveConstructor_should_generate()
classObj.Name.Should().NotBeNullOrEmpty();
}

[Fact]
public void Generate_TestClassWithRecursiveEnumerableConstructor_should_generate()
{
var faker = new AutoFaker();

var classObj = faker.Generate<TestClassWithRecursiveEnumerableConstructor>();
classObj.Should().NotBeNull();
classObj.Name.Should().NotBeNullOrEmpty();
}

[Fact]
public void Generate_TestClassIReadOnlyCollection_should_generate()
{
Expand Down Expand Up @@ -206,7 +260,7 @@ public void Generate_with_smartenum_should_generate()

order.NullableDaysOfWeek.Should().NotBeNull();
order.NullableDaysOfWeek.Should().BeEmpty();

order.Longitude.Should().NotBeNull();
CustomOrder.Constant.Should().Be("Order2x89ei");
}
Expand Down Expand Up @@ -242,7 +296,7 @@ public void Generate_MemoryStream_should_be_null()
public void Generate_Video_should_generate()
{
var faker = new AutoFaker();

var video = faker.Generate<Video>();
video.Should().NotBeNull();
video.MemoryStreamsList.Should().NotBeNullOrEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos;
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Abstract;

public interface IRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos;
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Abstract;

public interface ITimestamp
{
Expand Down
1 change: 1 addition & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/Dtos/Item.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Soenneker.Utils.AutoBogus.Tests.Dtos.Abstract;
using Soenneker.Utils.AutoBogus.Tests.Enums;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos;
Expand Down
1 change: 1 addition & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/Dtos/Service.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Soenneker.Utils.AutoBogus.Tests.Dtos.Abstract;
using Soenneker.Utils.AutoBogus.Tests.Enums;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Soenneker.Utils.AutoBogus.Tests.Enums;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Complex;
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

public class CalendarItem
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Numerics;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal class TestClassWithBigInteger
{
public BigInteger BigInteger { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

public class TestClassWithException
{
public Exception Exception { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Numerics;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal class TestClassWithNumber<T> where T : INumber<T>
{
public T Number { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal class TestClassWithRecursiveEnumerableConstructor
{
public string? Name { get; set; }
public int Age { get; set; }

public IEnumerable<TestClassWithRecursiveEnumerableConstructor> Child { get; set; }

public TestClassWithRecursiveEnumerableConstructor(string? name, int age, IEnumerable<TestClassWithRecursiveEnumerableConstructor> child)
{
Name = name;
Age = age;
Child = child;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
internal record TestRecord
{
public string? Name { get; set; }

public int Age { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Simple;

internal record struct TestRecordStruct
{
public string? Name { get; set; }

public int Age { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using FluentAssertions;
using FluentAssertions.Primitives;
using Soenneker.Utils.AutoBogus.Tests.Dtos;

namespace Soenneker.Utils.AutoBogus.Tests.Dtos;
namespace Soenneker.Utils.AutoBogus.Tests.Extensions;

public static class GenerateExtensions
public static class ObjectAssertionsExtensions
{
public static AndConstraint<object> BeGenerated(this ObjectAssertions assertions)
{
Expand Down
11 changes: 11 additions & 0 deletions test/Soenneker.Utils.AutoBogus.Tests/TestData/TypeTestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Numerics;

namespace Soenneker.Utils.AutoBogus.Tests.TestData;

Expand All @@ -25,8 +26,14 @@ public IEnumerator<object[]> GetEnumerator()
yield return [typeof(sbyte?), typeof(sbyte)];
yield return [typeof(short?), typeof(short)];
yield return [typeof(uint?), typeof(uint)];
yield return [typeof(Int16?), typeof(Int16)];
yield return [typeof(Int32?), typeof(Int32)];
yield return [typeof(ulong?), typeof(ulong)];
yield return [typeof(Uri), typeof(Uri)];
yield return [typeof(Half?), typeof(Half)];

// ReSharper disable once ConvertNullableToShortForm
yield return [typeof(Nullable<int>), typeof(int)];

// Non-nullable types
yield return [typeof(string), typeof(string)];
Expand All @@ -43,10 +50,14 @@ public IEnumerator<object[]> GetEnumerator()
yield return [typeof(sbyte), typeof(sbyte)];
yield return [typeof(short), typeof(short)];
yield return [typeof(uint), typeof(uint)];
yield return [typeof(Int16), typeof(Int16)];
yield return [typeof(Int32), typeof(Int32)];
yield return [typeof(ulong), typeof(ulong)];
yield return [typeof(Uri), typeof(Uri)];
yield return [typeof(DateTimeOffset), typeof(DateTimeOffset)];
yield return [typeof(IPAddress), typeof(IPAddress)];
yield return [typeof(Half), typeof(Half)];
yield return [typeof(TimeSpan), typeof(TimeSpan)];

// Enumerable types
yield return [typeof(IEnumerable<string>), typeof(List<string>)];
Expand Down

0 comments on commit 52ee1f2

Please sign in to comment.