Chuong
09/16/2024, 3:37 AMclass ImageToTextResponse
private constructor(
val errorId: ErrorId,
val status: Status,
val solution: ImageToTextSolution,
val taskId: TaskId
) {
companion object {
operator fun invoke(obj: JsonObject): EitherNel<Throwable, ImageToTextResponse> {
fun Raise<Throwable>.mkSolution(): ImageToTextSolution {
val solutionJson = obj.getValue("solution").jsonObject
val confidence = Confidence(solutionJson.getValue("confidence").toString()).bind()
val text = CaptchaText(solutionJson.getValue("text").toString()).bind()
return ImageToTextSolution(confidence, text)
}
return either {
zipOrAccumulate(
{ ErrorId(obj.getValue("errorId").toString()).bind() },
{ Status(obj.getValue("status").toString()).bind() },
{ mkSolution() },
{ TaskId(obj.getValue("taskId").toString()).bind() }) { eid, status, solution, tid ->
ImageToTextResponse(eid, status, solution, tid)
}
}
}
}
}
I wrote a kotest
FunSpec
test for it.
test("Missing errorId in Json gives Left of ImageToTextResponse") {
Arb.bind(genMaybeErrorId, genMaybeStatus, genMaybeImageToTextSolution, genMaybeTaskId) { a, b, c, d -> Tuple4(a, b, c, d) }
.forAll { (_, b, c, d) ->
either {
val status = b.bind()
val solution = c.bind()
val taskId = d.bind()
buildJsonObject {
put("status", Json.encodeToJsonElement(status.value))
put("solution", Json.encodeToJsonElement(solution))
put("taskId", Json.encodeToJsonElement(taskId.value))
}
}
.mapLeft { nonEmptyListOf(Throwable()) }
.flatMap { ImageToTextResponse(it) }
.isLeft() shouldBe true
}
}
When I run the test, I get the error
Cannot create an instance of the class ImageToTextResponse. Specs must have a public zero-arg constructor.
I've created tests for `value class`es in this way. What is different this time? Does ImageToTextResponse
being a class completely explain this? Why?Chris Lee
09/16/2024, 3:45 AMChuong
09/16/2024, 3:54 AMChris Lee
09/16/2024, 3:57 AMChuong
09/16/2024, 3:59 AMImageToTextResponse
is just a class. That is the only notable difference between my failing tests and the working ones that I can see, @Chris Lee.Chuong
09/17/2024, 4:07 AMkotest
or the code.