Skip to content

Commit

Permalink
feat: added additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Nov 12, 2023
1 parent fc56f3c commit edb5e4b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 1 deletion.
95 changes: 95 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/QueryableProjectionLoopTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,101 @@ public Task ReferenceLoopInitProperty()
return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task SetRecursiveDepthForLoop()
{
var source = TestSourceBuilder.Mapping(
"System.Linq.IQueryable<A>",
"System.Linq.IQueryable<B>",
TestSourceBuilderOptions.Default with
{
MaxRecursiveDepth = 2
},
"class A { public A? Parent { get; set; } }",
"class B { public B? Parent { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task MethodAttributeSetRecursiveDepthForLoop()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"[MapperMaxRecursiveDepth(2)] "
+ "partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> src);"
+ "[MapperMaxRecursiveDepth(2)] partial B Map(A src);",
"class A { public A? Parent { get; set; } }",
"class B { public B? Parent { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task MethodAttributeOverridesClassSetRecursiveDepthForLoop()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"[MapperMaxRecursiveDepth(2)] "
+ "partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> src);"
+ "[MapperMaxRecursiveDepth(2)] partial B Map(A src);",
TestSourceBuilderOptions.Default with
{
MaxRecursiveDepth = 4
},
"class A { public A? Parent { get; set; } }",
"class B { public B? Parent { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task AssemblyDefaultShouldWork()
{
var source = TestSourceBuilder.CSharp(
"""
using Riok.Mapperly.Abstractions;
[assembly: MapperDefaultsAttribute(MaxRecursiveDepth = 2)]
[Mapper()]
public partial class MyMapper
{
private partial B Map(A source);
}
class A { public A? Parent { get; set; } }
class B { public B? Parent { get; set; } }
"""
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task AttributeShouldOverrideAssemblyDefault()
{
var source = TestSourceBuilder.CSharp(
"""
using Riok.Mapperly.Abstractions;
[assembly: MapperDefaultsAttribute(MaxRecursiveDepth = 2)]
[Mapper(MaxRecursiveDepth = 4)]
public partial class MyMapper
{
private partial B Map(A source);
}
class A { public A? Parent { get; set; } }
class B { public B? Parent { get; set; } }
"""
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ReferenceLoopCtor()
{
Expand Down
4 changes: 4 additions & 0 deletions test/Riok.Mapperly.Tests/TestSourceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private static string BuildAttribute(TestSourceBuilderOptions options)
Attribute(options.IgnoreObsoleteMembersStrategy),
Attribute(options.RequiredMappingStrategy),
Attribute(options.IncludedMembers),
Attribute(options.MaxRecursiveDepth),
}.WhereNotNull();

return $"[Mapper({string.Join(", ", attrs)})]";
Expand All @@ -109,6 +110,9 @@ private static string BuildAttribute(TestSourceBuilderOptions options)
private static string? Attribute(bool? value, [CallerArgumentExpression("value")] string? expression = null) =>
value.HasValue ? Attribute(value.Value ? "true" : "false", expression) : null;

private static string? Attribute(int? value, [CallerArgumentExpression("value")] string? expression = null) =>
value.HasValue ? Attribute(value.Value.ToString(), expression) : null;

private static string? Attribute(string? value, [CallerArgumentExpression("value")] string? expression = null)
{
if (value == null)
Expand Down
3 changes: 2 additions & 1 deletion test/Riok.Mapperly.Tests/TestSourceBuilderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public record TestSourceBuilderOptions(
bool? EnumMappingIgnoreCase = null,
IgnoreObsoleteMembersStrategy? IgnoreObsoleteMembersStrategy = null,
RequiredMappingStrategy? RequiredMappingStrategy = null,
MemberVisibility? IncludedMembers = null
MemberVisibility? IncludedMembers = null,
int? MaxRecursiveDepth = null
)
{
public const string DefaultMapperClassName = "Mapper";
Expand Down

0 comments on commit edb5e4b

Please sign in to comment.