Index: DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Standard/TypeExtensionsTests.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Standard/TypeExtensionsTests.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Data.Tests/Standard/TypeExtensionsTests.cs (revision 2192) @@ -0,0 +1,48 @@ +// Copyright (C) Stichting Deltares 2019. All rights reserved. +// +// This file is part of the Dam Engine. +// +// The Dam Engine is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero 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 Deltares.DamEngine.Data.General; +using Deltares.DamEngine.Data.Standard; +using NUnit.Framework; + +namespace Deltares.DamEngine.Data.Tests.Standard +{ + /// Test the TypeExtensions + [TestFixture] + public class TypeExtensionsTests + { + [Test] + [TestCase(SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.Stability)] + [TestCase(SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.All)] + public void GivenValueAndSetWhenCheckingIfValueIsInSetThenReturnsTrue(SegmentFailureMechanismType val, params SegmentFailureMechanismType[] values) + { + Assert.IsTrue(val.In(values)); + } + + [Test] + [TestCase(SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.Piping)] + [TestCase(SegmentFailureMechanismType.Stability, SegmentFailureMechanismType.Piping, SegmentFailureMechanismType.All)] + public void GivenValueAndSetWhenCheckingIfValueIsNotInSetThenReturnsFalse(SegmentFailureMechanismType val, params SegmentFailureMechanismType[] values) + { + Assert.IsFalse(val.In(values)); + } + } +} Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/TypeExtensions.cs =================================================================== diff -u -r1974 -r2192 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/TypeExtensions.cs (.../TypeExtensions.cs) (revision 1974) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/TypeExtensions.cs (.../TypeExtensions.cs) (revision 2192) @@ -20,17 +20,29 @@ // All rights reserved. using System; -using System.Reflection; +using System.Linq; namespace Deltares.DamEngine.Data.Standard { public static class TypeExtensions { + /// Check if a type has a default constructor + /// public static bool HasDefaultConstructor(this Type type) { if (type.IsValueType) return true; - return type.GetConstructor(Type.EmptyTypes) != (ConstructorInfo)null; + return type.GetConstructor(Type.EmptyTypes) != null; } + + /// Check if a value (val) of a type is part of a defined set (values) + /// + /// The value. + /// The values. + /// + public static bool In(this T val, params T[] values) where T : struct + { + return values.Contains(val); + } } }