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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"mapster.tool": {
"version": "8.3.0",
"version": "10.0.11",
"commands": [
"dotnet-mapster"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
.NewConfig()
.Map(dest => dest.FullName, src => $"{src.Title} {src.FirstName} {src.LastName}")
.Map(dest => dest.Age,
src => DateTime.Now.Year - src.DateOfBirth.Value.Year,
src => CalculateAge(DateOnly.FromDateTime(src.DateOfBirth!.Value),
DateOnly.FromDateTime(DateTime.UtcNow)),
srcCond => srcCond.DateOfBirth.HasValue)
.Map(dest => dest.FullAddress, src => $"{src.Address.Street} {src.Address.PostCode} - {src.Address.City}");

Check warning on line 19 in dotnet-client-libraries/HowToUseMapster/HowToUseMapster/Config/MapsterConfig.cs

View workflow job for this annotation

GitHub Actions / Build & test (dotnet-client-libraries/HowToUseMapster)

Dereference of a possibly null reference.

TypeAdapterConfig<Person, PersonDto>
.NewConfig()
Expand All @@ -28,5 +29,17 @@

TypeAdapterConfig.GlobalSettings.Scan(Assembly.GetExecutingAssembly());
}

public static int CalculateAge(DateOnly birthDate, DateOnly today)
{
var age = today.Year - birthDate.Year;

if (birthDate > today.AddYears(-age))
{
age--;
}

return age;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mapster" Version="7.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Mapster" Version="10.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.2.3" />
</ItemGroup>

<Target Name="Mapster">
Expand Down
24 changes: 24 additions & 0 deletions dotnet-client-libraries/HowToUseMapster/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using HowToUseMapster;
using HowToUseMapster.Config;
using HowToUseMapster.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -46,5 +48,27 @@ public void WhenMappingPersonDtoToPersonEntity_ThenEntityNotNullAndFirstNameIsEq
Assert.NotNull(entity);
Assert.Equal(_person.FirstName, entity.FirstName);
}

[Fact]
public void GivenBirthdayAlreadyPassedThisYear_WhenCalculateAge_ThenReturnsFullYears()
{
var birthDate = new DateOnly(1990, 1, 1);
var today = new DateOnly(2026, 8, 15);

var age = MapsterConfig.CalculateAge(birthDate, today);

Assert.Equal(36, age);
}

[Fact]
public void GivenLeapDayBirthdayInNonLeapYear_WhenCalculateAgeBeforeMarch_ThenBirthdayNotYetCounted()
{
var birthDate = new DateOnly(2000, 2, 29);
var today = new DateOnly(2025, 2, 28);

var age = MapsterConfig.CalculateAge(birthDate, today);

Assert.Equal(24, age);
}
}
}
11 changes: 6 additions & 5 deletions dotnet-client-libraries/HowToUseMapster/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.9.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading