Skip to content

Commit

Permalink
Add conveniences for Path.pathTo and test
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed Feb 9, 2024
1 parent 91bc87f commit da3028b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/jvm/src/test/scala/krop/route/PathSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,22 @@ class PathSuite extends FunSuite {
}

test("Path.pathTo produces expected path") {
assertEquals(nonCapturingPath.pathTo, "/user/create")
assertEquals(nonCapturingPath.pathTo(EmptyTuple), "/user/create")

assertEquals(nonCapturingAllPath.pathTo, "/assets/html/")
assertEquals(nonCapturingAllPath.pathTo(EmptyTuple), "/assets/html/")

assertEquals(
capturingAllPath.pathTo(Seq("css", "main.css")),
"/assets/html/css/main.css"
)
assertEquals(
capturingAllPath.pathTo(Tuple1(Vector("css", "main.css"))),
"/assets/html/css/main.css"
)

assertEquals(simplePath.pathTo(1234), "/user/1234/view")
assertEquals(simplePath.pathTo(Tuple1(1234)), "/user/1234/view")
}
}
10 changes: 10 additions & 0 deletions core/shared/src/main/scala/krop/route/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ final class Path[P <: Tuple, Q <: Tuple] private (
// Interpreters --------------------------------------------------------------
//

/** Overload of `pathTo` for the case where the path has no parameters.
*/
def pathTo(using ev: EmptyTuple =:= P): String =
pathTo(ev(EmptyTuple))

/** Overload of `pathTo` for the case where the path has a single parameter.
*/
def pathTo[B](param: B)(using ev: Tuple1[B] =:= P): String =
pathTo(ev(Tuple1(param)))

/** Create a `String` that links to this path with the given parameters. For
* example, with the path
*
Expand Down

0 comments on commit da3028b

Please sign in to comment.