-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Map a object to another object. Can be used to make simpler snapshots
- Loading branch information
1 parent
288e446
commit bb45bd8
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace Polaroider | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class ObjectExtensions | ||
{ | ||
/// <summary> | ||
/// Map a object to another object. Can be used to make simpler snapshots | ||
/// </summary> | ||
/// <typeparam name="Tout"></typeparam> | ||
/// <typeparam name="Tin"></typeparam> | ||
/// <param name="obj"></param> | ||
/// <param name="factory"></param> | ||
/// <returns></returns> | ||
public static Tout MapTo<Tout, Tin>(this Tin obj, Func<Tin, Tout> factory) | ||
{ | ||
return factory(obj); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace Polaroider.Tests | ||
{ | ||
public class ObjectExtensionsTests | ||
{ | ||
[Test] | ||
public void ObjectExtensions_MapTo() | ||
{ | ||
var obj = new | ||
{ | ||
Age = 25, | ||
Name = "John", | ||
LastName = "Doe" | ||
}; | ||
|
||
obj.MapTo(o => | ||
new | ||
{ | ||
Name = o.Name, | ||
LastName = o.LastName | ||
}) | ||
.Should() | ||
.BeEquivalentTo(new | ||
{ | ||
Name = "John", | ||
LastName = "Doe" | ||
}); | ||
} | ||
} | ||
} |