Created
May 3, 2016 22:53
-
-
Save leekelleher/0b1a9b9a0623e997031b8ac65f19d1c5 to your computer and use it in GitHub Desktop.
Ditto - unit-test to explore how mapping polymorphic collections might work
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using NUnit.Framework; | |
namespace Our.Umbraco.Ditto.Tests | |
{ | |
[TestFixture] | |
public class PolymorphicCollectionMappingTests | |
{ | |
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] | |
public sealed class DittoDocTypeAttribute : Attribute | |
{ | |
public DittoDocTypeAttribute(string alias = null) | |
{ } | |
} | |
public interface IModel | |
{ } | |
[DittoDocType] | |
public class MyModel : IModel | |
{ } | |
[DittoDocType("myDocType2")] | |
public class MyModel2 : IModel | |
{ } | |
[Test] | |
public void Polymorphic_Collection_Maps() | |
{ | |
var nodes = new List<Mocks.PublishedContentMock>() | |
{ | |
new Mocks.PublishedContentMock { Id = 1111, DocumentTypeAlias = "myDocType1" }, | |
new Mocks.PublishedContentMock { Id = 2222, DocumentTypeAlias = "myDocType2" }, | |
new Mocks.PublishedContentMock { Id = 3333, DocumentTypeAlias = "myDocType3" }, | |
}; | |
var items = nodes.As<IEnumerable<IModel>>(); | |
Assert.That(items, Is.Not.Null); | |
Assert.That(items.Count(), Is.EqualTo(3)); | |
CollectionAssert.AllItemsAreInstancesOfType(items, typeof(IModel)); | |
Assert.That(items.First(), Is.TypeOf<MyModel>()); | |
Assert.That(items.Skip(1).First(), Is.TypeOf<MyModel2>()); | |
Assert.That(items.Skip(2).First(), Is.TypeOf<MyModel>()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment