Skip to content

Commit

Permalink
Map a object to another object. Can be used to make simpler snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswalpen committed Jan 22, 2024
1 parent 288e446 commit bb45bd8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Polaroider/ObjectExtensions.cs
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);
}
}
}
32 changes: 32 additions & 0 deletions src/Tests/Polaroider.Tests/ObjectExtensionsTests.cs
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"
});
}
}
}

0 comments on commit bb45bd8

Please sign in to comment.