Ejemplo n.º 1
0
        public void Dictionary_OnProperty_simpleKey_simpleValue()
        {
            var victim = new SimpleKeyValueVictim
            {
                Dictionary = new Dictionary <string, int>
                {
                    { "one", 1 },
                    { "two", 2 }
                }
            };

            var type = victim.GetType();

            var callCounts = new Dictionary <string, int>
            {
                { ".Root", 0 },
                { "Dictionary[one]", 0 },
                { "Dictionary[two]", 0 }
            };

            // the calls for the list field and each of its elements
            A.CallTo(() => listener.OnProperty(GetProp(type, "Dictionary"), 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);

            // 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}");
            }
        }
Ejemplo n.º 2
0
        public void Dictionary_OnProperty_stopTraversing_becauseOnPropertyReturnsFalse()
        {
            var victim = new SimpleKeyValueVictim
            {
                Dictionary = new Dictionary <string, int>
                {
                    { "one", 1 },
                    { "two", 2 }
                }
            };

            var type = victim.GetType();

            var callCounts = new Dictionary <string, int>
            {
                { ".Root", 0 },
                { "Dictionary[one]", 0 },
                { "Dictionary[two]", 0 }
            };


            // the calls for the list field and each of its elements
            A.CallTo(() => listener.OnProperty(GetProp(type, "Dictionary"), 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);

            Assert.Equal(1, callCounts[".Root"]);

            Assert.Equal(0, callCounts["Dictionary[one]"]);
            Assert.Equal(0, callCounts["Dictionary[two]"]);
        }