public void FindDifferences_Array_int_sameValues_differentOrder_withIgnore() { var oldState = new ArrayVictim { IntArray = new [] { 11, 22, 33 } }; var newState = new ArrayVictim { IntArray = new [] { 22, 33, 11 } }; worker.FindDifferences ( oldState, newState, stateChanges, 5, (field, context) => false, (prop, context) => context.BreadcrumbAsString.Equals("IntArray[1]") ); Assert.Equal(2, stateChanges.Changes.Count); var change = stateChanges.Changes[0]; Assert.Equal("IntArray[0]", change.Breadcrumb); Assert.Equal(11, change.OldValue); Assert.Equal(22, change.NewValue); change = stateChanges.Changes[1]; Assert.Equal("IntArray[2]", change.Breadcrumb); Assert.Equal(33, change.OldValue); Assert.Equal(11, change.NewValue); }
public void FindDifferences_Array_int_sameLength_differentValues() { var oldState = new ArrayVictim { IntArray = new [] { 11, 22, 33 } }; var newState = new ArrayVictim { IntArray = new [] { 44, 55, 66 } }; worker.FindDifferences(oldState, newState, stateChanges, 5); Assert.Equal(3, stateChanges.Changes.Count); var change = stateChanges.Changes[0]; Assert.Equal("IntArray[0]", change.Breadcrumb); Assert.Equal(11, change.OldValue); Assert.Equal(44, change.NewValue); change = stateChanges.Changes[1]; Assert.Equal("IntArray[1]", change.Breadcrumb); Assert.Equal(22, change.OldValue); Assert.Equal(55, change.NewValue); change = stateChanges.Changes[2]; Assert.Equal("IntArray[2]", change.Breadcrumb); Assert.Equal(33, change.OldValue); Assert.Equal(66, change.NewValue); }
public void Array_OnProperty_isCalled_forEachElement_andRecurses() { var one = new InnocentBystander(); var two = new InnocentBystander(); var three = new InnocentBystander(); var victim = new ArrayVictim { PropComplex = new[] { one, two, three } }; var type = victim.GetType(); var complexType = typeof(InnocentBystander); var callCounts = new Dictionary <string, int> { { ".Root", 0 }, { "PropComplex[0]", 0 }, { "PropComplex[0].FieldString", 0 }, { "PropComplex[1]", 0 }, { "PropComplex[1].FieldString", 0 }, { "PropComplex[2]", 0 }, { "PropComplex[2].FieldString", 0 } }; // the calls for the array Prop and each of its elements A.CallTo(() => listener.OnProperty(GetProp(type, "PropComplex"), A <Func <object> > ._, A <InstanceTraversalContext> ._)) .Invokes(x => { var ctx = (InstanceTraversalContext)x.Arguments[2]; if (ctx.CurrentDepth == 0) { callCounts[".Root"]++; return; } // for the individual elements Assert.Equal(1, ctx.CurrentDepth); callCounts[ctx.BreadcrumbAsString]++; }); // the calls for the recursion of each array element A.CallTo(() => listener.OnField(GetField(complexType, "FieldString"), A <Func <object> > ._, A <InstanceTraversalContext> ._)) .Invokes(x => { var ctx = (InstanceTraversalContext)x.Arguments[2]; Assert.Equal(1, ctx.CurrentDepth); callCounts[$"{ctx.BreadcrumbAsString}.FieldString"]++; }); Traverser.TraverseInstance(victim, 5, listener); // all "methods" should have been called exactly once foreach (var(key, value) in callCounts) { // this sillyness provides us with a hint for which thing was not called as expected Assert.Equal($"{key}=1", $"{key}={value}"); } }
public void FindDifferences_Array_int_bothEmpty() { var oldState = new ArrayVictim { IntArray = new int[5] }; var newState = new ArrayVictim { IntArray = new int[5] }; worker.FindDifferences(oldState, newState, stateChanges, 5); Assert.Empty(stateChanges.Changes); }
public void Array_OnProperty_isCalled_forEachElement_simpleType_andDoesNotRecurse() { var victim = new ArrayVictim { PropInts = new[] { 11, 22, 33 } }; var type = victim.GetType(); var callCounts = new Dictionary <string, int> { { ".Root", 0 }, { "PropInts[0]", 0 }, { "PropInts[1]", 0 }, { "PropInts[2]", 0 } }; A.CallTo(() => listener.OnProperty(GetProp(type, "PropInts"), A <Func <object> > ._, A <InstanceTraversalContext> ._)) .Invokes(x => { var ctx = (InstanceTraversalContext)x.Arguments[2]; if (ctx.CurrentDepth == 0) { callCounts[".Root"]++; return; } // for the individual elements Assert.Equal(1, ctx.CurrentDepth); callCounts[ctx.BreadcrumbAsString]++; }) .Returns(new SimpleInstanceListenerOnFieldOrPropResult { DoContinueRecursion = true }); Traverser.TraverseInstance(victim, 5, listener); A.CallTo(() => listener.OnField(A <FieldInfo> .That.Matches(x => x.Name.Equals("m_value")), A <Func <object> > ._, A <IReadOnlyInstanceTraversalContext> ._)).MustNotHaveHappened(); // all "methods" should have been called exactly once foreach (var(key, value) in callCounts) { // this sillyness provides us with a hint for which thing was not called as expected Assert.Equal($"{key}=1", $"{key}={value}"); } }
public void Array_OnProperty_isNotCalled_forEachElement_becauseOnPropertyReturnsFalse() { var victim = new ArrayVictim { PropInts = new[] { 11, 22, 33 } }; var type = victim.GetType(); var callCounts = new Dictionary <string, int> { { ".Root", 0 }, { "PropInts[0]", 0 }, { "PropInts[1]", 0 }, { "PropInts[2]", 0 } }; A.CallTo(() => listener.OnProperty(GetProp(type, "PropInts"), A <Func <object> > ._, A <InstanceTraversalContext> ._)) .Invokes(x => { var ctx = (InstanceTraversalContext)x.Arguments[2]; if (ctx.CurrentDepth == 0) { callCounts[".Root"]++; return; } // for the individual elements Assert.Equal(1, ctx.CurrentDepth); callCounts[ctx.BreadcrumbAsString]++; }) .Returns(new SimpleInstanceListenerOnFieldOrPropResult { DoContinueRecursion = false }); // do not traverse further down this path Traverser.TraverseInstance(victim, 5, listener); A.CallTo(() => listener.OnField(A <FieldInfo> .That.Matches(x => x.Name.Equals("m_value")), A <Func <object> > ._, A <IReadOnlyInstanceTraversalContext> ._)).MustNotHaveHappened(); Assert.Equal(1, callCounts[".Root"]); Assert.Equal(0, callCounts["PropInts[0]"]); Assert.Equal(0, callCounts["PropInts[1]"]); Assert.Equal(0, callCounts["PropInts[2]"]); }
public void Array_OnField_isCalled_forEachElement_simpleType() { var victim = new ArrayVictim { FieldInts = new[] { 11, 22, 33 } }; var type = victim.GetType(); var callCounts = new Dictionary <string, int> { { ".Root", 0 }, { "FieldInts[0]", 0 }, { "FieldInts[1]", 0 }, { "FieldInts[2]", 0 } }; A.CallTo(() => listener.OnField(GetField(type, "FieldInts"), A <Func <object> > ._, A <InstanceTraversalContext> ._)) .Invokes(x => { var ctx = (InstanceTraversalContext)x.Arguments[2]; if (ctx.CurrentDepth == 0) { callCounts[".Root"]++; return; } // for the individual elements Assert.Equal(1, ctx.CurrentDepth); callCounts[ctx.BreadcrumbAsString]++; }); Traverser.TraverseInstance(victim, 5, listener); // all "methods" should have been called exactly once foreach (var(key, value) in callCounts) { // this sillyness provides us with a hint for which thing was not called as expected Assert.Equal($"{key}=1", $"{key}={value}"); } }