@@ -4,6 +4,7 @@ import cafe.adriel.voyager.navigator.CurrentScreen
4
4
import cafe.adriel.voyager.navigator.Navigator
5
5
import dev.programadorthi.routing.core.RouteMethod
6
6
import dev.programadorthi.routing.core.application.ApplicationCall
7
+ import dev.programadorthi.routing.core.application.call
7
8
import dev.programadorthi.routing.core.application.createApplicationPlugin
8
9
import dev.programadorthi.routing.core.application.hooks.CallFailed
9
10
import dev.programadorthi.routing.core.call
@@ -17,12 +18,12 @@ import dev.programadorthi.routing.voyager.helper.FakeScreen
17
18
import dev.programadorthi.routing.voyager.helper.runComposeTest
18
19
import io.ktor.http.Parameters
19
20
import io.ktor.http.parametersOf
20
- import kotlinx.coroutines.ExperimentalCoroutinesApi
21
- import kotlinx.coroutines.test.advanceTimeBy
22
21
import kotlin.test.Test
23
22
import kotlin.test.assertEquals
24
23
import kotlin.test.assertIs
25
24
import kotlin.test.assertNotNull
25
+ import kotlinx.coroutines.ExperimentalCoroutinesApi
26
+ import kotlinx.coroutines.test.advanceTimeBy
26
27
27
28
@OptIn(ExperimentalCoroutinesApi ::class )
28
29
internal class VoyagerRoutingTest {
@@ -150,10 +151,10 @@ internal class VoyagerRoutingTest {
150
151
// THEN
151
152
assertNotNull(result)
152
153
assertNotNull(exception)
153
- assertEquals(" /path" , " ${ result? .uri} " )
154
- assertEquals(" " , " ${ result? .name} " )
155
- assertEquals(RouteMethod .Empty , result? .routeMethod)
156
- assertEquals(Parameters .Empty , result? .parameters)
154
+ assertEquals(" /path" , result.uri)
155
+ assertEquals(" " , result.name)
156
+ assertEquals(RouteMethod .Empty , result.routeMethod)
157
+ assertEquals(Parameters .Empty , result.parameters)
157
158
assertIs<IllegalStateException >(exception)
158
159
assertEquals(
159
160
" Voyager needs a stack route method to work. You called a screen /path using " +
@@ -374,9 +375,9 @@ internal class VoyagerRoutingTest {
374
375
VoyagerRouting (
375
376
routing = routing,
376
377
initialScreen =
377
- FakeScreen ().apply {
378
- content = " I am the initial screen"
379
- },
378
+ FakeScreen ().apply {
379
+ content = " I am the initial screen"
380
+ },
380
381
) { nav ->
381
382
navigator = nav
382
383
CurrentScreen ()
@@ -444,9 +445,9 @@ internal class VoyagerRoutingTest {
444
445
VoyagerRouting (
445
446
routing = routing,
446
447
initialScreen =
447
- FakeScreen ().apply {
448
- content = " I am the initial screen"
449
- },
448
+ FakeScreen ().apply {
449
+ content = " I am the initial screen"
450
+ },
450
451
) { nav ->
451
452
navigator = nav
452
453
CurrentScreen ()
@@ -496,9 +497,9 @@ internal class VoyagerRoutingTest {
496
497
VoyagerRouting (
497
498
routing = routing,
498
499
initialScreen =
499
- FakeScreen ().apply {
500
- content = " I am the initial screen"
501
- },
500
+ FakeScreen ().apply {
501
+ content = " I am the initial screen"
502
+ },
502
503
) { nav ->
503
504
navigator = nav
504
505
CurrentScreen ()
@@ -528,4 +529,71 @@ internal class VoyagerRoutingTest {
528
529
// THEN
529
530
assertEquals(parametersOf(" key" to listOf (" value" )), firstPushedScreen?.parameters)
530
531
}
532
+
533
+ @Test
534
+ fun shouldNavigateByRegex () =
535
+ runComposeTest { coroutineContext, composition, clock ->
536
+ // GIVEN
537
+ val fakeScreen = FakeScreen ()
538
+
539
+ val routing =
540
+ routing(parentCoroutineContext = coroutineContext) {
541
+ screen(path = Regex (" /(?<number>\\ d+)" )) {
542
+ fakeScreen.apply {
543
+ content = " Hey, I am the called screen with number ${call.parameters[" number" ]} "
544
+ }
545
+ }
546
+ }
547
+
548
+ composition.setContent {
549
+ VoyagerRouting (
550
+ routing = routing,
551
+ initialScreen = FakeScreen (),
552
+ )
553
+ }
554
+
555
+ // WHEN
556
+ routing.push(path = " /123" )
557
+ advanceTimeBy(99 ) // Ask for routing
558
+ clock.sendFrame(0L ) // Ask for recomposition
559
+
560
+ // THEN
561
+ assertEquals(" Hey, I am the called screen with number 123" , fakeScreen.composed)
562
+ }
563
+
564
+ @Test
565
+ fun shouldNavigateByRegexWithMultipleParameters () =
566
+ runComposeTest { coroutineContext, composition, clock ->
567
+ // GIVEN
568
+ val fakeScreen = FakeScreen ()
569
+
570
+ val routing =
571
+ routing(parentCoroutineContext = coroutineContext) {
572
+ route(path = Regex (" /(?<number>\\ d+)" )) {
573
+ screen(path = Regex (" (?<user>\\ w+)/(?<login>.+)" )) {
574
+ fakeScreen.apply {
575
+ content = " Hey, I am the called screen with ${call.parameters} "
576
+ }
577
+ }
578
+ }
579
+ }
580
+
581
+ composition.setContent {
582
+ VoyagerRouting (
583
+ routing = routing,
584
+ initialScreen = FakeScreen (),
585
+ )
586
+ }
587
+
588
+ // WHEN
589
+ routing.push(path = " /456/qwe/rty" )
590
+ advanceTimeBy(99 ) // Ask for routing
591
+ clock.sendFrame(0L ) // Ask for recomposition
592
+
593
+ // THEN
594
+ assertEquals(
595
+ " Hey, I am the called screen with Parameters [number=[456], user=[qwe], login=[rty]]" ,
596
+ fakeScreen.composed
597
+ )
598
+ }
531
599
}
0 commit comments