From 9c9784a13a978e4168bbf474330b01b74a331882 Mon Sep 17 00:00:00 2001 From: Gabriel Ittner Date: Tue, 17 Oct 2023 10:48:16 +0200 Subject: [PATCH] add deep links test helper --- deeplinks-plugin/deeplinks-plugin.gradle.kts | 2 +- .../plugin/DeeplinksManifestConfigurator.kt | 4 +++ .../navigation-testing.gradle.kts | 27 ++++++++++++++---- .../khonshu/navigation/NavigatorTurbine.kt | 0 .../khonshu/navigation/ResultOwnerTesting.kt | 0 .../navigation/TestNavEventCollector.kt | 0 .../deeplinks}/DeepLinkDefinitions.kt | 4 +-- .../navigation/deeplinks/DeepLinkMatcher.kt | 28 +++++++++++++++++++ 8 files changed, 55 insertions(+), 10 deletions(-) rename navigation-testing/src/{main => androidMain}/kotlin/com/freeletics/khonshu/navigation/NavigatorTurbine.kt (100%) rename navigation-testing/src/{main => androidMain}/kotlin/com/freeletics/khonshu/navigation/ResultOwnerTesting.kt (100%) rename navigation-testing/src/{main => androidMain}/kotlin/com/freeletics/khonshu/navigation/TestNavEventCollector.kt (100%) rename {deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin => navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks}/DeepLinkDefinitions.kt (90%) create mode 100644 navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkMatcher.kt diff --git a/deeplinks-plugin/deeplinks-plugin.gradle.kts b/deeplinks-plugin/deeplinks-plugin.gradle.kts index e0c430576..4d4d1789a 100644 --- a/deeplinks-plugin/deeplinks-plugin.gradle.kts +++ b/deeplinks-plugin/deeplinks-plugin.gradle.kts @@ -7,7 +7,7 @@ plugins { dependencies { implementation(libs.toml) implementation(libs.serialization) - implementation(projects.navigation) + implementation(projects.navigationTesting) compileOnly(libs.android.gradle.api) diff --git a/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeeplinksManifestConfigurator.kt b/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeeplinksManifestConfigurator.kt index 4ff64bac0..2a061f24e 100644 --- a/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeeplinksManifestConfigurator.kt +++ b/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeeplinksManifestConfigurator.kt @@ -1,5 +1,9 @@ package com.freeletics.khonshu.deeplinks.plugin +import com.freeletics.khonshu.navigation.deeplinks.DeepLinkDefinition +import com.freeletics.khonshu.navigation.deeplinks.DeepLinkDefinitions +import com.freeletics.khonshu.navigation.deeplinks.PatternDefinition +import com.freeletics.khonshu.navigation.deeplinks.PrefixDefinition import com.freeletics.khonshu.navigation.internal.InternalNavigationApi import java.io.File diff --git a/navigation-testing/navigation-testing.gradle.kts b/navigation-testing/navigation-testing.gradle.kts index 85d289b0c..0ec8576a1 100644 --- a/navigation-testing/navigation-testing.gradle.kts +++ b/navigation-testing/navigation-testing.gradle.kts @@ -1,16 +1,31 @@ plugins { - alias(libs.plugins.fgp.android) + alias(libs.plugins.fgp.multiplatform) alias(libs.plugins.fgp.publish) + alias(libs.plugins.kotlin.serialization) } freeletics { optIn("com.freeletics.khonshu.navigation.internal.InternalNavigationApi") + + multiplatform { + addJvmTarget() + addAndroidTarget() + } +} + +dependencies { } dependencies { - api(projects.navigation) - api(libs.coroutines.core) - api(libs.turbine) - implementation(libs.androidx.activity) - implementation(libs.truth) + "commonMainApi"(projects.navigation) + "commonMainApi"(libs.kotlin.test) + + "commonMainImplementation"(libs.toml) + "commonMainImplementation"(libs.serialization) + + "androidMainApi"(libs.coroutines.core) + "androidMainApi"(libs.turbine) + + "androidMainImplementation"(libs.androidx.activity) + "androidMainImplementation"(libs.truth) } diff --git a/navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/NavigatorTurbine.kt b/navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/NavigatorTurbine.kt similarity index 100% rename from navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/NavigatorTurbine.kt rename to navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/NavigatorTurbine.kt diff --git a/navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/ResultOwnerTesting.kt b/navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/ResultOwnerTesting.kt similarity index 100% rename from navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/ResultOwnerTesting.kt rename to navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/ResultOwnerTesting.kt diff --git a/navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/TestNavEventCollector.kt b/navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/TestNavEventCollector.kt similarity index 100% rename from navigation-testing/src/main/kotlin/com/freeletics/khonshu/navigation/TestNavEventCollector.kt rename to navigation-testing/src/androidMain/kotlin/com/freeletics/khonshu/navigation/TestNavEventCollector.kt diff --git a/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeepLinkDefinitions.kt b/navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkDefinitions.kt similarity index 90% rename from deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeepLinkDefinitions.kt rename to navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkDefinitions.kt index 8e9e31e61..b3647ff37 100644 --- a/deeplinks-plugin/src/main/kotlin/com/freeletics/khonshu/deeplinks/plugin/DeepLinkDefinitions.kt +++ b/navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkDefinitions.kt @@ -1,7 +1,5 @@ -package com.freeletics.khonshu.deeplinks.plugin +package com.freeletics.khonshu.navigation.deeplinks -import com.freeletics.khonshu.navigation.deeplinks.DeepLinkHandler -import com.freeletics.khonshu.navigation.deeplinks.replacePlaceholders import com.freeletics.khonshu.navigation.internal.InternalNavigationApi import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString diff --git a/navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkMatcher.kt b/navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkMatcher.kt new file mode 100644 index 000000000..5fff7ff5d --- /dev/null +++ b/navigation-testing/src/commonMain/kotlin/com/freeletics/khonshu/navigation/deeplinks/DeepLinkMatcher.kt @@ -0,0 +1,28 @@ +package com.freeletics.khonshu.navigation.deeplinks + +import kotlin.test.assertTrue + +public fun Set.matchAllDeepLinks(defaultPrefixes: Set) { + val codePrefixPatternCombinations = flatMapTo(HashSet()) { handler -> + handler.prefixes.ifEmpty { defaultPrefixes }.flatMap { prefix -> + handler.patterns.map { pattern -> + prefix to pattern + } + } + } + + val definitions = DeepLinkDefinitions.decodeFromString("") + val definedPrefixPatternCombinations = definitions.deepLinks.values.flatMapTo(HashSet()) { deepLink -> + deepLink.prefixes.ifEmpty { definitions.prefixes }.flatMap { prefix -> + deepLink.patterns.map { pattern -> + DeepLinkHandler.Prefix("${prefix.scheme}://${prefix.host}") to + DeepLinkHandler.Pattern(pattern.value) + } + } + } + + val codeOnly = codePrefixPatternCombinations - definedPrefixPatternCombinations + assertTrue(codeOnly.isEmpty(), "The following deep links are not defined in TOML but are present in code: $codeOnly") + val tomlOnly = definedPrefixPatternCombinations - codePrefixPatternCombinations + assertTrue(tomlOnly.isEmpty(), "The following deep links are not defined in code but are present in the TOML file: $tomlOnly") +}