Example #1
0
        public static Dictionary <string, Tuple <object, object> > DifferentProperties(this IBHoMObject obj1, IBHoMObject obj2, BH.oM.Base.ComparisonConfig config = null)
        {
            var dict = new Dictionary <string, Tuple <object, object> >();

            //Use default config if null
            config = config ?? new BH.oM.Base.ComparisonConfig();

            CompareLogic comparer = new CompareLogic();

            comparer.Config.MaxDifferences  = 1000;
            comparer.Config.MembersToIgnore = config.PropertyExceptions;
            comparer.Config.DoublePrecision = config.NumericTolerance;
            comparer.Config.TypesToIgnore   = config.TypeExceptions;

            ComparisonResult result = comparer.Compare(obj1, obj2);

            dict = result.Differences.ToDictionary(diff => diff.PropertyName, diff => new Tuple <object, object>(diff.Object1, diff.Object2));

            if (dict.Count == 0)
            {
                return(null);
            }

            return(dict);
        }
Example #2
0
        public static Output <bool, List <string>, List <string>, List <string> > IsEqual(this object obj1, object obj2, BH.oM.Base.ComparisonConfig config = null)
        {
            //Use default config if null
            config = config ?? Create.DefaultTestComparisonConfig();

            CompareLogic comparer = new CompareLogic();

            comparer.Config.MaxDifferences          = config.MaxPropertyDifferences;
            comparer.Config.MembersToIgnore         = config.PropertyExceptions;
            comparer.Config.DoublePrecision         = config.NumericTolerance;
            comparer.Config.TypesToIgnore           = config.TypeExceptions;
            comparer.Config.CompareStaticFields     = false;
            comparer.Config.CompareStaticProperties = false;

            Output <bool, List <string>, List <string>, List <string> > output = new Output <bool, List <string>, List <string>, List <string> >
            {
                Item1 = false,
                Item2 = new List <string>(),
                Item3 = new List <string>(),
                Item4 = new List <string>()
            };

            if (obj1 == null || obj2 == null)
            {
                return(output);
            }

            try
            {
                ComparisonResult result = comparer.Compare(obj1, obj2);
                output.Item1 = result.AreEqual;
                output.Item2 = result.Differences.Select(x => x.PropertyName).ToList();
                output.Item3 = result.Differences.Select(x => x.Object1Value).ToList();
                output.Item4 = result.Differences.Select(x => x.Object2Value).ToList();
            }
            catch (Exception e)
            {
                Engine.Base.Compute.RecordError($"Comparison between {obj1.IToText()} and {obj2.IToText()} failed:\n{e.Message}");
            }

            return(output);
        }
Example #3
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static TestResult RunOneSet(IBHoMAdapter adapter, string setName, List <IBHoMObject> objects, IRequest request, IEqualityComparer <IBHoMObject> comparer, bool resetModelBetweenRuns)
        {
            if (adapter == null)
            {
                return(new TestResult {
                    Description = "PushPullCompare", Message = "Adapter is null, could not preform test", Status = oM.Test.TestStatus.Error
                });
            }

            TestResult setResult = new TestResult {
                Description = $"PushPullCompare: Adapter: {adapter.GetType().Name}, test set: {setName}.", ID = adapter.GetType().Name + ":" + setName
            };

            bool success = true;

            //CalculateObjectHashes
            objects = objects.SetHashFragment();

            //Start up an empty model
            if (resetModelBetweenRuns)
            {
                adapter.Execute(new NewModel());
            }

            List <IBHoMObject> pushedObjects = new List <IBHoMObject>();
            List <IBHoMObject> pulledObjects = new List <IBHoMObject>();

            BH.oM.Base.ComparisonConfig config = Engine.Test.Create.DefaultTestComparisonConfig();
            config.PropertyExceptions.Add("CustomData");
            config.TypeExceptions.Add(typeof(HashFragment));

            //Push objects
            try
            {
                Engine.Base.Compute.ClearCurrentEvents();
                pushedObjects = adapter.Push(objects).Cast <IBHoMObject>().ToList();
                success      &= pushedObjects.Count == objects.Count;
            }
            catch (Exception e)
            {
                Engine.Base.Compute.RecordError(e.Message);
                setResult.Message = "The adapter crashed trying to push the objects.";
                setResult.Status  = oM.Test.TestStatus.Error;
                return(setResult);
            }
            finally
            {
                setResult.Information.AddRange(Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()));
            }

            if (pushedObjects.Count != objects.Count)
            {
                setResult.Information.AddRange(objects.Where(x => !pushedObjects.Any(y => x.Name == y.Name)).Select(x => TestFailedResult(x, "Object could not be pushed.")));
            }

            //Pull objects
            try
            {
                Engine.Base.Compute.ClearCurrentEvents();
                pulledObjects = adapter.Pull(request).Cast <IBHoMObject>().ToList();
                success      &= pulledObjects.Count == pushedObjects.Count;
            }
            catch (Exception e)
            {
                Engine.Base.Compute.RecordError(e.Message);
                setResult.Message = "The adapter crashed trying to pull the objects.";
                setResult.Status  = oM.Test.TestStatus.Error;
                return(setResult);
            }
            finally
            {
                setResult.Information.AddRange(Engine.Base.Query.CurrentEvents().Select(x => x.ToEventMessage()));
            }

            //Compare pushed and pulled objects
            VennDiagram <IBHoMObject> diagram = Engine.Data.Create.VennDiagram(pushedObjects, pulledObjects, comparer);


            foreach (Tuple <IBHoMObject, IBHoMObject> pair in diagram.Intersection)
            {
                setResult.Information.Add(ComparePushedAndPulledObject(pair.Item1, pair.Item2, config));
            }

            setResult.Information.AddRange(diagram.OnlySet1.Select(x => TestFailedResult(x, "Unable to compare the object.")));

            //Close the model
            if (resetModelBetweenRuns)
            {
                adapter.Execute(new Close {
                    SaveBeforeClose = false
                });
            }

            //Clear events
            Engine.Base.Compute.ClearCurrentEvents();

            setResult.Status = setResult.Information.OfType <TestResult>().MostSevereStatus();

            if (setResult.Status == oM.Test.TestStatus.Error)
            {
                setResult.Message = "Errors where raised trying to run the test";
            }
            else if (setResult.Status == oM.Test.TestStatus.Pass)
            {
                setResult.Message = "All objects were successfully pushed, pulled and compared without any differences.";
            }
            else
            {
                setResult.Message = "All objects were successfully pushed and pulled, but some differences were found.";
            }

            return(setResult);
        }