Skip to content

Commit

Permalink
Handling period durations of days with T suffix (#80)
Browse files Browse the repository at this point in the history
* Improved test

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Added more fix cases

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>

* Use assert signature

---------

Signed-off-by: Arnau Mora <arnyminer.z@gmail.com>
Co-authored-by: Ricki Hirner <hirner@bitfire.at>
  • Loading branch information
ArnyminerZ and rfc2822 committed Feb 8, 2023
1 parent c39b454 commit 1c6bcb3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ package at.bitfire.ical4android.validation

/**
* Fixes durations with day offsets with the 'T' prefix.
* See also https://github.com/bitfireAT/icsx5/issues/100
* See also https://github.com/bitfireAT/ical4android/issues/77
*/
object FixInvalidDayOffsetPreprocessor : StreamPreprocessor() {

override fun regexpForProblem() = Regex(
"^(DURATION|TRIGGER):-?PT-?\\d+D$",
// Examples:
// TRIGGER:-P2DT
// TRIGGER:-PT2D
"^(DURATION|TRIGGER):-?P((T-?\\d+D)|(-?\\d+DT))\$",
setOf(RegexOption.MULTILINE, RegexOption.IGNORE_CASE)
)

Expand All @@ -21,7 +24,9 @@ object FixInvalidDayOffsetPreprocessor : StreamPreprocessor() {
// Find all matches for the expression
val found = regexpForProblem().find(s) ?: return s
for (match in found.groupValues) {
val fixed = match.replace("PT", "P")
val fixed = match
.replace("PT", "P")
.replace("DT", "D")
s = s.replace(match, fixed)
}
return s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ package at.bitfire.ical4android.validation

import org.junit.Assert.*
import org.junit.Test
import java.time.Duration

class FixInvalidDayOffsetPreprocessorTest {

private fun fixStringAndAssert(expected: String, string: String) {
val fixed = FixInvalidDayOffsetPreprocessor.fixString(string)
Duration.parse(fixed.substring(fixed.indexOf(':') + 1))
assertEquals(expected, fixed)
}

@Test
fun test_FixString_NoOccurrence() {
assertEquals(
Expand All @@ -19,26 +26,17 @@ class FixInvalidDayOffsetPreprocessorTest {

@Test
fun test_FixString_DayOffsetFrom_Invalid() {
assertEquals(
"DURATION:-P1D",
FixInvalidDayOffsetPreprocessor.fixString("DURATION:-PT1D"),
)
assertEquals(
"TRIGGER:-P2D",
FixInvalidDayOffsetPreprocessor.fixString("TRIGGER:-PT2D"),
)
fixStringAndAssert("DURATION:-P1D", "DURATION:-PT1D")
fixStringAndAssert("TRIGGER:-P2D", "TRIGGER:-PT2D")

fixStringAndAssert("DURATION:-P1D", "DURATION:-P1DT")
fixStringAndAssert("TRIGGER:-P2D", "TRIGGER:-P2DT")
}

@Test
fun test_FixString_DayOffsetFrom_Valid() {
assertEquals(
"DURATION:-PT12H",
FixInvalidDayOffsetPreprocessor.fixString("DURATION:-PT12H"),
)
assertEquals(
"TRIGGER:-PT12H",
FixInvalidDayOffsetPreprocessor.fixString("TRIGGER:-PT12H"),
)
fixStringAndAssert("DURATION:-PT12H", "DURATION:-PT12H")
fixStringAndAssert("TRIGGER:-PT12H", "TRIGGER:-PT12H")
}

@Test
Expand Down

0 comments on commit 1c6bcb3

Please sign in to comment.