Skip to content

Commit

Permalink
added parameter checking to Get
Browse files Browse the repository at this point in the history
  • Loading branch information
jackschonherr committed Aug 16, 2024
1 parent 6d63b42 commit a0559c2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
11 changes: 9 additions & 2 deletions Consequences/Hazards/DepthHazard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ public bool Has(HazardParameter hp)
public T Get<T>(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();
}
}
}
22 changes: 19 additions & 3 deletions ConsequencesTest/DepthHazardTest.cs
Original file line number Diff line number Diff line change
@@ -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<float>(HazardParameter.Depth);
if (has)
{
float depth = dh.Get<float>(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<NotSupportedException>(() => dh.Get<float>(HazardParameter.ArrivalTime));

}
}

0 comments on commit a0559c2

Please sign in to comment.