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 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}"); } }