-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce munit-cats-effect #4522
Merged
shinyhappydan
merged 3 commits into
BlueBrain:master
from
shinyhappydan:cats-effect-munit
Nov 24, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 45 additions & 46 deletions
91
...c/test/scala/ch/epfl/bluebrain/nexus/delta/kernel/utils/ClasspathResourceLoaderSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,65 @@ | ||
package ch.epfl.bluebrain.nexus.delta.kernel.utils | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits._ | ||
import ch.epfl.bluebrain.nexus.delta.kernel.utils.ClasspathResourceError.{InvalidJson, InvalidJsonObject} | ||
import io.circe.syntax._ | ||
import io.circe.{Json, JsonObject} | ||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpecLike | ||
import munit.CatsEffectSuite | ||
|
||
class ClasspathResourceLoaderSpec extends AnyWordSpecLike with Matchers with ScalaFutures { | ||
class ClasspathResourceLoaderSpec extends CatsEffectSuite { | ||
private val loader: ClasspathResourceLoader = ClasspathResourceLoader() | ||
|
||
private def accept[A](io: IO[A]): A = | ||
io.attempt.unsafeRunSync() match { | ||
case Left(value) => fail(s"Expected Right, found Left with value '$value'") | ||
case Right(value) => value | ||
} | ||
|
||
private def reject[A](io: IO[A]): Throwable = | ||
io.attempt.unsafeRunSync() match { | ||
case Left(value) => value | ||
case Right(value) => fail(s"Expected Left, found Right with value '$value'") | ||
} | ||
|
||
"A ClasspathResourceUtils" should { | ||
val resourceIO = loader.contentOf("resource.txt", "value" -> "v") | ||
test("return the path") { | ||
loader.absolutePath("resource.txt").assert(_.endsWith("resource.txt")) | ||
} | ||
|
||
"return the path" in { | ||
accept(loader.absolutePath("resource.txt")) should endWith("resource.txt") | ||
} | ||
test("return the contents of a handlebar template") { | ||
loader | ||
.contentOf("resource.txt", "value" -> "v") | ||
.assertEquals("A text resource with replacement 'v'") | ||
} | ||
|
||
"return a text" in { | ||
accept(resourceIO) shouldEqual "A text resource with replacement 'v'" | ||
} | ||
test("return the contents of a handlebar template, multiple times") { | ||
val io = loader.contentOf("resource.txt", "value" -> "v") | ||
|
||
"read a second time" in { | ||
accept(resourceIO) shouldEqual "A text resource with replacement 'v'" | ||
for { | ||
_ <- io.assertEquals("A text resource with replacement 'v'") | ||
_ <- io.assertEquals("A text resource with replacement 'v'") | ||
} yield { | ||
() | ||
} | ||
} | ||
|
||
"return a json" in { | ||
accept(loader.jsonContentOf("resource.json", "value" -> "v")) shouldEqual Json.obj("k" -> "v".asJson) | ||
} | ||
test("return the contents of a handlebar template as json") { | ||
loader | ||
.jsonContentOf("resource.json", "value" -> "v") | ||
.assertEquals(Json.obj("k" -> "v".asJson)) | ||
} | ||
|
||
"return a json object" in { | ||
accept(loader.jsonObjectContentOf("resource.json", "value" -> "v")) shouldEqual JsonObject("k" -> "v".asJson) | ||
} | ||
test("fail when a file cannot be parsed as json") { | ||
loader | ||
.jsonContentOf("resource.txt", "value" -> "v") | ||
.intercept[InvalidJson] | ||
} | ||
|
||
"fail when resource is not a json" in { | ||
reject(loader.jsonContentOf("resource.txt")) shouldBe a[InvalidJson] | ||
} | ||
test("return the contaents of a handlebar template as a json object") { | ||
loader | ||
.jsonObjectContentOf("resource.json", "value" -> "v") | ||
.assertEquals(JsonObject("k" -> "v".asJson)) | ||
} | ||
|
||
"fail when resource is not a json object" in { | ||
reject(loader.jsonObjectContentOf("resource-json-array.json")) shouldEqual InvalidJsonObject( | ||
"resource-json-array.json" | ||
test("fail when a file contains JSON but is not a json object") { | ||
loader | ||
.jsonObjectContentOf("resource-json-array.json") | ||
.intercept[InvalidJsonObject] | ||
.assertEquals( | ||
InvalidJsonObject("resource-json-array.json") | ||
) | ||
} | ||
} | ||
|
||
"fail when resource does not exists" in { | ||
reject(loader.contentOf("resource2.txt", "value" -> "v")).getMessage should (include("not found") and include( | ||
"resource2.txt" | ||
)) | ||
} | ||
test("fail when a resource does not exist") { | ||
loader | ||
.contentOf("resource2.txt", "value" -> "v") | ||
.intercept[Throwable] | ||
.assert(e => e.getMessage.contains("not found") && e.getMessage.contains("resource2.txt")) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a new change and I'm not sure about asserting on library errors anyway, but if we are gonna do that could do something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote it this way because the message changed slightly when I switched libraries and I wanted the assertion to be a bit looser (say that it wasn't found, say the name of the thing)