Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Use [Vortex](https://github.com/equilaterus/Vortex) side-by-side with **AutoMapp
using Equilaterus.Vortex.AutoMapper.Extensions;
...

// Returns IMaybe<DestinationType>
// Returns Maybe<DestinationType>
var maybeResult = IMapper.MaybeMap<DestinationType>(sourceEntity);
```

Expand Down
12 changes: 6 additions & 6 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
version: 0.1.0.{build}
version: 0.1.1.{build}
image: Visual Studio 2019
dotnet_csproj:
patch: true
file: '**\*.csproj'
version: '0.1.0'
package_version: '0.1.0-alpha'
assembly_version: '0.1.0'
file_version: '0.1.0'
informational_version: '0.1.0'
version: '0.1.1'
package_version: '0.1.1-alpha'
assembly_version: '0.1.1'
file_version: '0.1.1'
informational_version: '0.1.1'
before_build:
- cmd: nuget restore
build:
Expand Down
24 changes: 24 additions & 0 deletions src/MapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,34 @@ public static Maybe<TDestination> MaybeMap<TDestination>(
return new Maybe<TDestination>(mapper.Map<TDestination>(source));
}

public static Maybe<TDestination> MaybeMap<TDestination>(
this IMapper mapper, object source, Action<IMappingOperationOptions> opts)
{
return new Maybe<TDestination>(mapper.Map<TDestination>(source, opts));
}

public static Maybe<TDestination> MaybeMap<TSource, TDestination>(
this IMapper mapper, TSource source)
{
return new Maybe<TDestination>(mapper.Map<TSource, TDestination>(source));
}

public static Maybe<TDestination> MaybeMap<TSource, TDestination>(
this IMapper mapper, TSource source, Action<IMappingOperationOptions<TSource, TDestination>> opts)
{
return new Maybe<TDestination>(mapper.Map<TSource, TDestination>(source, opts));
}

public static Maybe<TDestination> MaybeMap<TSource, TDestination>(
this IMapper mapper, TSource source, TDestination destination)
{
return new Maybe<TDestination>(mapper.Map(source, destination));
}

public static Maybe<TDestination> MaybeMap<TSource, TDestination>(
this IMapper mapper, TSource source, TDestination destination, Action<IMappingOperationOptions<TSource, TDestination>> opts)
{
return new Maybe<TDestination>(mapper.Map(source, destination, opts));
}
}
}
138 changes: 127 additions & 11 deletions test/MapperExtensions.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,169 @@ public class MapperExtensions
[Fact]
public void MaybeMap_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<double>(1))
.Returns(1.2);

var objectMock = mapperMock.Object;
var result = objectMock.MaybeMap<double>(1);
// Execute
var result = mapperMock.Object.MaybeMap<double>(1);

// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMap_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<object>(It.IsAny<object>()))
mapperMock.Setup(x => x.Map<object>(null))
.Returns(null);

var objectMock = mapperMock.Object;
// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<object>(null));
}

[Fact]
public void MaybeMapOpts_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<double>(1, null))
.Returns(1.2);

// Execute
var result = mapperMock.Object.MaybeMap<double>(1, null);

// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMapOpts_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<object>(null, null))
.Returns(null);

// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<object>(null, null));
}

[Fact]
public void MaybeMapDestination_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<int, double>(1))
.Returns(1.2);

// Execute
var result = mapperMock.Object.MaybeMap<int, double>(1);

// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMapDestination_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<string, object>(null))
.Returns(null);

Assert.Throws<ArgumentNullException>(() => objectMock.MaybeMap<object>(new object()));
// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<string, object>(null));
}

[Fact]
public void MaybeMapDestinationOpt_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<int, double>(1, null))
.Returns(1.2);

// Execute
var result = mapperMock.Object.MaybeMap<int, double>(1, null);

// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMapDestinationOpt_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<string, object>(null, null))
.Returns(null);

// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<string, object>(null, null));
}

[Fact]
public void MaybeMap_SourceDestination_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map(It.IsAny<object>(), It.IsAny<double>()))
mapperMock.Setup(x => x.Map("1", 0.2))
.Returns(1.2);

var objectMock = mapperMock.Object;
var result = objectMock.MaybeMap(new object(), new double());
// Execute
var result = mapperMock.Object.MaybeMap("1", 0.2);

// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMap_SourceDestination_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map(It.IsAny<object>(), It.IsAny<object>()))
mapperMock.Setup(x => x.Map<object, object>(null, null))
.Returns(null);

// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<object, object>(null, new object()));
}

[Fact]
public void MaybeMapOpt_SourceDestination_Success()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map("1", 0.2, null))
.Returns(1.2);

// Execute
var result = mapperMock.Object.MaybeMap("1", 0.2, null);

var objectMock = mapperMock.Object;
// Verify
Assert.IsType<Maybe<double>>(result);
Assert.Equal(1.2, result.Match(x => x, 0));
}

[Fact]
public void MaybeMapOpt_SourceDestination_Null_ThrowsException()
{
// Prepare
var mapperMock = new Mock<IMapper>();
mapperMock.Setup(x => x.Map<object, object>(null, null, null))
.Returns(null);

Assert.Throws<ArgumentNullException>(() => objectMock.MaybeMap(new object(), new object()));
// Execute
Assert.Throws<ArgumentNullException>(() => mapperMock.Object.MaybeMap<object, object>(null, new object(), null));
}
}
}
Loading