Skip to content

Commit

Permalink
Excludes constants in automembers
Browse files Browse the repository at this point in the history
  • Loading branch information
soenneker committed Feb 9, 2024
1 parent 39cdd60 commit 1b863d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
22 changes: 15 additions & 7 deletions src/AutoFakerBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public AutoFakerBinder(AutoFakerConfig autoFakerConfig)

if (cachedParameters.Length == 0)
{
return (TType?)constructor.Invoke();
return (TType?) constructor.Invoke();
}

var parameters = new object[cachedParameters.Length];
Expand Down Expand Up @@ -231,16 +231,24 @@ private List<AutoMember> GetMembersToPopulate(CachedType cachedType)

for (var i = 0; i < properties.Length; i++)
{
if (properties[i].IsConstant())
continue;

PropertyInfo property = properties[i];
autoMembers.Add(new AutoMember(property, _autoFakerConfig));
}

for (var i = 0; i < fields.Length; i++)
if (fields != null)
{
FieldInfo field = fields[i];
autoMembers.Add(new AutoMember(field, _autoFakerConfig));
}
for (var i = 0; i < fields.Length; i++)
{
if (fields[i].IsConstant())
continue;

FieldInfo field = fields[i];
autoMembers.Add(new AutoMember(field, _autoFakerConfig));
}
}
_autoMembersCache.TryAdd(cachedType, autoMembers);

return autoMembers;
Expand All @@ -255,7 +263,7 @@ private static void PopulateDictionary(object value, object parent, AutoMember m
CachedType[] argTypes = member.CachedType.GetAddMethodArgumentTypes();
CachedMethod? addMethod = GetAddMethod(member.CachedType, argTypes);

if (instance == null || addMethod == null)
if (instance == null || addMethod == null)
return;

foreach (object? key in dictionary.Keys)
Expand All @@ -273,7 +281,7 @@ private static void PopulateCollection(object value, object parent, AutoMember m
CachedType[] argTypes = member.CachedType.GetAddMethodArgumentTypes();
CachedMethod? addMethod = GetAddMethod(member.CachedType, argTypes);

if (instance == null || addMethod == null)
if (instance == null || addMethod == null)
return;

foreach (object? item in collection)
Expand Down
2 changes: 1 addition & 1 deletion src/Soenneker.Utils.AutoBogus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ItemGroup>
<PackageReference Include="Ardalis.SmartEnum" Version="8.0.0" />
<PackageReference Include="Bogus" Version="35.4.0" />
<PackageReference Include="Soenneker.Reflection.Cache" Version="2.1.36" />
<PackageReference Include="Soenneker.Reflection.Cache" Version="2.1.37" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
</ItemGroup>
</Project>
6 changes: 5 additions & 1 deletion test/Soenneker.Utils.AutoBogus.Tests/Dtos/Complex/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ namespace Soenneker.Utils.AutoBogus.Tests.Dtos.Complex;
public sealed class Order
{
public DateTime Timestamp;


private int _privateFieldId;

public const string Constant = "Order2x89ei";

public Order(int id, ICalculator calculator)
{
Id = id;
Expand Down

0 comments on commit 1b863d0

Please sign in to comment.