forked from AutoMapper/AutoMapper
-
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.
Added NameValueCollectionMapper so that NameValueCollections can be m…
…apped. Fixes existing issue where the mappin attempt falls back to an IList, resulting in an InvalidCastException fixes AutoMapper#141
- Loading branch information
Showing
6 changed files
with
156 additions
and
7 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Collections.Specialized; | ||
|
||
namespace AutoMapper.Mappers | ||
{ | ||
public class NameValueCollectionMapper : IObjectMapper | ||
{ | ||
public object Map(ResolutionContext context, IMappingEngineRunner mapper) | ||
{ | ||
if (!IsMatch(context) || context.SourceValue == null) | ||
return null; | ||
|
||
var nvc = new NameValueCollection(); | ||
var source = context.SourceValue as NameValueCollection; | ||
foreach (var s in source.AllKeys) | ||
nvc.Add(s, source[s]); | ||
|
||
return nvc; | ||
} | ||
|
||
public bool IsMatch(ResolutionContext context) | ||
{ | ||
return | ||
context.SourceType == typeof(NameValueCollection) && | ||
context.DestinationType == typeof (NameValueCollection); | ||
} | ||
} | ||
} |
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
103 changes: 103 additions & 0 deletions
103
src/UnitTests/Mappers/NameValueCollectionMapperTests.cs
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,103 @@ | ||
using System; | ||
using System.Collections.Specialized; | ||
using AutoMapper; | ||
using AutoMapper.Mappers; | ||
using NUnit.Framework; | ||
|
||
namespace Automapper.UnitTests.Mappers | ||
{ | ||
[TestFixture] | ||
public class NameValueCollectionMapperTests | ||
{ | ||
[TestFixture] | ||
public class IsMatch | ||
{ | ||
[Test] | ||
public void ReturnsTrueWhenBothSourceAndDestinationTypesAreNameValueCollection() | ||
{ | ||
var rc = new ResolutionContext(null, null, null, typeof(NameValueCollection), typeof(NameValueCollection), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.IsMatch(rc); | ||
|
||
Assert.IsTrue(result); | ||
} | ||
|
||
[Test] | ||
public void ReturnsIsFalseWhenDestinationTypeIsNotNameValueCollection() | ||
{ | ||
var rc = new ResolutionContext(null, null, null, typeof(NameValueCollection), typeof(Object), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.IsMatch(rc); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
|
||
[Test] | ||
public void ReturnsIsFalseWhenSourceTypeIsNotNameValueCollection() | ||
{ | ||
var rc = new ResolutionContext(null, null, null, typeof(Object), typeof(NameValueCollection), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.IsMatch(rc); | ||
|
||
Assert.IsFalse(result); | ||
} | ||
} | ||
|
||
[TestFixture] | ||
public class Map | ||
{ | ||
[Test] | ||
public void ReturnsNullIfSourceTypeIsNotNameValueCollection() | ||
{ | ||
var rc = new ResolutionContext(null, new Object(), new NameValueCollection(), typeof(Object), typeof(NameValueCollection), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.Map(rc, null); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[Test] | ||
public void ReturnsNullIfSourceValueIsNull() | ||
{ | ||
var rc = new ResolutionContext(null, null, new NameValueCollection(), typeof(NameValueCollection), typeof(NameValueCollection), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.Map(rc, null); | ||
|
||
Assert.IsNull(result); | ||
} | ||
|
||
[Test] | ||
public void ReturnsEmptyCollectionWhenSourceCollectionIsEmpty() | ||
{ | ||
var sourceValue = new NameValueCollection(); | ||
var rc = new ResolutionContext(null, sourceValue, new NameValueCollection(), typeof(NameValueCollection), typeof(NameValueCollection), null); | ||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.Map(rc, null) as NameValueCollection; | ||
|
||
Assert.IsEmpty(result); | ||
} | ||
|
||
[Test] | ||
public void ReturnsMappedObjectWithExpectedValuesWhenSourceCollectionHasOneItem() | ||
{ | ||
var sourceValue = new NameValueCollection() { { "foo", "bar" } }; | ||
var rc = new ResolutionContext(null, sourceValue, new NameValueCollection(), typeof(NameValueCollection), typeof(NameValueCollection), null); | ||
|
||
var nvcm = new NameValueCollectionMapper(); | ||
|
||
var result = nvcm.Map(rc, null) as NameValueCollection; | ||
|
||
Assert.AreEqual(1, result.Count); | ||
Assert.AreEqual("foo", result.AllKeys[0]); | ||
Assert.AreEqual("bar", result["foo"]); | ||
} | ||
} | ||
|
||
} | ||
} |
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