Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Mapster.Tool.Tests/Helpers/ConfigHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;

namespace Mapster.Tool.Tests.Helpers
{
internal static class ConfigHelpers
{
internal static MapperOptions optMappers => new MapperOptions() { Assembly = Assembly.GetExecutingAssembly().Location, Output = Path.GetTempPath() };
internal static ModelOptions optModels = new ModelOptions() { Assembly = Assembly.GetExecutingAssembly().Location, Output = Path.GetTempPath() };
internal static ExtensionOptions optExtentions = new ExtensionOptions() { Assembly = Assembly.GetExecutingAssembly().Location, Output = Path.GetTempPath() };
}
}
5 changes: 1 addition & 4 deletions src/Mapster.Tool.Tests/Mapster.Tool.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
<TargetFrameworks>net10.0;net9.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TargetFrameworks>$(MapsterToolTFMs)</TargetFrameworks>
<IsTestProject>true</IsTestProject>
<IsPackable>false</IsPackable>
</PropertyGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFrameworks>$(TargetFrameworks);net48</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
Expand Down
4 changes: 3 additions & 1 deletion src/Mapster.Tool.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
global using Xunit;
global using Xunit;
global using Mapster.Tool;
global using Mapster.Tool.Tests.Helpers;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,57 @@ public void MapWithReflection()
userMapper.MapTo(user, dto);
dto.Name.Should().Be(expected);
}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/1017
/// </summary>
[Fact]
public void CreateDtoWithcustomResolver()
{
var mappers = new List<string>();

Generators.GenerateExtensions(ConfigHelpers.optExtentions, mappers);

var result = mappers.Where(x => x.Contains("User1017Dto AdaptToDto(this User1017")).FirstOrDefault();

result.Should().NotBeNullOrEmpty();
result.Contains("FullName = string.Format(\"{0} {1}\", p1.FirstName, p1.LastName)").Should().BeTrue();
}
}


public class User1017
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

public partial class User1017Dto
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}


public class UserCodeGenConfig : ICodeGenerationRegister
{
public void Register(CodeGenerationConfig config)
{
config.AdaptTo("[name]Dto", MapType.Map)
.ForType<User1017>(p =>
{
p.Ignore(s => s.FirstName);
p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");
});

config.GenerateMapper("[name]Mapper")
.ForType<User1017>();
}
}

public class UserMappingRegister : IRegister
Expand Down
Loading
Loading