Skip to content

Commit

Permalink
Merge pull request #233 from alejandrohdezma/feature/name-creator
Browse files Browse the repository at this point in the history
Add `http4sMUnitNameCreatorReplacements` to simplify editing test names
  • Loading branch information
alejandrohdezma authored Jun 17, 2022
2 parents 515e729 + 3901787 commit 25abc26
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ abstract class Http4sAuthedRoutesSuite[A: Show] extends Http4sSuite[AuthedReques
followingRequests: List[String],
testOptions: TestOptions,
config: Http4sMUnitConfig
): String = Http4sMUnitDefaults.http4sMUnitNameCreator(request, followingRequests, testOptions, config)
): String = Http4sMUnitDefaults.http4sMUnitNameCreator(
request,
followingRequests,
testOptions,
config,
http4sMUnitNameCreatorReplacements()
)

implicit class Request2AuthedRequest(request: Request[IO]) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ abstract class Http4sHttpRoutesSuite extends Http4sSuite[Request[IO]] {
testOptions: TestOptions,
config: Http4sMUnitConfig
): String =
Http4sMUnitDefaults.http4sMUnitNameCreator(ContextRequest((), request), followingRequests, testOptions, config)
Http4sMUnitDefaults.http4sMUnitNameCreator(
ContextRequest((), request),
followingRequests,
testOptions,
config,
http4sMUnitNameCreatorReplacements()
)

override def http4sMUnitFunFixture: SyncIO[FunFixture[Request[IO] => Resource[IO, Response[IO]]]] =
SyncIO.pure(FunFixture(_ => req => routes.orNotFound.run(req).to[Resource[IO, *]], _ => ()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ object Http4sMUnitDefaults {
request: ContextRequest[IO, A],
followingRequests: List[String],
testOptions: TestOptions,
config: Http4sMUnitConfig
config: Http4sMUnitConfig,
replacements: Seq[(String, String)] = Nil
): String = {
val clue = followingRequests.+:(testOptions.name).filter(_.nonEmpty) match {
case Nil => ""
Expand All @@ -49,7 +50,12 @@ object Http4sMUnitDefaults {
case _ => ""
}

s"${request.req.method.name} -> ${Uri.decode(request.req.uri.renderString)}$clue${context.fold("")(" as " + _)}$reps"
val nameWithoutReplacements = s"${request.req.method.name} -> ${Uri.decode(request.req.uri.renderString)}" +
s"$clue${context.fold("")(" as " + _)}$reps"

replacements.foldLeft(nameWithoutReplacements) { case (name, (value, replacement)) =>
name.replaceAll(value, replacement)
}
}

}
3 changes: 3 additions & 0 deletions modules/http4s-munit/src/main/scala/munit/Http4sSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ abstract class Http4sSuite[Request] extends CatsEffectSuite with Http4sDsl[IO] w
config: Http4sMUnitConfig
): String

/** List of replacements that will be applied to the result of `http4sMUnitNameCreator` using `String#replaceAll` */
def http4sMUnitNameCreatorReplacements(): Seq[(String, String)] = Nil

/** Returns the response as suite clues.
*
* This method is then used by `response.clues` extension method.
Expand Down
8 changes: 7 additions & 1 deletion modules/http4s-munit/src/main/scala/munit/HttpSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,13 @@ abstract class HttpSuite extends Http4sSuite[Request[IO]] with CatsEffectFunFixt
testOptions: TestOptions,
config: Http4sMUnitConfig
): String =
Http4sMUnitDefaults.http4sMUnitNameCreator(ContextRequest((), request), followingRequests, testOptions, config)
Http4sMUnitDefaults.http4sMUnitNameCreator(
ContextRequest((), request),
followingRequests,
testOptions,
config,
http4sMUnitNameCreatorReplacements()
)

/** This client is used under the hood to execute the requests. */
def http4sMUnitClient: Resource[IO, Client[IO]] = try
Expand Down

0 comments on commit 25abc26

Please sign in to comment.