Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 1c6bcb3f authored by Arnau Mora's avatar Arnau Mora Committed by GitHub
Browse files

Handling period durations of days with `T` suffix (#80)



* Improved test

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

* Added more fix cases

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

* Use assert signature

---------

Signed-off-by: default avatarArnau Mora <arnyminer.z@gmail.com>
Co-authored-by: default avatarRicki Hirner <hirner@bitfire.at>
parent c39b4544
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -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)
    )

@@ -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
+14 −16
Original line number Diff line number Diff line
@@ -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(
@@ -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