Skip to content

Commit

Permalink
Moved composable annotation test to compose module
Browse files Browse the repository at this point in the history
Also improved some core route invocation extensions
  • Loading branch information
programadorthi committed Nov 20, 2024
1 parent 234fa76 commit 37cadef
Show file tree
Hide file tree
Showing 7 changed files with 852 additions and 107 deletions.
192 changes: 186 additions & 6 deletions core/common/src/dev/programadorthi/routing/core/RoutingExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package dev.programadorthi.routing.core

import io.ktor.http.Parameters

public fun Routing.push(path: String) {
push(path = path, parameters = Parameters.Empty)
}

public fun Routing.push(
path: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
uri = path,
Expand All @@ -13,9 +17,13 @@ public fun Routing.push(
)
}

public fun Routing.pushNamed(name: String) {
pushNamed(name = name, parameters = Parameters.Empty)
}

public fun Routing.pushNamed(
name: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
name = name,
Expand All @@ -24,9 +32,64 @@ public fun Routing.pushNamed(
)
}

public fun <T : Any> Routing.pushWithBody(
path: String,
body: T,
) {
pushWithBody(
path = path,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.pushWithBody(
path: String,
parameters: Parameters,
body: T,
) {
callWithBody(
uri = path,
parameters = parameters,
routeMethod = RouteMethod.Push,
body = body,
)
}

public fun <T : Any> Routing.pushNamedWithBody(
name: String,
body: T,
) {
pushNamedWithBody(
name = name,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.pushNamedWithBody(
name: String,
parameters: Parameters,
body: T,
) {
callWithBody(
name = name,
parameters = parameters,
routeMethod = RouteMethod.Push,
body = body,
)
}

public fun Routing.replace(path: String) {
replace(
path = path,
parameters = Parameters.Empty,
)
}

public fun Routing.replace(
path: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
uri = path,
Expand All @@ -35,9 +98,16 @@ public fun Routing.replace(
)
}

public fun Routing.replaceNamed(name: String) {
replaceNamed(
name = name,
parameters = Parameters.Empty,
)
}

public fun Routing.replaceNamed(
name: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
name = name,
Expand All @@ -46,9 +116,64 @@ public fun Routing.replaceNamed(
)
}

public fun <T : Any> Routing.replaceWithBody(
path: String,
body: T,
) {
replaceWithBody(
path = path,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.replaceWithBody(
path: String,
parameters: Parameters,
body: T,
) {
callWithBody(
uri = path,
parameters = parameters,
routeMethod = RouteMethod.Replace,
body = body,
)
}

public fun <T : Any> Routing.replaceNamedWithBody(
name: String,
body: T,
) {
replaceNamedWithBody(
name = name,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.replaceNamedWithBody(
name: String,
parameters: Parameters,
body: T,
) {
callWithBody(
name = name,
parameters = parameters,
routeMethod = RouteMethod.Replace,
body = body,
)
}

public fun Routing.replaceAll(path: String) {
replaceAll(
path = path,
parameters = Parameters.Empty,
)
}

public fun Routing.replaceAll(
path: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
uri = path,
Expand All @@ -57,13 +182,68 @@ public fun Routing.replaceAll(
)
}

public fun Routing.replaceAllNamed(name: String) {
replaceAllNamed(
name = name,
parameters = Parameters.Empty,
)
}

public fun Routing.replaceAllNamed(
name: String,
parameters: Parameters = Parameters.Empty,
parameters: Parameters,
) {
call(
name = name,
parameters = parameters,
routeMethod = RouteMethod.ReplaceAll,
)
}

public fun <T : Any> Routing.replaceAllWithBody(
path: String,
body: T,
) {
replaceAllWithBody(
path = path,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.replaceAllWithBody(
path: String,
parameters: Parameters,
body: T,
) {
callWithBody(
uri = path,
parameters = parameters,
routeMethod = RouteMethod.ReplaceAll,
body = body,
)
}

public fun <T : Any> Routing.replaceAllNamedWithBody(
name: String,
body: T,
) {
replaceAllNamedWithBody(
name = name,
parameters = Parameters.Empty,
body = body,
)
}

public fun <T : Any> Routing.replaceAllNamedWithBody(
name: String,
parameters: Parameters,
body: T,
) {
callWithBody(
name = name,
parameters = parameters,
routeMethod = RouteMethod.ReplaceAll,
body = body,
)
}
8 changes: 4 additions & 4 deletions core/common/test/dev/programadorthi/routing/core/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import io.ktor.http.Parameters
import io.ktor.util.Attributes
import kotlin.to

val invoked = mutableMapOf<String, List<Any?>>()
internal val invoked = mutableMapOf<String, List<Any?>>()

data class User(
internal data class User(
val id: Int,
val name: String,
)
Expand Down Expand Up @@ -59,14 +59,14 @@ fun regex2(number: Int) {
}

@Route("/with-body")
fun withBody(
internal fun withBody(
@Body user: User
) {
invoked += "/with-body" to listOf(user)
}

@Route("/with-null-body")
fun withNullBody(
internal fun withNullBody(
@Body user: User?
) {
invoked += "/with-null-body" to listOf(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import dev.programadorthi.routing.generated.configure
import io.ktor.http.Parameters
import io.ktor.http.parametersOf
import io.ktor.util.Attributes
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runTest

@OptIn(ExperimentalCoroutinesApi::class)
class RoutingByAnnotationsTest {
Expand Down Expand Up @@ -268,11 +268,11 @@ class RoutingByAnnotationsTest {
}

// WHEN
routing.call(uri = "/path", routeMethod = RouteMethod.Push)
routing.push(path = "/path")
advanceTimeBy(99)

// THEN
assertEquals(listOf("PUSH"), invoked.remove("/path"))
assertEquals(listOf(RouteMethod.Push.value), invoked.remove("/path"))
}

@Test
Expand Down
14 changes: 14 additions & 0 deletions integration/compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
kotlin("plugin.serialization")
alias(libs.plugins.jetbrains.compose)
alias(libs.plugins.compose.compiler)
alias(libs.plugins.ksp)
id("org.jetbrains.kotlinx.kover")
alias(libs.plugins.maven.publish)
}
Expand All @@ -19,5 +20,18 @@ kotlin {
implementation(libs.serialization.json)
}
}
commonTest {
dependencies {
implementation(projects.ksp.coreAnnotations)
}
}
}
}

dependencies {
add("kspJvmTest", projects.ksp.coreProcessor)
}

ksp {
arg("Routing_Module_Name", "Compose")
}
Loading

0 comments on commit 37cadef

Please sign in to comment.