Skip to content

Commit

Permalink
adds test for truncated data handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sprintf committed Feb 25, 2024
1 parent 6df4029 commit c582591
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,23 @@ class DataSourceHandler(private val leaderboard: RaceOrder,


internal fun convertToSeconds(rawTime: String): Double {
val timeFields = rawTime.trim('"').split(':')
val hours = timeFields[0].toInt()
val minutes = timeFields[1].toInt()
val seconds = when ("." in timeFields[2]) {
true -> {
val fields = timeFields[2].split(".")
fields[0].toInt() + fields[1].toInt() / 1000.0
try {
val timeFields = rawTime.trim('"').split(':')
val hours = timeFields[0].toInt()
val minutes = timeFields[1].toInt()
val seconds = when ("." in timeFields[2]) {
true -> {
val fields = timeFields[2].split(".")
fields[0].toInt() + fields[1].toInt() / 1000.0
}
false -> timeFields[2].toDouble()
}
false -> timeFields[2].toDouble()
return hours * 3600.0 +
minutes * 60.0 +
seconds
} catch (e: IndexOutOfBoundsException) {
throw DateTimeParseException(e.message, rawTime, 0)
}
return hours * 3600.0 +
minutes * 60.0 +
seconds
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import java.io.BufferedReader
import java.io.FileReader
import java.time.format.DateTimeParseException

internal class DataSourceHandlerTest {

Expand All @@ -25,6 +26,14 @@ internal class DataSourceHandlerTest {
assertEquals(97335.123, ds.convertToSeconds("27:02:15.123"))
}

@Test
fun testTimeTruncated() {
val ds = DataSourceHandler(RaceOrder(), "thil", 0, setOf())
assertThrows(DateTimeParseException::class.java) {
ds.convertToSeconds("27:0")
}
}

class TestHandler: EventHandler {

val callbackCount: MutableMap<String, Int> = mutableMapOf()
Expand Down

0 comments on commit c582591

Please sign in to comment.