public void RecursiveItemsAreOnlyRecursiveItemsTest() { var first = new ExtendedClassWithRecursion(); first.Name = "first"; var second = new ExtendedClassWithRecursion(); second.Name = "second"; var third = new ClassWithRecursion(); third.Name = "third"; first.Subclass = second; second.Subclass = first; first.AnotherSubclass = third; third.Subclass = second; var sourceTree = new ObjectTreeItem(first); sourceTree.CreateTree(); foreach (var recursiveSourceTreeItem in sourceTree.RecursionObjectTreeItems) { //if (!ReferenceEquals(recursiveSourceTreeItem.Parent.Item, first)) if (recursiveSourceTreeItem.Item != first) { // Tree contains errors, if recursive tree items does not contain root as any of their child Trace.TraceInformation(sourceTree.ToFormattedString()); throw new Exception("<<<< BUG IN OBJECT TREE>>>>>\n" + "In: \"" + recursiveSourceTreeItem.Item + "\"\n" + "Expected Item: \"" + first + "\"\n" + "Actual Items: \"" + recursiveSourceTreeItem.ToFormattedString()); } } }
public void CreateTreeProperly() { var first = new ClassWithRecursion(); first.Name = "First"; var second = new ClassWithRecursion(); second.Name = "Second"; first.Subclass = second; var sourceTree = new ObjectTreeItem(first); sourceTree.CreateTree(); Trace.WriteLine(sourceTree.ToFormattedString()); Assert.IsNull(sourceTree.Parent); Assert.IsNotNull(sourceTree.Childs); Assert.IsTrue(IEnumerableExtensions.Any(sourceTree.Childs)); Assert.AreEqual(2, sourceTree.Childs.Count); Assert.AreEqual(first, sourceTree.Item); Assert.IsTrue(sourceTree.Childs.Any(item => item.Item == second)); foreach (var objectTreeItem in sourceTree.Childs) { Assert.AreEqual(sourceTree, objectTreeItem.Parent); } }
// [ExpectedException(typeof(AssertFailedException))] public void TreeContainsCorrectRecuriveTreeItems() { var first = new ExtendedClassWithRecursion(); first.Name = "first"; var second = new ExtendedClassWithRecursion(); second.Name = "second"; var third = new ClassWithRecursion(); third.Name = "third"; first.Subclass = second; second.Subclass = first; first.AnotherSubclass = third; third.Subclass = second; var sourceTree = new ObjectTreeItem(first); sourceTree.CreateTree(); Trace.WriteLine(string.Join("\n", sourceTree.RecursionObjectTreeItems.Select(item => item.Parent.Item))); Assert.AreEqual(2, sourceTree.RecursionObjectTreeItems.Count); Assert.IsTrue(sourceTree.RecursionObjectTreeItems.Any(item => item.Parent.Item == second)); Assert.IsFalse(sourceTree.RecursionObjectTreeItems.Any(item => item.Parent.Item == first)); Assert.IsFalse(sourceTree.RecursionObjectTreeItems.Any(item => item.Parent.Item == third)); }
private void BuildListTree <T>(params T[] arrayElements) { var instance = new ClassWithMembers <List <T> >(arrayElements.ToList(), arrayElements.ToList()); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
public void TreeCanHandleGuid() { var instance = new ClassWithMembers <Guid>(new Guid(), new Guid()); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
private void TestObjectTreeCreation <T>(T property, T field) { var instance = new ClassWithMembers <T>(field, property); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
public void TreeCanHandleIpAddress() { var instance = new ClassWithMembers <IPAddress>(new IPAddress(19216811), new IPAddress(19216811)); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
public static DifferenceItem FromBranchRemoved(ObjectTreeItem source) { if (source is INodeObjectTreeItem node) { var children = node.Children.Select(n => FromBranchRemoved(n)).ToArray(); return(new DifferenceItemContainer(children, source, null, DiffType.Removed)); } else { return(new DifferenceItem(source, null, DiffType.Removed)); } }
public void TreeCanHandleEnumberable() { var action = new Func <IEnumerable <string> >(CreateString); var instance = new ClassWithMembers <IEnumerable <string> >(action.Invoke(), new List <string>()); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
public static DifferenceItem FromBranchModified(ObjectTreeItem current, ObjectTreeItem next) { if (next is INodeObjectTreeItem node) { var children = node.Children.Select(n => FromBranchAdded(n)).ToArray(); return(new DifferenceItemContainer(children, current, next, DiffType.None)); } else { return(new DifferenceItem(current, next, DiffType.None)); } }
public void TreeCanHandleDelegates() { Del del = delegate(string name) { Console.WriteLine("Notification received for: {0}", name); }; var instance = new ClassWithMembers <Delegate>(del); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
/// <summary> /// /// </summary> /// <param name="current"></param> /// <param name="next"></param> /// <returns></returns> /// <remarks> /// Root has to be the same type, no property. /// Also it can't be a primitive /// </remarks> public static DifferenceItem CreateDifferenceTree(ObjectTreeItem current, ObjectTreeItem next) { if (ReferenceEquals(current, null)) { if (!ReferenceEquals(next, null)) { return(FromBranchAdded(next)); } else { // when both are null return(null); } } if (!ReferenceEquals(current, null)) { if (ReferenceEquals(next, null)) { return(new DifferenceItem(current, null, DiffType.Removed)); } } if (ReferenceEquals(current.Source, next.Source)) { return(null); } if (current.GetType() != next.GetType()) { var removed = FromBranchRemoved(current); var added = FromBranchAdded(next); return(new DifferenceItemContainer(new[] { removed, added }, current, next, DiffType.Modified)); } // from this point both states are same type else if (current is StateObjectTreeItem currentState) { var nextState = (StateObjectTreeItem)next; return(FromNamedProperties(currentState, nextState)); } else if (current is DictionaryObjectTreeItem currentDictionary) { var nextDictionary = (DictionaryObjectTreeItem)next; return(FromNamedProperties(currentDictionary, nextDictionary)); } else if (current is ListObjectTreeItem) { return(FromList((ListObjectTreeItem)current, (ListObjectTreeItem)next)); } else if (current is PrimitiveObjectTreeItem) { return(FromPrimitive((PrimitiveObjectTreeItem)current, (PrimitiveObjectTreeItem)next)); } throw new Exception($"Unknown source type {current.GetType().Name}"); }
public void TreeCanHandleOwnGenericList() { var instance = new ClassWithMembers <OwnGenericListClass <string> >(new OwnGenericListClass <string>() { "test1", "test2" }, new OwnGenericListClass <string>()); var treeItem = new ObjectTreeItem(instance); treeItem.CreateTree(); Trace.WriteLine(treeItem.ToFormattedString()); }
public Step(int key, ReduxAction action, object state, ObjectData actionData, ObjectTreeItem actionTreeItem, ObjectData stateData, ObjectTreeItem stateTreeItem, DifferenceItem differenceItem, bool differenceCalculated) { Key = key; Action = action; State = state; ActionData = actionData; ActionTreeItem = actionTreeItem; StateData = stateData; StateTreeItem = stateTreeItem; DifferenceItem = differenceItem; DifferenceCalculated = differenceCalculated; }
// [ExpectedException(typeof(AssertFailedException))] public void SelfReferencingObjectIsProperlyCreated() { var first = new ClassWithRecursion(); first.Name = "first"; first.Subclass = first; var sourceTree = new ObjectTreeItem(first); sourceTree.CreateTree(); Trace.WriteLine(sourceTree.ToFormattedString()); Assert.AreEqual(1, sourceTree.RecursionObjectTreeItems.Count); Assert.IsTrue(sourceTree.RecursionObjectTreeItems.Any(item => item.Parent.Item == first)); Assert.AreSame(sourceTree.Item, sourceTree.Childs.First(item => item.Item == sourceTree.Item).Item); }
public void TreeCanHandleFunctions() { var instance1 = new ClassWithMembers <Func <string, int> >(s => 5); var instance2 = new ClassWithMembers <Func <string> >(() => "test", () => "test"); var instance3 = new ClassWithMembers <Func <string> >(() => "test", () => "test"); var treeItem1 = new ObjectTreeItem(instance1); var treeItem2 = new ObjectTreeItem(instance2); var treeItem3 = new ObjectTreeItem(instance3); treeItem1.CreateTree(); treeItem2.CreateTree(); treeItem3.CreateTree(); Trace.WriteLine(treeItem1.ToFormattedString()); Trace.WriteLine(treeItem2.ToFormattedString()); Trace.WriteLine(treeItem3.ToFormattedString()); }
public static bool AreKeysEqual(ObjectTreeItem left, ObjectTreeItem right) { var leftObjectSource = left.Source; var rightObjectSource = right.Source; if (leftObjectSource is null || rightObjectSource is null || leftObjectSource.GetType() != rightObjectSource.GetType()) { return(false); } // state objects might implement keys var leftStateObject = left as StateObjectTreeItem; var rightStateObject = right as StateObjectTreeItem; if (!(leftStateObject is null) && !(rightStateObject is null)) { return(Equals(leftStateObject.Key, rightStateObject.Key)); } // just check for equality return(Equals(left, right)); }
// [ExpectedException(typeof(AssertFailedException))] public void ToFormattedStringTest() { var first = new ExtendedClassWithRecursion(); first.Name = "first"; var second = new ExtendedClassWithRecursion(); second.Name = "second"; var third = new ClassWithRecursion(); third.Name = "third"; first.Subclass = second; second.Subclass = first; first.AnotherSubclass = third; third.Subclass = second; var sourceTree = new ObjectTreeItem(first); sourceTree.CreateTree(); Trace.WriteLine(sourceTree.ToFormattedString()); }
public static ObjectTreeItem ToTreeHierarchy(ObjectTreeItem parent, ObjectData source, int depth, string propertyName) { if (depth > MaxDepth) { throw new Exception("Object graph is too deep"); } switch (source) { case StateObjectData state: return(FormatState(parent, depth, propertyName, state)); case ListData list: return(FormatList(parent, depth, propertyName, list)); case DictionaryData dictionary: return(FormatDictionary(parent, depth, propertyName, dictionary)); case PrimitiveData primitive: return(FormatPrimitive(parent, depth, propertyName, primitive)); default: throw new Exception($"Unknown ObjectData {source.GetType()}"); } }
public async Task <RootState> ReduceAsync(RootState state, ReduxAction action, CancellationToken ct) { RootState result; switch (action) { case InsertNewAction insertNew: int key = state.Steps.Length; var actionDataTask = Task.Run(() => PropertiesCollector.Collect(insertNew.Action), ct); var stateDataTask = Task.Run(() => PropertiesCollector.Collect(insertNew.State), ct); await actionDataTask.ConfigureAwait(false); var actionTreeItem = await Task.Run(() => { string actionName = StateFormatter.GetActionName(insertNew.Action); return(StateFormatter.ToTreeHierarchy(actionDataTask.Result, actionName)); }).ConfigureAwait(false); await stateDataTask.ConfigureAwait(false); result = state.Clone(steps: state.Steps.Spread( new Step(key, insertNew.Action, insertNew.State, actionData: actionDataTask.Result, actionTreeItem: actionTreeItem, stateData: stateDataTask.Result, stateTreeItem: null, differenceItem: null, differenceCalculated: false))); break; case GenerateTreeHierarchyAction generateTreeHierarchy: { Step selectedStep = state.SelectedStep; result = state; if (selectedStep != null) { if (selectedStep.StateTreeItem is null) { var hierarchy = StateFormatter.ToTreeHierarchy(selectedStep.StateData); Step updated = selectedStep.Clone(stateTreeItem: hierarchy); result = result.Clone(steps: state.Steps.Replace(selectedStep, updated), selectedStep: updated); selectedStep = updated; } if (!selectedStep.DifferenceCalculated) { // first check if previous step has StateTree var selectedStepIndex = Array.IndexOf(result.Steps, selectedStep); var previousStep = selectedStepIndex > 0 ? result.Steps[selectedStepIndex - 1] : null; ObjectTreeItem previousHierarchy = null; if (previousStep != null) { if (previousStep.StateTreeItem == null) { previousHierarchy = StateFormatter.ToTreeHierarchy(previousStep.StateData); Step previousUpdated = previousStep.Clone(stateTreeItem: previousHierarchy); result = result.Clone(steps: result.Steps.Replace(previousStep, previousUpdated)); } else { previousHierarchy = previousStep.StateTreeItem; } } var difference = TreeComparer.CreateDifferenceTree(previousHierarchy, selectedStep.StateTreeItem); Step updated = selectedStep.Clone(differenceItem: difference, differenceCalculated: true); result = result.Clone(steps: result.Steps.Replace(selectedStep, updated), selectedStep: updated); } } } break; case SelectedStepChangedAction selectedStepChanged: { var selectedStep = selectedStepChanged.Key.HasValue ? state.Steps.Single(s => s.Key == selectedStepChanged.Key) : null; result = state.Clone(selectedStep: selectedStep); } break; default: result = state; break; } return(result); }
private static IEnumerable <ObjectTreeItem> GetChildren(IEnumerable <ObjectTreeItem> forestElements, ObjectTreeItem parent) { return(forestElements.Where(i => i.ParentId == parent.Id)); }
private static void AddChildrenToTreeElements(List <ObjectTreeItem> treeElements, IEnumerable <ObjectTreeItem> forestElements, ObjectTreeItem parent) { var children = GetChildren(forestElements, parent); var goodChildren = children.Where(c => !AreAlreadyInGraph(treeElements, GetChildren(forestElements, c))); treeElements.AddRange(goodChildren); foreach (var goodChild in goodChildren) { AddChildrenToTreeElements(treeElements, forestElements, goodChild); } }
public static PrimitiveObjectTreeItem FormatPrimitive(ObjectTreeItem parent, int depth, string propertyName, PrimitiveData source) { return(new PrimitiveObjectTreeItem(source.Value, parent, propertyName, source)); }
public static ListObjectTreeItem FormatList(ObjectTreeItem parent, int depth, string propertyName, ListData source) { return(new ListObjectTreeItem(parent, propertyName, source, depth)); }
public static DictionaryObjectTreeItem FormatDictionary(ObjectTreeItem parent, int depth, string propertyName, DictionaryData source) { return(new DictionaryObjectTreeItem(parent, propertyName, source, depth)); }
/// <summary> /// Creates <see cref=" StateObjectTreeItem"/>. Besides properties it could implement either a <see cref="IDictionary"/> or <see cref="IEnumerable"/> /// but not both. /// </summary> /// <param name="depth"></param> /// <param name="propertyName"></param> /// <param name="source"></param> /// <returns></returns> public static StateObjectTreeItem FormatState(ObjectTreeItem parent, int depth, string propertyName, StateObjectData source) { return(new StateObjectTreeItem(parent, propertyName, source, depth)); }