-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add support to specify ShouldMapMethod #2960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
88fb3e0
Add support to specify ShouldMapMethod
bfcamara 7c57825
Fix duplicate assertion
bfcamara 7d23423
CR Feedback: Apply filter ShouldMapMethod to the input param when cal…
bfcamara d95d80d
CR Feedback: Add unittest to to ShouldMapMethod in case of extension …
bfcamara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
using Xunit; | ||
using Shouldly; | ||
using System; | ||
|
||
namespace AutoMapper.UnitTests | ||
{ | ||
public class ShouldMapMethodInstanceMethods : NonValidatingSpecBase | ||
{ | ||
public int SomeValue = 2354; | ||
public int AnotherValue = 6798; | ||
|
||
private Destination _destination; | ||
|
||
class Source | ||
{ | ||
private int _someValue; | ||
private int _anotherValue; | ||
|
||
public Source(int someValue, int anotherValue) | ||
{ | ||
_someValue = someValue; | ||
anotherValue = _anotherValue; | ||
} | ||
|
||
public int SomeNumber() | ||
{ | ||
return _someValue; | ||
} | ||
|
||
public int AnotherNumber() { | ||
return _anotherValue; | ||
} | ||
} | ||
|
||
class Destination | ||
{ | ||
public int SomeNumber { get; set; } | ||
public int AnotherNumber { get; set; } | ||
} | ||
|
||
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => | ||
{ | ||
cfg.ShouldMapMethod = (m => m.Name != nameof(Source.AnotherNumber)); | ||
cfg.CreateMap<Source, Destination>(); | ||
}); | ||
|
||
protected override void Because_of() | ||
{ | ||
_destination = Mapper.Map<Source, Destination>(new Source(SomeValue, AnotherValue)); | ||
} | ||
|
||
[Fact] | ||
public void Should_report_unmapped_property() | ||
{ | ||
new Action(() => Configuration.AssertConfigurationIsValid()) | ||
.ShouldThrowException<AutoMapperConfigurationException>(ex => | ||
{ | ||
ex.Errors.ShouldNotBeNull(); | ||
ex.Errors.ShouldNotBeEmpty(); | ||
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeNull(); | ||
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeEmpty(); | ||
ex.Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Destination.AnotherNumber)); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_not_map_another_number_method() | ||
{ | ||
_destination.SomeNumber.ShouldBe(SomeValue); | ||
_destination.AnotherNumber.ShouldNotBe(AnotherValue); | ||
} | ||
} | ||
|
||
|
||
static class SourceExtensions | ||
{ | ||
public static int SomeNumber(this ShouldMapMethodExtensionMethods.Source source) | ||
{ | ||
return source.SomeValue; | ||
} | ||
|
||
public static int AnotherNumber(this ShouldMapMethodExtensionMethods.Source source) | ||
{ | ||
return source.AnotherValue; | ||
} | ||
} | ||
|
||
public class ShouldMapMethodExtensionMethods : NonValidatingSpecBase | ||
{ | ||
public int SomeValue = 4698; | ||
public int AnotherValue = 2374; | ||
|
||
private Destination _destination; | ||
|
||
public class Source | ||
{ | ||
public int SomeValue { get; set; } | ||
public int AnotherValue { get; set; } | ||
} | ||
|
||
public class Destination | ||
{ | ||
public int SomeNumber { get; set; } | ||
public int AnotherNumber { get; set; } | ||
} | ||
|
||
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => | ||
{ | ||
cfg.IncludeSourceExtensionMethods(typeof(SourceExtensions)); | ||
cfg.ShouldMapMethod = (m => m.Name != nameof(SourceExtensions.AnotherNumber)); | ||
cfg.CreateMap<Source, Destination>(); | ||
}); | ||
|
||
protected override void Because_of() | ||
{ | ||
_destination = Mapper.Map<Source, Destination>(new Source { SomeValue = SomeValue, AnotherValue = AnotherValue }); | ||
} | ||
|
||
[Fact] | ||
public void Should_report_unmapped_property() | ||
{ | ||
new Action(() => Configuration.AssertConfigurationIsValid()) | ||
.ShouldThrowException<AutoMapperConfigurationException>(ex => | ||
{ | ||
ex.Errors.ShouldNotBeNull(); | ||
ex.Errors.ShouldNotBeEmpty(); | ||
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeNull(); | ||
ex.Errors[0].UnmappedPropertyNames.ShouldNotBeEmpty(); | ||
ex.Errors[0].UnmappedPropertyNames[0].ShouldBe(nameof(Destination.AnotherNumber)); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_not_map_another_number_method() | ||
{ | ||
_destination.SomeNumber.ShouldBe(SomeValue); | ||
_destination.AnotherNumber.ShouldNotBe(AnotherValue); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, but the extension method case needs a test too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'm gonna add it.