1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using FluentAssertions ;
5+ using NetChris . Core . Paging ;
6+ using Xunit ;
7+
8+ namespace NetChris . Core . UnitTests . Paging ;
9+
10+ public class MapToTests
11+ {
12+ private class FromType
13+ {
14+ public string Name { get ; set ; } = null ! ;
15+ public int Age { get ; set ; }
16+ }
17+
18+ private class ToType
19+ {
20+ public string PersonName { get ; set ; } = null ! ;
21+ public TimeSpan Age { get ; set ; }
22+ }
23+
24+
25+ private readonly List < FromType > _fromTypes = new ( )
26+ {
27+ new FromType { Name = "Alice" , Age = 30 } ,
28+ new FromType { Name = "Bob" , Age = 25 } ,
29+ new FromType { Name = "Charlie" , Age = 35 } ,
30+ new FromType { Name = "Diana" , Age = 28 }
31+ } ;
32+
33+
34+ private readonly Page < FromType > _sourcePage ;
35+ private readonly Page < ToType > _targetPage ;
36+
37+ private static ToType MappingFunction ( FromType from )
38+ {
39+ return new ToType
40+ {
41+ PersonName = from . Name ,
42+ Age = TimeSpan . FromDays ( from . Age * 365.25 ) ,
43+ } ;
44+ }
45+
46+ public MapToTests ( )
47+ {
48+ _sourcePage = new Page < FromType > ( _fromTypes , 2 , 4 , 500 ) ;
49+ _targetPage = _sourcePage . MapTo ( MappingFunction ) ;
50+ }
51+
52+ [ Fact ]
53+ public void TotalPagesShouldBeCorrect ( )
54+ {
55+ _targetPage . TotalPages . Should ( ) . Be ( _sourcePage . TotalPages ) ;
56+ }
57+
58+ [ Fact ]
59+ public void CurrentPageShouldBeCorrect ( )
60+ {
61+ _targetPage . CurrentPage . Should ( ) . Be ( _sourcePage . CurrentPage ) ;
62+ }
63+
64+ [ Fact ]
65+ public void PageSizeShouldBeCorrect ( )
66+ {
67+ _targetPage . PageSize . Should ( ) . Be ( _sourcePage . PageSize ) ;
68+ }
69+
70+ [ Fact ]
71+ public void MappedItemShouldMatch ( )
72+ {
73+ var charlieFromSourcePage = _sourcePage . Items . ElementAt ( 2 ) ;
74+ var charlieFromTargetPage = _targetPage . Items . ElementAt ( 2 ) ;
75+
76+ charlieFromTargetPage . PersonName . Should ( ) . Be ( charlieFromSourcePage . Name ) ;
77+ charlieFromTargetPage . Age . Should ( ) . Be (
78+ TimeSpan . FromDays ( charlieFromSourcePage . Age * 365.25 ) ) ;
79+ }
80+ }
0 commit comments