// Copyright (C) Stichting Deltares 2022. All rights reserved. // // This file is part of Riskeer. // // Riskeer is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections; using System.Collections.Generic; using Core.Common.Base; using NUnit.Framework; namespace Core.Common.Data.TestUtil { /// /// Class for asserting whether two objects are clones. /// public static class CoreCloneAssert { /// /// Method that asserts whether and /// are clones. Some general clone assertions are performed, followed by the type specific /// assertions (provided via ). /// /// The type of the objects to assert. /// The original object. /// The cloned object. /// The action for performing the /// specific assertions. /// Thrown when /// is null. /// Thrown when and /// are not clones. public static void AreObjectClones(T original, object clone, Action typeSpecificAsserts) { if (typeSpecificAsserts == null) { throw new ArgumentNullException(nameof(typeSpecificAsserts)); } if (original == null && clone == null) { return; } Assert.IsNotNull(original); Assert.IsInstanceOf(clone); Assert.AreNotSame(original, clone); var observable = original as IObservable; if (observable != null) { Assert.AreNotSame(observable.Observers, ((IObservable) clone).Observers); } typeSpecificAsserts(original, (T) clone); } /// /// Method that asserts whether and /// are clones. Some general clone assertions are performed, followed by the type specific /// assertions on a per element basis (provided via ). /// /// The type of the objects in the enumerations to assert. /// The original enumeration. /// The cloned enumeration. /// The action for performing the /// specific assertions on a per element basis. /// Thrown when /// is null. /// Thrown when and /// are not clones. public static void AreEnumerationClones(IEnumerable original, object clone, Action typeSpecificAsserts) { if (typeSpecificAsserts == null) { throw new ArgumentNullException(nameof(typeSpecificAsserts)); } if (original == null && clone == null) { return; } Assert.IsNotNull(original); Assert.IsInstanceOf>(clone); Assert.AreNotSame(original, clone); var observable = original as IObservable; if (observable != null) { Assert.AreNotSame(observable.Observers, ((IObservable) clone).Observers); } CollectionAssert.AreEqual(original, (IEnumerable) clone, new AreClonesComparer(typeSpecificAsserts)); } private class AreClonesComparer : IComparer { private readonly Action typeSpecificAsserts; public AreClonesComparer(Action typeSpecificAsserts) { this.typeSpecificAsserts = typeSpecificAsserts; } public int Compare(object x, object y) { AreObjectClones((T) x, y, typeSpecificAsserts); return 0; } } } }