forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request MapsterMapper#53 from chaowlert/clone-setting
cloning config
- Loading branch information
Showing
24 changed files
with
323 additions
and
172 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
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
using System; | ||
using System.Linq; | ||
using Mapster.Models; | ||
using NUnit.Framework; | ||
using Should; | ||
|
||
namespace Mapster.Tests | ||
{ | ||
[TestFixture] | ||
public class WhenCloningConfig | ||
{ | ||
[TearDown] | ||
public void TearDown() | ||
{ | ||
TypeAdapterConfig.GlobalSettings.Clear(); | ||
} | ||
|
||
[Test] | ||
public void Alter_Config_After_Map_Should_Error() | ||
{ | ||
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig() | ||
.Map(dest => dest.Name, src => "a"); | ||
|
||
var poco = new SimplePoco | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "test", | ||
}; | ||
var result = TypeAdapter.Adapt<SimpleDto>(poco); | ||
result.Name.ShouldEqual("a"); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => | ||
TypeAdapterConfig<SimplePoco, SimpleDto>.ForType() | ||
.Map(dest => dest.Name, src => "b")); | ||
ex.Message.ShouldContain("TypeAdapter.Adapt was already called"); | ||
} | ||
|
||
[Test] | ||
public void Clone() | ||
{ | ||
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig() | ||
.Map(dest => dest.Name, src => "a"); | ||
|
||
var poco = new SimplePoco | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "test", | ||
}; | ||
var result = TypeAdapter.Adapt<SimpleDto>(poco); | ||
result.Name.ShouldEqual("a"); | ||
|
||
var config = TypeAdapterConfig.GlobalSettings.Clone(); | ||
var global = TypeAdapterConfig.GlobalSettings; | ||
config.ShouldNotBeSameAs(global); | ||
config.Default.ShouldNotBeSameAs(global.Default); | ||
config.Default.Settings.ShouldNotBeSameAs(global.Default.Settings); | ||
config.RuleMap.ShouldNotBeSameAs(global.RuleMap); | ||
foreach (var kvp in config.RuleMap) | ||
{ | ||
var globalRule = global.RuleMap[kvp.Key]; | ||
kvp.Value.ShouldNotBeSameAs(globalRule); | ||
kvp.Value.Settings.ShouldNotBeSameAs(globalRule.Settings); | ||
} | ||
config.Rules.ShouldNotBeSameAs(global.Rules); | ||
for (var i = 0; i < config.Rules.Count; i++) | ||
{ | ||
config.Rules[i].ShouldNotBeSameAs(global.Rules[i]); | ||
config.Rules[i].Settings.ShouldNotBeSameAs(global.Rules[i].Settings); | ||
} | ||
config.Rules.Any(rule => object.ReferenceEquals(rule.Settings, config.Default.Settings)).ShouldBeTrue(); | ||
config.Rules.ShouldContain(config.RuleMap[new TypeTuple(typeof(SimplePoco), typeof(SimpleDto))]); | ||
} | ||
|
||
public class SimplePoco | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
|
||
public class SimpleDto | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; protected set; } | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.