public void CaptureGraph_Compare_UnitTest()
        {
            var source = new Foo();

            source.e = new Foo();
            var initialGraph = ObjectGraphCapture.CaptureGraph(source);

            source.a++;
            source.c = "Changed";
            source.e = new Foo();
            source.e.a++;
            var finalGraph = ObjectGraphCapture.CaptureGraph(source);
            var result     = GraphCompare.ReportChanges(initialGraph, finalGraph);
        }
Example #2
0
        /// <summary>
        ///     Tracks the object graph.
        /// </summary>
        /// <param name="graphRoot">The graph root.</param>
        /// <param name="action">The action.</param>
        protected void TrackObjectGraph(Object graphRoot, Action action)
        {
            if (graphRoot == null)
            {
                action();
                return;
            }

            var            monitors     = new List <EventMonitor>();
            CaptureResults initialGraph = null;

            if (ReportObjectGraphChanges)
            {
                initialGraph = ObjectGraphCapture.CaptureGraph(graphRoot);
                if (initialGraph.Events.Count > 0)
                {
                    foreach (var pair in initialGraph.Events)
                    {
                        var eventInfo = pair.Value.Item2;
                        if ((eventInfo.AddMethod.IsPublic /*|| eventInfo.AddMethod.IsAssembly*/) &&
                            (eventInfo.RemoveMethod.IsPublic /*|| eventInfo.RemoveMethod.IsAssembly*/))
                        {
                            var target  = pair.Value.Item1;
                            var monitor = new EventMonitor(pair.Key, eventInfo, target);
                            monitor.EventExpected = r_ExpectedEvents.Contains(pair.Key);
                            monitors.Add(monitor);
                        }
                    }
                }
            }

            action();
            if (ReportObjectGraphChanges)
            {
                int unexpectedCount = 0;
                foreach (var monitor in monitors)
                {
                    if (monitor.EventFired && !monitor.EventExpected)
                    {
                        ++unexpectedCount;
                        Console.WriteLine("Unexprected Event: {0}", monitor.Source);
                    }
                    monitor.RemoveHandler();
                }
                monitors.Clear();
                CaptureResults finalGraph = ObjectGraphCapture.CaptureGraph(graphRoot);
                ChangeList     result     = GraphCompare.ReportChanges(initialGraph, finalGraph);
                if (result.Count > 0)
                {
                    Console.WriteLine("===== Detected Object Graph Changes =====");
                    result.Display(Console.Out);
                    Console.WriteLine("==========================================");
                }
                else
                {
                    Console.WriteLine("===== Invocation did not Modify State! =====");
                }
                if (unexpectedCount > 0)
                {
                    Assert.Fail("Unexpected Events Occured!!!!");
                }
            }
        }