Update stopwatch string
SuccessfulCode/BritishHorseRacingApi/master This commit looks good

This commit is contained in:
2020-02-27 22:38:55 +00:00
parent f1bbe045fb
commit 1a8bc774b0
2 changed files with 15 additions and 0 deletions
@@ -22,6 +22,19 @@ namespace BritishHorseRacingApi.Tests
Assert.Equal(milliseconds, timeSpan.Milliseconds); Assert.Equal(milliseconds, timeSpan.Milliseconds);
} }
[Theory()]
[InlineData("", null)]
[InlineData(null, null)]
[InlineData(" ", null)]
public void TestStopWatchStringConversionForEmpty(string time, double? expectedValue)
{
//Arrange
//Act
var totalMilliseconds = DataConverterHelper.StopWatchStringToMilliseconds(time);
//Assert
Assert.Equal(totalMilliseconds, expectedValue);
}
[Theory()] [Theory()]
[InlineData("1m 1f 104y", 2084)] [InlineData("1m 1f 104y", 2084)]
[InlineData("1f 104y", 324)] [InlineData("1f 104y", 324)]
@@ -9,6 +9,8 @@ namespace BritishHorseRacingApi.Helpers
private static Regex _stopwatchTimeRegex = new Regex(@"^(?'minutes'[0-9]*(?=m))?m? ?(?'seconds'[0-9\.]*)s ?(?:\(\+)?(?'lossDifference'[0-9\.]*)(?:s\))?", RegexOptions.Compiled); private static Regex _stopwatchTimeRegex = new Regex(@"^(?'minutes'[0-9]*(?=m))?m? ?(?'seconds'[0-9\.]*)s ?(?:\(\+)?(?'lossDifference'[0-9\.]*)(?:s\))?", RegexOptions.Compiled);
public static int? StopWatchStringToMilliseconds(string stopwatchValue) public static int? StopWatchStringToMilliseconds(string stopwatchValue)
{ {
if (String.IsNullOrWhiteSpace(stopwatchValue))
return null;
var match = _stopwatchTimeRegex.Match(stopwatchValue); var match = _stopwatchTimeRegex.Match(stopwatchValue);
var minutes = GetComponent(match, "minutes", 0); var minutes = GetComponent(match, "minutes", 0);
var secondsAndMilliseconds = GetComponent(match, "seconds", "0").Split('.'); var secondsAndMilliseconds = GetComponent(match, "seconds", "0").Split('.');