From a0559c228e5ca547729cd8d7c195f8a2f4fc1efc Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 16 Aug 2024 09:34:52 -0700 Subject: [PATCH] added parameter checking to Get --- Consequences/Hazards/DepthHazard.cs | 11 +++++++++-- ConsequencesTest/DepthHazardTest.cs | 22 +++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/Consequences/Hazards/DepthHazard.cs b/Consequences/Hazards/DepthHazard.cs index a6d4c9d..a84dc0f 100644 --- a/Consequences/Hazards/DepthHazard.cs +++ b/Consequences/Hazards/DepthHazard.cs @@ -21,12 +21,19 @@ public bool Has(HazardParameter hp) public T Get(HazardParameter hp) { // return ((T?)_depth).GetValueOrDefault(); + + if (!Has(hp)) + { + throw new NotSupportedException(); + } if (typeof(T) == typeof(float)) { return (T)(object)_depth; } - - throw new NotSupportedException(); + else + { + throw new InvalidCastException(); + } } } diff --git a/ConsequencesTest/DepthHazardTest.cs b/ConsequencesTest/DepthHazardTest.cs index d32b1a5..8ffeb51 100644 --- a/ConsequencesTest/DepthHazardTest.cs +++ b/ConsequencesTest/DepthHazardTest.cs @@ -1,15 +1,31 @@ using USACE.HEC.Hazards; +using Xunit.Sdk; namespace ConsequencesTest; public class DepthHazardTest { [Fact] - public void Test() + public void TestInterfaceImplementation() { - DepthHazard dh = new DepthHazard(1.01f); + float input = 1.01f; + IHazard dh = new DepthHazard(input); bool has = dh.Has(HazardParameter.Depth); - float depth = dh.Get(HazardParameter.Depth); + if (has) + { + float depth = dh.Get(HazardParameter.Depth); + Assert.Equal(input, depth); + } else + { + throw new Exception("Failed to find appropriate parameter"); + } + } + + [Fact] + public void TestGetWrongParameter() + { + IHazard dh = new DepthHazard(1.01f); + Assert.Throws(() => dh.Get(HazardParameter.ArrivalTime)); } }