This commit is contained in:
José Manuel Nieto Sánchez 2016-04-17 23:17:08 +02:00
Родитель 2d2de4689e
Коммит d582b31434
3 изменённых файлов: 373 добавлений и 374 удалений

Просмотреть файл

@ -1,6 +1,9 @@
namespace OmniXaml.Tests.Common
{
using System.Collections.Generic;
using Builder;
using ObjectAssembler;
using TypeConversion;
using Typing;
public class GivenARuntimeTypeSource
@ -18,5 +21,12 @@ namespace OmniXaml.Tests.Common
public NamespaceDeclaration SpecialNs { get; } = new NamespaceDeclaration(CoreTypes.SpecialNamespace, "x");
public XamlInstructionBuilder X { get; }
public ProtoInstructionBuilder P { get; }
protected IObjectAssembler CreateObjectAssembler()
{
var topDownValueContext = new TopDownValueContext();
var objectAssembler = new ObjectAssembler(RuntimeTypeSource, new ValueContext(RuntimeTypeSource, topDownValueContext, new Dictionary<string, object>()));
return objectAssembler;
}
}
}

Просмотреть файл

@ -6,40 +6,363 @@
using Classes;
using Classes.WpfLikeModel;
using Common;
using Xunit;
using ObjectAssembler;
using Resources;
using TypeConversion;
using Xunit;
public class ObjectAssemblerTests : GivenARuntimeTypeSource
{
private InstructionResources source;
private IObjectAssembler sut;
public ObjectAssemblerTests()
{
source = new InstructionResources(this);
sut = CreateSut();
}
private readonly InstructionResources source;
private readonly IObjectAssembler sut;
private ObjectAssembler CreateSut()
{
var topDownValueContext = new TopDownValueContext();
var valueConnectionContext = new ValueContext(RuntimeTypeSource, topDownValueContext, new Dictionary<string, object>());
return new ObjectAssembler(RuntimeTypeSource, valueConnectionContext, new Settings { InstanceLifeCycleListener = new TestListener() });
return new ObjectAssembler(RuntimeTypeSource, valueConnectionContext, new Settings {InstanceLifeCycleListener = new TestListener()});
}
public IObjectAssembler CreateSutForLoadingSpecificInstance(object instance)
private IObjectAssembler CreateSutForLoadingSpecificInstance(object instance)
{
var topDownValueContext = new TopDownValueContext();
var valueConnectionContext = new ValueContext(RuntimeTypeSource, topDownValueContext, new Dictionary<string, object>());
var settings = new Settings { RootInstance = instance };
var settings = new Settings {RootInstance = instance};
var assembler = new ObjectAssembler(RuntimeTypeSource, valueConnectionContext, settings);
return assembler;
}
[Fact]
public void AttachableMemberThatIsCollection()
{
sut.Process(source.AttachableMemberThatIsCollection);
var instance = sut.Result;
var col = Container.GetCollection(instance);
Assert.NotEmpty(col);
}
[Fact]
public void AttemptToAssignItemsToNonCollectionMember()
{
Assert.Throws<ParseException>(() => sut.Process(source.AttemptToAssignItemsToNonCollectionMember));
}
[Fact]
public void ChildIsAssociatedBeforeItsPropertiesAreSet()
{
sut.Process(source.InstanceWithChildAndProperty);
var result = (DummyClass) sut.Result;
Assert.False(result.TitleWasSetBeforeBeingAssociated);
}
[Fact]
public void CollectionWithInnerCollection()
{
sut.Process(source.CollectionWithInnerCollection);
var result = sut.Result;
var children = ((DummyClass) result).Items;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(3, children.Count);
Assert.All(children, child => Assert.IsType(typeof(Item), child));
var innerCollection = children[0].Children;
Assert.Equal(2, innerCollection.Count);
Assert.All(innerCollection, child => Assert.IsType(typeof(Item), child));
}
[Fact]
public void CorrectInstanceSetupSequence()
{
var expectedSequence = new[] {SetupSequence.Begin, SetupSequence.AfterAssociatedToParent, SetupSequence.AfterSetProperties, SetupSequence.End};
sut.Process(source.InstanceWithChild);
var listener = (TestListener) sut.LifecycleListener;
Assert.Equal(expectedSequence.ToList().AsReadOnly(), listener.InvocationOrder);
}
[Fact]
public void CustomCollection()
{
sut.Process(source.CustomCollection);
Assert.NotEmpty((IEnumerable) sut.Result);
}
[Fact]
public void DirectContentForOneToMany()
{
sut.Process(source.DirectContentForOneToMany);
}
[Fact]
public void ExpandedAttachablePropertyAndItemBelow()
{
sut.Process(source.ExpandedAttachablePropertyAndItemBelow);
var items = ((DummyClass) sut.Result).Items;
var firstChild = items.First();
var attachedProperty = Container.GetProperty(firstChild);
Assert.Equal(2, items.Count);
Assert.Equal("Value", attachedProperty);
}
[Fact]
public void ExplicitCollection_ShouldHaveItems()
{
sut.Process(source.ExplicitCollection);
var actual = (RootObject) sut.Result;
var customCollection = actual.Collection;
Assert.NotEmpty(customCollection);
}
[Fact]
public void ExplicitCollection_ShouldReplaceCollectionInstance()
{
sut.Process(source.ExplicitCollection);
var actual = (RootObject) sut.Result;
Assert.True(actual.CollectionWasReplaced);
}
[Fact]
public void ExtensionThatReturnsNull()
{
sut.Process(source.ExtensionThatReturnsNull);
var result = sut.Result;
var property = ((DummyClass) result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Null(property);
}
[Fact]
public void ExtensionWithArgument()
{
sut.Process(source.ExtensionWithArgument);
var result = sut.Result;
var property = ((DummyClass) result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Option", property);
}
[Fact]
public void ExtensionWithNonStringArgument()
{
sut.Process(source.ExtensionWithNonStringArgument);
var result = sut.Result;
var property = ((DummyClass) result).Number;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(123, property);
}
[Fact]
public void ExtensionWithTwoArguments()
{
sut.Process(source.ExtensionWithTwoArguments);
var result = sut.Result;
var property = ((DummyClass) result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("OneSecond", property);
}
[Fact]
public void ImplicitCollection_ShouldHaveItems()
{
sut.Process(source.ImplicitCollection);
var actual = (RootObject) sut.Result;
var customCollection = actual.Collection;
Assert.NotEmpty(customCollection);
}
[Fact]
public void ImplicitCollection_ShouldKeepSameCollectionInstance()
{
sut.Process(source.ImplicitCollection);
var actual = (RootObject) sut.Result;
Assert.False(actual.CollectionWasReplaced);
}
[Fact]
public void KeyDirective()
{
sut.Process(source.KeyDirective);
var actual = sut.Result;
Assert.IsType(typeof(DummyClass), actual);
var dictionary = (IDictionary) ((DummyClass) actual).Resources;
Assert.True(dictionary.Count > 0);
}
[Fact]
public void ListBoxWithItemAndTextBlockNoNames()
{
sut.Process(source.ListBoxWithItemAndTextBlockNoNames);
var w = (Window) sut.Result;
var lb = (ListBox) w.Content;
var lvi = (ListBoxItem) lb.Items.First();
var tb = lvi.Content;
Assert.IsType(typeof(TextBlock), tb);
}
[Fact]
public void ListBoxWithItemAndTextBlockWithNames_HaveCorrectNames()
{
sut.Process(source.ListBoxWithItemAndTextBlockWithNames);
var w = (Window) sut.Result;
var lb = (ListBox) w.Content;
var lvi = (ListBoxItem) lb.Items.First();
var tb = (TextBlock) lvi.Content;
Assert.Equal("MyListBox", lb.Name);
Assert.Equal("MyListBoxItem", lvi.Name);
Assert.Equal("MyTextBlock", tb.Name);
}
[Fact]
public void MemberAfterInitalizationValue()
{
sut.Process(source.MemberAfterInitalizationValue);
var root = (RootObject) sut.Result;
var str = root.Collection[0];
var dummy = (DummyClass) root.Collection[1];
Assert.Equal("foo", str);
Assert.Equal(123, dummy.Number);
}
[Fact]
public void MemberWithIncompatibleTypes()
{
sut.Process(source.MemberWithIncompatibleTypes);
var result = sut.Result;
var property = ((DummyClass) result).Number;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(12, property);
}
[Fact]
public void MixedCollection()
{
sut.Process(source.MixedCollection);
var result = sut.Result;
Assert.IsType(typeof(ArrayList), result);
var arrayList = (ArrayList) result;
Assert.True(arrayList.Count > 0);
}
[Fact]
public void MixedCollectionWithRootInstance()
{
var root = new ArrayList();
var assembler = CreateSutForLoadingSpecificInstance(root);
assembler.Process(source.MixedCollection);
var result = assembler.Result;
Assert.IsType(typeof(ArrayList), result);
var arrayList = (ArrayList) result;
Assert.True(arrayList.Count > 0);
}
[Fact]
public void NamedObject_HasCorrectName()
{
sut.Process(source.NamedObject);
var result = sut.Result;
var tb = (TextBlock) result;
Assert.Equal("MyTextBlock", tb.Name);
}
[Fact]
public void ObjectWithChild()
{
sut.Process(source.ObjectWithChild);
var result = sut.Result;
var property = ((DummyClass) result).Child;
Assert.IsType(typeof(DummyClass), result);
Assert.IsType(typeof(ChildClass), property);
}
[Fact]
public void ObjectWithEnumMember()
{
sut.Process(source.ObjectWithEnumMember);
var result = sut.Result;
var property = ((DummyClass) result).EnumProperty;
Assert.IsType<DummyClass>(result);
Assert.Equal(SomeEnum.One, property);
}
[Fact]
public void ObjectWithMember()
{
sut.Process(source.ObjectWithMember);
var result = sut.Result;
var property = ((DummyClass) result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Property!", property);
}
[Fact]
public void ObjectWithNullableEnumProperty()
{
sut.Process(source.ObjectWithNullableEnumProperty);
var result = sut.Result;
var property = ((DummyClass) result).EnumProperty;
Assert.IsType<DummyClass>(result);
Assert.Equal(SomeEnum.One, property);
}
[Fact]
public void ObjectWithTwoMembers()
{
sut.Process(source.ObjectWithTwoMembers);
var result = sut.Result;
var property1 = ((DummyClass) result).SampleProperty;
var property2 = ((DummyClass) result).AnotherProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Property!", property1);
Assert.Equal("Another!", property2);
}
[Fact]
public void OneObject()
{
@ -51,182 +374,30 @@
}
[Fact]
public void ObjectWithMember()
public void PropertyShouldBeAssignedBeforeChildIsAssociatedToItsParent()
{
sut.Process(source.ObjectWithMember);
var result = sut.Result;
var property = ((DummyClass)result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Property!", property);
sut.Process(source.ParentShouldReceiveInitializedChild);
var parent = (SpyingParent) sut.Result;
Assert.True(parent.ChildHadNamePriorToBeingAssigned);
}
[Fact]
public void ObjectWithEnumMember()
public void PureCollection()
{
var sut = CreateSut();
sut.Process(source.ObjectWithEnumMember);
var result = sut.Result;
var property = ((DummyClass)result).EnumProperty;
Assert.IsType<DummyClass>(result);
Assert.Equal(SomeEnum.One, property);
sut.Process(source.PureCollection);
var actual = (ArrayList) sut.Result;
Assert.NotEmpty(actual);
}
[Fact]
public void ObjectWithNullableEnumProperty()
public void RootInstanceWithAttachableMember()
{
var sut = CreateSut();
sut.Process(source.ObjectWithNullableEnumProperty);
var root = new DummyClass();
var sut = CreateSutForLoadingSpecificInstance(root);
sut.Process(source.RootInstanceWithAttachableMember);
var result = sut.Result;
var property = ((DummyClass)result).EnumProperty;
Assert.IsType<DummyClass>(result);
Assert.Equal(SomeEnum.One, property);
}
[Fact]
public void ObjectWithTwoMembers()
{
sut.Process(source.ObjectWithTwoMembers);
var result = sut.Result;
var property1 = ((DummyClass)result).SampleProperty;
var property2 = ((DummyClass)result).AnotherProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Property!", property1);
Assert.Equal("Another!", property2);
}
[Fact]
public void ObjectWithChild()
{
sut.Process(source.ObjectWithChild);
var result = sut.Result;
var property = ((DummyClass)result).Child;
Assert.IsType(typeof(DummyClass), result);
Assert.IsType(typeof(ChildClass), property);
}
[Fact]
public void WithCollection()
{
sut.Process(source.CollectionWithMoreThanOneItem);
var result = sut.Result;
var children = ((DummyClass)result).Items;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(3, children.Count);
Assert.All(children, (child) => Assert.IsType(typeof(Item), child));
}
[Fact]
public void CollectionWithInnerCollection()
{
sut.Process(source.CollectionWithInnerCollection);
var result = sut.Result;
var children = ((DummyClass)result).Items;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(3, children.Count);
Assert.All(children, (child) => Assert.IsType(typeof(Item), child));
var innerCollection = children[0].Children;
Assert.Equal(2, innerCollection.Count);
Assert.All(innerCollection, (child) => Assert.IsType(typeof(Item), child));
}
[Fact]
public void WithCollectionAndInnerAttribute()
{
sut.Process(source.WithCollectionAndInnerAttribute);
var result = sut.Result;
var children = ((DummyClass)result).Items;
var firstChild = children.First();
var property = firstChild.Title;
Assert.IsType(typeof(DummyClass), result);
Assert.All(children, (child) => Assert.IsType(typeof(Item), child));
Assert.Equal("SomeText", property);
}
[Fact]
public void MemberWithIncompatibleTypes()
{
sut.Process(source.MemberWithIncompatibleTypes);
var result = sut.Result;
var property = ((DummyClass)result).Number;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(12, property);
}
[Fact]
public void ExtensionWithArgument()
{
sut.Process(source.ExtensionWithArgument);
var result = sut.Result;
var property = ((DummyClass)result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("Option", property);
}
[Fact]
public void ExtensionWithTwoArguments()
{
sut.Process(source.ExtensionWithTwoArguments);
var result = sut.Result;
var property = ((DummyClass)result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal("OneSecond", property);
}
[Fact]
public void ExtensionThatReturnsNull()
{
sut.Process(source.ExtensionThatReturnsNull);
var result = sut.Result;
var property = ((DummyClass)result).SampleProperty;
Assert.IsType(typeof(DummyClass), result);
Assert.Null(property);
}
[Fact]
public void ExtensionWithNonStringArgument()
{
sut.Process(source.ExtensionWithNonStringArgument);
var result = sut.Result;
var property = ((DummyClass)result).Number;
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(123, property);
}
[Fact]
public void KeyDirective()
{
sut.Process(source.KeyDirective);
var actual = sut.Result;
Assert.IsType(typeof(DummyClass), actual);
var dictionary = (IDictionary)((DummyClass)actual).Resources;
Assert.True(dictionary.Count > 0);
var attachedProperty = Container.GetProperty(result);
Assert.Equal("Value", attachedProperty);
}
[Fact]
@ -253,231 +424,50 @@
Assert.IsType(typeof(DummyClass), lastInstance);
}
[Fact]
public void AttemptToAssignItemsToNonCollectionMember()
{
Assert.Throws<ParseException>(() => sut.Process(source.AttemptToAssignItemsToNonCollectionMember));
}
[Fact]
public void TwoChildrenWithNoRoot_ShouldThrow()
{
Assert.Throws<ParseException>(() => sut.Process(source.TwoRoots));
}
[Fact]
public void PropertyShouldBeAssignedBeforeChildIsAssociatedToItsParent()
{
sut.Process(source.ParentShouldReceiveInitializedChild);
var parent = (SpyingParent)sut.Result;
Assert.True(parent.ChildHadNamePriorToBeingAssigned);
}
[Fact]
public void MixedCollection()
{
sut.Process(source.MixedCollection);
var result = sut.Result;
Assert.IsType(typeof(ArrayList), result);
var arrayList = (ArrayList)result;
Assert.True(arrayList.Count > 0);
}
[Fact]
public void MixedCollectionWithRootInstance()
{
var root = new ArrayList();
var assembler = CreateSutForLoadingSpecificInstance(root);
assembler.Process(source.MixedCollection);
var result = assembler.Result;
Assert.IsType(typeof(ArrayList), result);
var arrayList = (ArrayList)result;
Assert.True(arrayList.Count > 0);
}
[Fact]
public void RootInstanceWithAttachableMember()
{
var root = new DummyClass();
var sut = CreateSutForLoadingSpecificInstance(root);
sut.Process(source.RootInstanceWithAttachableMember);
var result = sut.Result;
var attachedProperty = Container.GetProperty(result);
Assert.Equal("Value", attachedProperty);
}
[Fact]
public void ExpandedAttachablePropertyAndItemBelow()
{
var sut = CreateSut();
sut.Process(source.ExpandedAttachablePropertyAndItemBelow);
var items = ((DummyClass)sut.Result).Items;
var firstChild = items.First();
var attachedProperty = Container.GetProperty(firstChild);
Assert.Equal(2, items.Count);
Assert.Equal("Value", attachedProperty);
}
[Fact]
public void AttachableMemberThatIsCollection()
{
var sut = CreateSut();
sut.Process(source.AttachableMemberThatIsCollection);
var instance = sut.Result;
var col = Container.GetCollection(instance);
Assert.NotEmpty(col);
}
[Fact]
public void CustomCollection()
{
var sut = CreateSut();
sut.Process(source.CustomCollection);
Assert.NotEmpty((IEnumerable)sut.Result);
}
[Fact]
public void PureCollection()
{
var sut = CreateSut();
sut.Process(source.PureCollection);
var actual = (ArrayList)sut.Result;
Assert.NotEmpty(actual);
}
[Fact]
public void ImplicitCollection_ShouldHaveItems()
{
var sut = CreateSut();
sut.Process(source.ImplicitCollection);
var actual = (RootObject)sut.Result;
var customCollection = actual.Collection;
Assert.NotEmpty(customCollection);
}
[Fact]
public void ExplicitCollection_ShouldHaveItems()
{
var sut = CreateSut();
sut.Process(source.ExplicitCollection);
var actual = (RootObject)sut.Result;
var customCollection = actual.Collection;
Assert.NotEmpty(customCollection);
}
[Fact]
public void ImplicitCollection_ShouldKeepSameCollectionInstance()
{
var sut = CreateSut();
sut.Process(source.ImplicitCollection);
var actual = (RootObject)sut.Result;
Assert.False(actual.CollectionWasReplaced);
}
[Fact]
public void ExplicitCollection_ShouldReplaceCollectionInstance()
{
var sut = CreateSut();
sut.Process(source.ExplicitCollection);
var actual = (RootObject)sut.Result;
Assert.True(actual.CollectionWasReplaced);
}
[Fact]
public void NamedObject_HasCorrectName()
{
sut.Process(source.NamedObject);
var result = sut.Result;
var tb = (TextBlock)result;
Assert.Equal("MyTextBlock", tb.Name);
}
[Fact]
public void TwoNestedNamedObjects_HaveCorrectNames()
{
sut.Process(source.TwoNestedNamedObjects);
var result = sut.Result;
var lbi = (ListBoxItem)result;
var textBlock = (TextBlock)lbi.Content;
var lbi = (ListBoxItem) result;
var textBlock = (TextBlock) lbi.Content;
Assert.Equal("MyListBoxItem", lbi.Name);
Assert.Equal("MyTextBlock", textBlock.Name);
}
[Fact]
public void ListBoxWithItemAndTextBlockNoNames()
public void WithCollection()
{
sut.Process(source.ListBoxWithItemAndTextBlockNoNames);
sut.Process(source.CollectionWithMoreThanOneItem);
var w = (Window)sut.Result;
var lb = (ListBox)w.Content;
var lvi = (ListBoxItem)lb.Items.First();
var tb = lvi.Content;
var result = sut.Result;
var children = ((DummyClass) result).Items;
Assert.IsType(typeof(TextBlock), tb);
Assert.IsType(typeof(DummyClass), result);
Assert.Equal(3, children.Count);
Assert.All(children, child => Assert.IsType(typeof(Item), child));
}
[Fact]
public void ListBoxWithItemAndTextBlockWithNames_HaveCorrectNames()
public void WithCollectionAndInnerAttribute()
{
sut.Process(source.ListBoxWithItemAndTextBlockWithNames);
sut.Process(source.WithCollectionAndInnerAttribute);
var w = (Window)sut.Result;
var lb = (ListBox)w.Content;
var lvi = (ListBoxItem)lb.Items.First();
var tb = (TextBlock)lvi.Content;
var result = sut.Result;
var children = ((DummyClass) result).Items;
var firstChild = children.First();
var property = firstChild.Title;
Assert.Equal("MyListBox", lb.Name);
Assert.Equal("MyListBoxItem", lvi.Name);
Assert.Equal("MyTextBlock", tb.Name);
}
[Fact]
public void DirectContentForOneToMany()
{
sut.Process(source.DirectContentForOneToMany);
}
[Fact]
public void CorrectInstanceSetupSequence()
{
var expectedSequence = new[] { SetupSequence.Begin, SetupSequence.AfterAssociatedToParent, SetupSequence.AfterSetProperties, SetupSequence.End };
sut.Process(source.InstanceWithChild);
var listener = (TestListener)sut.LifecycleListener;
Assert.Equal(expectedSequence.ToList().AsReadOnly(), listener.InvocationOrder);
}
[Fact]
public void ChildIsAssociatedBeforeItsPropertiesAreSet()
{
sut.Process(source.InstanceWithChildAndProperty);
var result = (DummyClass)sut.Result;
Assert.False(result.TitleWasSetBeforeBeingAssociated);
}
[Fact]
public void MemberAfterInitalizationValue()
{
sut.Process(source.MemberAfterInitalizationValue);
var root = (RootObject)sut.Result;
var str = root.Collection[0];
var dummy = (DummyClass)root.Collection[1];
Assert.Equal("foo", str);
Assert.Equal(123, dummy.Number);
Assert.IsType(typeof(DummyClass), result);
Assert.All(children, child => Assert.IsType(typeof(Item), child));
Assert.Equal("SomeText", property);
}
}
}

Просмотреть файл

@ -33,8 +33,7 @@
var assembler = new DummyDeferredLoader();
mapping.Map<Template>(t => t.Content, assembler);
var topDownValueContext = new TopDownValueContext();
var objectAssembler = new ObjectAssembler(RuntimeTypeSource, new ValueContext(RuntimeTypeSource, topDownValueContext, new Dictionary<string, object>()));
var objectAssembler = CreateObjectAssembler();
var sut = new TemplateHostingObjectAssembler(objectAssembler, mapping);