Skip to content

Commit

Permalink
Added multi mapping to the mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Renaud committed Dec 7, 2021
1 parent 2413810 commit 629248d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Mapr/IMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Mapr
using System.Collections.Generic;

namespace Mapr
{
/// <summary>
/// Represents the interface for a Mapper.
Expand All @@ -14,5 +16,14 @@ public interface IMapper
/// <typeparam name="TDestination">The destination object type.</typeparam>
/// <returns>A instance of <typeparamref name="TDestination"/> that represents a mapped type.</returns>
public TDestination Map<TSource, TDestination>(TSource source);

/// <summary>
/// Map from a collection of <typeparamref name="TSource"/> to a collection of <typeparamref name="TDestination"/>
/// </summary>
/// <param name="source">The source collection.</param>
/// <typeparam name="TSource">The source type</typeparam>
/// <typeparam name="TDestination">The destination type</typeparam>
/// <returns>An <see cref="IEnumerable{T}"/> of the mapped values.</returns>
public IEnumerable<TDestination> Map<TSource, TDestination>(IEnumerable<TSource> source);
}
}
17 changes: 16 additions & 1 deletion src/Mapr/Mapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Mapr
using System.Collections.Generic;
using System.Linq;

namespace Mapr
{
/// <summary>
/// Represents an implementation of <see cref="IMapper"/>
Expand Down Expand Up @@ -36,5 +39,17 @@ public TDestination Map<TSource, TDestination>(TSource source)
{
return _locator.LocateMapFor<TSource, TDestination>().Map(source);
}

/// <summary>
/// Maps the provided collection of <paramref name="source"/> objects to the provided type <typeparamref name="TDestination"/>.
/// </summary>
/// <param name="source">The objects that will be mapped</param>
/// <typeparam name="TSource">The type of the source objects.</typeparam>
/// <typeparam name="TDestination">The type of the destination objects.</typeparam>
/// <returns>A collection of objects mapped.</returns>
public IEnumerable<TDestination> Map<TSource, TDestination>(IEnumerable<TSource> source)
{
return source.Select(Map<TSource, TDestination>);
}
}
}
22 changes: 21 additions & 1 deletion tests/Mapr.Tests/MapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using AutoFixture;
using FluentAssertions;
using NSubstitute;
Expand Down Expand Up @@ -34,9 +35,28 @@ public void Map_ShouldNotThrow_WhenPassedNull()

var mapper = new Mapper(locator);

Action act = () => mapper.Map<string?, string?>(null);
Action act = () => mapper.Map<string?, string?>((string)null!);

act.Should().NotThrow<ArgumentNullException>();
}

[Fact]
public void Map_WithCollection_ShouldMap_WhenTypeMapExists()
{
var typeMap = Substitute.For<IMap<int, string>>();
var locator = Substitute.For<IMapLocator>();
locator.LocateMapFor<int, string>().Returns(typeMap);

var mapper = new Mapper(locator);

var source = Fixture.CreateMany<int>().ToList();
var expectedResult = source.Select(s => s.ToString()).ToList();

typeMap.Map(Arg.Any<int>()).Returns(c => c.Arg<int>().ToString());

var result = mapper.Map<int, string>(source);

result.Should().BeEquivalentTo(expectedResult);
}
}
}

0 comments on commit 629248d

Please sign in to comment.