Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace AgileObjects.AgileMapper.UnitTests.MoreTestClasses
{
using System.Collections.Generic;
using Configuration;

public class ServiceDictionaryMapperConfiguration : MapperConfiguration
{
protected override void Configure()
{
var mappersByName = GetServiceOrNull<Dictionary<string, IMapper>>();

var mapper1 = CreateNewMapper();

mapper1.WhenMapping
.From<Dog>()
.To<Dog>()
.Map((s, t) => "BARK 1!")
.To(t => t.WoofSound);

mappersByName.Add("DogMapper1", mapper1);

var mapper2 = CreateNewMapper();

mapper2.WhenMapping
.From<Dog>()
.To<Dog>()
.Map((s, t) => "BARK 2!")
.To(t => t.WoofSound);

mappersByName.Add("DogMapper2", mapper2);
}

public static bool VerifyConfigured(Dictionary<string, IMapper> mappersByName)
{
if (!mappersByName.ContainsKey("DogMapper1") || !mappersByName.ContainsKey("DogMapper2"))
{
return false;
}

var result1 = mappersByName["DogMapper1"].DeepClone(new Dog());

if (result1.WoofSound != "BARK 1!")
{
return false;
}

var result2 = mappersByName["DogMapper2"].DeepClone(new Dog());

if (result2.WoofSound != "BARK 2!")
{
return false;
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="Configuration\Inline\WhenConfiguringDataSourcesInline.cs" />
<Compile Include="Configuration\WhenApplyingMapperConfigurations.cs" />
<Compile Include="Configuration\WhenConfiguringDataSources.cs" />
<Compile Include="Configuration\WhenConfiguringDerivedTypes.cs" />
<Compile Include="Configuration\WhenConfiguringMappingCallbacks.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace AgileObjects.AgileMapper.UnitTests.NonParallel.Configuration
{
using System.Collections.Generic;
using MoreTestClasses;
using Xunit;
using UnitTestsMapperConfigurations = UnitTests.Configuration.WhenApplyingMapperConfigurations;

public class WhenApplyingMapperConfigurations : NonParallelTestsBase
{
[Fact]
public void ShouldApplyMapperConfigurationsInGivenAssembliesViaTheStaticApi()
{
TestThenReset(() =>
{
var mappersByName = new Dictionary<string, IMapper>();

Mapper.WhenMapping
.UseConfigurations
.FromAssemblyOf<UnitTestsMapperConfigurations>()
.FromAssemblyOf<AnimalBase>(mappersByName);

UnitTestsMapperConfigurations.PfiToPfsMapperConfiguration.VerifyConfigured(Mapper.Default);
UnitTestsMapperConfigurations.PfsToPfiMapperConfiguration.VerifyConfigured(Mapper.Default);

ServiceDictionaryMapperConfiguration
.VerifyConfigured(mappersByName)
.ShouldBeTrue();
});
}

[Fact]
public void ShouldProvideRegisteredServicesToMapperConfigurationsViaTheStaticApi()
{
var mappersByName = new Dictionary<string, IMapper>();

TestThenReset(() =>
{
Mapper.WhenMapping
.UseConfigurations.From<ServiceDictionaryMapperConfiguration>(mappersByName);

ServiceDictionaryMapperConfiguration
.VerifyConfigured(mappersByName)
.ShouldBeTrue();
});
}
}
}
1 change: 1 addition & 0 deletions AgileMapper.UnitTests.NonParallel/NonParallelTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ protected void TestThenReset(Action testAction)
finally
{
Mapper.ResetDefaultInstance();
GlobalContext.Instance.DerivedTypes.Reset();
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions AgileMapper.UnitTests/AgileMapper.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>DEBUG;TRACE;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
Expand All @@ -35,7 +35,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
Expand Down Expand Up @@ -85,6 +85,7 @@
<Compile Include="..\VersionInfo.cs">
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="Configuration\AssemblyScanningTestClassBase.cs" />
<Compile Include="Configuration\Inline\InlineMappingExtensions.cs" />
<Compile Include="Configuration\Inline\WhenConfiguringCallbacksInline.cs" />
<Compile Include="Configuration\Inline\WhenConfiguringConstructorDataSourcesInline.cs" />
Expand All @@ -102,6 +103,8 @@
<Compile Include="Configuration\Inline\WhenMappingToNullInline.cs" />
<Compile Include="Configuration\Inline\WhenValidatingMappingsInline.cs" />
<Compile Include="Configuration\Inline\WhenViewingMappingPlans.cs" />
<Compile Include="Configuration\WhenApplyingMapperConfigurations.cs" />
<Compile Include="Configuration\WhenApplyingMapperConfigurationsIncorrectly.cs" />
<Compile Include="Configuration\WhenViewingMappingPlans.cs" />
<Compile Include="Dictionaries\WhenMappingFromDictionariesOnToComplexTypes.cs" />
<Compile Include="Dictionaries\WhenMappingFromDictionariesOnToEnumerableMembers.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace AgileObjects.AgileMapper.UnitTests.Configuration
{
using System;

public abstract class AssemblyScanningTestClassBase
{
protected void TestThenReset(Action testAction)
{
try
{
testAction.Invoke();
}
finally
{
GlobalContext.Instance.DerivedTypes.Reset();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
namespace AgileObjects.AgileMapper.UnitTests.Configuration
{
#if NET40
using System;
#endif
using System.Collections.Generic;
using AgileMapper.Configuration;
using MoreTestClasses;
using NetStandardPolyfills;
using TestClasses;
using Xunit;

public class WhenApplyingMapperConfigurations : AssemblyScanningTestClassBase
{
[Fact]
public void ShouldApplyAGivenMapperConfiguration()
{
TestThenReset(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.UseConfigurations.From<PfiToPfsMapperConfiguration>();

PfiToPfsMapperConfiguration.VerifyConfigured(mapper);
}
});
}

[Fact]
public void ShouldApplyMapperConfigurationsInAGivenTypeAssembly()
{
TestThenReset(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping
.UseConfigurations.FromAssemblyOf<WhenApplyingMapperConfigurations>();

PfiToPfsMapperConfiguration.VerifyConfigured(mapper);
PfsToPfiMapperConfiguration.VerifyConfigured(mapper);
}
});
}

[Fact]
public void ShouldProvideRegisteredServicesToMapperConfigurations()
{
TestThenReset(() =>
{
using (var mapper = Mapper.CreateNew())
{
var mappersByName = new Dictionary<string, IMapper>();

mapper.WhenMapping
.UseConfigurations.FromAssemblyOf<AnimalBase>(mappersByName);

ServiceDictionaryMapperConfiguration
.VerifyConfigured(mappersByName)
.ShouldBeTrue();
}
});
}

#if NET40
[Fact]
public void ShouldApplyMapperConfigurationsFromGivenAssemblies()
{
TestThenReset(() =>
{
using (var mapper = Mapper.CreateNew())
{
var mappersByName = new Dictionary<string, IMapper>();

mapper.WhenMapping
.UseConfigurations.From(AppDomain.CurrentDomain.GetAssemblies(), mappersByName);

PfiToPfsMapperConfiguration.VerifyConfigured(mapper);
PfsToPfiMapperConfiguration.VerifyConfigured(mapper);

ServiceDictionaryMapperConfiguration
.VerifyConfigured(mappersByName)
.ShouldBeTrue();
}
});
}
#endif

[Fact]
public void ShouldFilterMapperConfigurationsFromGivenAssemblies()
{
TestThenReset(() =>
{
using (var mapper = Mapper.CreateNew())
{
mapper.WhenMapping.UseConfigurations.From(
new[]
{
typeof(PfiToPfsMapperConfiguration).GetAssembly(),
typeof(ServiceDictionaryMapperConfiguration).GetAssembly()
},
assembly => !assembly.FullName.Contains(nameof(MoreTestClasses)));

PfiToPfsMapperConfiguration.VerifyConfigured(mapper);
PfsToPfiMapperConfiguration.VerifyConfigured(mapper);
}
});
}

#region Helper Classes

public class PfiToPfsMapperConfiguration : MapperConfiguration
{
protected override void Configure()
{
WhenMapping
.From<PublicField<int>>()
.ToANew<PublicField<string>>()
.Map(ctx => ctx.Source.Value * 2)
.To(t => t.Value);

GetPlanFor<PublicField<int>>().ToANew<PublicField<string>>();
}

public static void VerifyConfigured(IMapper mapper)
{
var source = new PublicField<int> { Value = 123 };
var result = mapper.Map(source).ToANew<PublicField<string>>();

result.Value.ShouldBe(246);
}
}

// ReSharper disable once ClassNeverInstantiated.Local
public class PfsToPfiMapperConfiguration : MapperConfiguration
{
protected override void Configure()
{
WhenMapping
.From<PublicField<string>>()
.ToANew<PublicField<int>>()
.Map(ctx => ctx.Source.Value + "10")
.To(t => t.Value);

GetPlansFor<PublicField<string>>().To<PublicField<int>>();
}

public static void VerifyConfigured(IMapper mapper)
{
var source = new PublicField<string> { Value = "10" };
var result = mapper.Map(source).ToANew<PublicField<int>>();

result.Value.ShouldBe(1010);
}
}

#endregion
}
}
Loading