Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leekelleher/0b1a9b9a0623e997031b8ac65f19d1c5 to your computer and use it in GitHub Desktop.
Save leekelleher/0b1a9b9a0623e997031b8ac65f19d1c5 to your computer and use it in GitHub Desktop.
Ditto - unit-test to explore how mapping polymorphic collections might work
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