public void WhenCurrentAndNextAreDifferentTypes_ReturnsModified()
            {
                var current = CreateListTreeItem();
                var next    = CreatePrimitiveTreeItem(1);

                var actual = TreeComparer.CreateDifferenceTree(current, next);

                Assert.That(actual.DiffType, Is.EqualTo(DiffType.Modified));
            }
            public void WhenSimpleState_IsEqual_ReturnsNull()
            {
                var objectData = new StateObjectData("tn", ImmutableDictionary <string, ObjectData> .Empty, hasKey: false, key: null);
                var current    = new StateObjectTreeItem(null, "pn", objectData, depth: 0);
                var next       = new StateObjectTreeItem(null, "pn", objectData, depth: 0);

                var actual = TreeComparer.CreateDifferenceTree(current, next);

                Assert.That(actual, Is.Null);
            }
            public void WhenCurrentIsNullButNextIsValid_ReturnsAddedDifferenceItem()
            {
                var next = CreatePrimitiveTreeItem(1);

                var actual = TreeComparer.CreateDifferenceTree(null, next);

                Assert.That(actual, Is.Not.Null);
                Assert.That(actual.DiffType, Is.EqualTo(DiffType.Added));
                Assert.That(actual.Current, Is.Null);
                Assert.That(actual.Next, Is.SameAs(next));
            }
            public void WhenCurrentIsPresentButNextIsNull_ReturnsRemovedDifferenceItem()
            {
                var current = CreatePrimitiveTreeItem(1);

                var actual = TreeComparer.CreateDifferenceTree(current, null);

                Assert.That(actual, Is.Not.Null);
                Assert.That(actual.DiffType, Is.EqualTo(DiffType.Removed));
                Assert.That(actual.Current, Is.SameAs(current));
                Assert.That(actual.Next, Is.Null);
            }
            public void WhenReferencesChangeButContentIsSame_NullIsReturned()
            {
                var current = new Root {
                    First = new FirstBranch {
                        Alpha = "a", Omega = 1
                    }
                };
                var next = new Root {
                    First = new FirstBranch {
                        Alpha = "a", Omega = 1
                    }
                };

                var actual = (DifferenceItemContainer)TreeComparer.CreateDifferenceTree(ToTreeItem(current), ToTreeItem(next));

                Assert.That(actual, Is.Null);
            }
            public void WhenCurrentIsNull_ButNextIsNot_ReturnsWholeNextBranchAsAdded()
            {
                var next = new Root {
                    First = new FirstBranch {
                        Alpha = "a", Omega = 1
                    }
                };

                var actual = (DifferenceItemContainer)TreeComparer.CreateDifferenceTree(null, ToTreeItem(next));

                Assert.That(actual.DiffType, Is.EqualTo(DiffType.Added));
                Assert.That(actual.Children.Length, Is.EqualTo(2));
                var first = actual.Children[0];

                Assert.That(first.DiffType, Is.EqualTo(DiffType.Added));
                var second = actual.Children[1];

                Assert.That(second.DiffType, Is.EqualTo(DiffType.Added));
            }
            public void WhenLeafChangesAndNotReferenceEquals_OnlyLeafIsCollected()
            {
                var current = new Root {
                    First = new FirstBranch {
                        Alpha = "a", Omega = 1
                    }
                };
                var next = new Root {
                    First = new FirstBranch {
                        Alpha = "ab", Omega = 1
                    }
                };

                var actual = (DifferenceItemContainer)TreeComparer.CreateDifferenceTree(ToTreeItem(current), ToTreeItem(next));

                Assert.That(actual.DiffType, Is.EqualTo(DiffType.Modified));
                var first = (DifferenceItemContainer)actual.Children[0];

                Assert.That(first.Children.Length, Is.EqualTo(1));
                var alpha = first.Children[0];

                Assert.That(alpha.DescriptionHeader, Is.EqualTo("Alpha"));
                Assert.That(alpha.DiffType, Is.EqualTo(DiffType.Modified));
            }
            public void WhenBothTreeItemsAreNull_ReturnsNull()
            {
                var actual = TreeComparer.CreateDifferenceTree(null, null);

                Assert.That(actual, Is.Null);
            }
Beispiel #9
0
        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);
        }