Ambiguous implicit conversions causing compilation failure in Scalatest and Argonaut.io

I am currently doing this noblest programming by writing tests for Json encoding / decoding. I use Argonaut.io for Json and Scalatest for my testing platform. With a scatbet, using === during verification validation provides additional information if a failure occurs, while using == just gives it org.scalatest.exceptions.TestFailedException was thrown. . However, the scala compiler is not happy. Here is the code:

 val default = new Broadcast("default", "default", "default") test("Should parse out network when present") { val hcursor = testHCursor(jsonPath + "complete-broadcast.json") val actualNetwork = Parser.BroadcastDecodeJson(hcursor) .getOr(default) .network assert(actualNetwork === "ESPNU") } 

It spews this out:

 [info] Compiling 1 Scala source to /home/vagrant/waltercamp/waltercamp-dataservice/target/scala-2.10/test-classes... [error] /home/vagrant/waltercamp/waltercamp-dataservice/src/test/scala/io/ptx/waltercamp/schedules/BroadcastParserSuite.scala:16: type mismatch; [error] found : actualNetwork.type (with underlying type String) [error] required: ?{def ===(x$1: ? >: String("ESPNU")): ?} [error] Note that implicit conversions are not applicable because they are ambiguous: [error] both method ToEqualOps in trait ToEqualOps of type [F](v: F)(implicit F0: scalaz.Equal[F])scalaz.syntax.EqualOps[F] [error] and method convertToEqualizer in trait Assertions of type (left: Any)BroadcastParserSuite.this.Equalizer [error] are possible conversion functions from actualNetwork.type to ?{def ===(x$1: ? >: String("ESPNU")): ?} [error] assert(actualNetwork === "ESPNU") [error] ^ [error] one error found [error] (test:compile) Compilation failed 

However, using == provides a clean compilation and transfer. Is there a way to give the compiler a hint about which transform or transform order to use?

+5
source share
1 answer

I would go with the ScalaTest version. One approach would be to explicitly apply the transformation:

 assert(convertToEqualizer(actualNetwork) === "ESPNU") 
However, this is unpleasant and requires a lot of repeating pattern if you use === many times in the file. Another way would be to exclude the Scalaz conversion from general imports:
 import scalaz._, Scalaz.{ ToEqualOps => _, _ } 

You can also switch to à la carte imports for Scalaz and just make sure you don't pull ToEqualOps through scala.syntax.equal._ . I admit that I find that à la carte imports pain to support it sometimes, but if you do not do much with Scalaz in the test, it will not be so bad.

+2
source

Source: https://habr.com/ru/post/1201030/


All Articles