ScalaTest v3: why require to implement convertToLegacyEqualizer - scala

Using ScalaTest 3.0.0
Environment: Scala 2.11.8, sbt 0.13.5, IntelliJ 14.1.4
build.sbt has only
// NOTE: not using org.scalactic
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"
The test below passed. However, IntelliJ marks a squiggly red line below MyMiniTest with the warning message:
Class 'MyMiniTest ' must either be declared abstract or implement
abstract member 'convertToLegacyEqualizer[T](left: T):
TripleEqualsSupport.this.LegacyEqualizer[T]' in
'org.scalactic.TripleEqualsSupport'
import org.scalatest.FeatureSpec
class MyMiniTest extends FeatureSpec {
scenario("A simple test") {
val a = 12
assert(a * 3 == 36)
}
}
What is the reason of this warning and what is the recommended solution to fix it?

I had the same problem on IntelliJ just follow this steps to invalidate cache/restart. This will solve the problem.

In my case it was a transitive dependency (don't know how a test library could appear as such) of a different version clashing with the dependency defined in my project. SBT knows how to deal with most of these cases, IntelliJ doesn't seem to know. Note that invalidating the cache and restarting IntelliJ wouldn't help in this case.
To be sure it's your case, check the following: File -> Project Structure -> [Project Settings - Libraries]. Look for org.scalatest:* and you will probably find two libraries, like this:
Then remove the unnecessary one by selecting it and pressing - at the top of the panel. That's it, IntelliJ will be happy now.
A cleaner solution would be to exclude the unnecessary library from your dependencies, e.g.:
ExclusionRule("org.scalatest", "scalatest_2.11-2.2.4")
IntelliJ will show the library among the project's dependencies, but will know that it should be ingored.

Please check all you dependencies and check if any of those dependencies is downloading org.scalatest.* . If the version of org.scalatest.* you have defined is different from the one getting downloaded due to other defined dependencies, this issue occurs.
I was using org.mockito%mockito-scala whose pom defined that scalatest 3.0.8 was provided. But the scalatest I had defined was 2.2.5. By changing the version of scalatest to 3.0.8, I was able to resolve this issue.
Hope this helps.

Related

Can scalameter be imported to scala3 projects (and how)?

I tried to use scalameter 0.21 (and some other versions) with scala 3.1.2.
I added some configurations from the scalameter web-page in my build.sbt file and tried serveral things to make this work, but sbt was not able to find the desired packages.
I would have concluded that scalameter is simply not available for scala3, but there is a question here where somebody somehow got it to work.
The problem seemed to bee that sbt added a 3 in all the sources, as in:
https://repo1.maven.org/maven2/com/storm-enroute/scalameter_3/0.21/scalameter_3-0.21.pom
but the path with scalameter_3 did not exist. If I change the scalaversion to 2.13.8, sbt is able to download all the nesessary files without error. Starting with scala version 3.0.0 the problem exists.
If somebody could post a build.sbt file where scalameter and scala3 are used together, I would apreciate it.
Otherwise, if somebody knows a different library for benchmarking with scala 3...
Thanks very much
As #maxkar mentioned, using crossVersion should do the job:
("com.storm-enroute" %% "scalameter" % "0.21").cross(CrossVersion.for3Use2_13) % Test
However, doing so, you may encounter a problem with conflicting dependencies, e.g.:
[error] Modules were resolved with conflicting cross-version suffixes in ProjectRef(...
[error] org.scala-lang.modules:scala-xml _2.13, _3
In such a case, dependency exclusion, as described in sbt documentation, should solve the issue:
("com.storm-enroute" %% "scalameter" % "0.21").cross(CrossVersion.for3Use2_13) % Test exclude("org.scala-lang.modules", "scala-xml_2.13")
I'm not sure if this cannot backfire in certain situations, but it worked perfectly in my case.

Scala "not found: object com" - should i really add entry in build.sbt if there are no other dependencies?

I have created basic Scala Play application with https://www.playframework.com/getting-started play-scala-seed. This project compiles and runs with sbt run. But I have another Scala project that compiles and runs and which I have submitted to my local Ivy repository with command sbt publishLocal. This other project was saved at C:\Users\tomr\.ivy2\local\com.agiintelligence\scala-isabelle_2.13\master-SNAPSHOT as a result of this command.
Then I imported (exactly so - imported, no just opened) my Play project in IntelliJ and I used Project - Open Module Settings - Project Settings - Libraries to add com.agiintelligence jar from my ivy2 location. After such operations IntelliJ editor recognizes com.agiintelligence classes. That is fine.
But when I am trying to run my Play application with sbt run, I experience the error message not found: object com that is exactly when compiling import com.agiintelligence line in my Scala controller file of Play application.
Of course - such error has been reported and resolved with, e.g. object play not found in scala application
But that solution suggests to append build.sbt file. My build.sbt file is pretty bare:
name := """agiintelligence"""
organization := "com.agiintelligence"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.13.5"
libraryDependencies += guice
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "5.0.0" % Test
// Adds additional packages into Twirl
//TwirlKeys.templateImports += "com.skaraintelligence.controllers._"
// Adds additional packages into conf/routes
// play.sbt.routes.RoutesKeys.routesImport += "com.skaraintelligence.binders._"
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries' (it shows my com.agiintelligence jar as well), but why should I add my own ivy2 library in build.sbt file if no other libraries are listed here? What is different with my library? It is on my computer, in the repository as expected already?
Of course, I can try to add it build.sbt and issue sbt update and see what happens, but I can not understand this logic? Can someone explain it and provide some clue to intelligible solution of my error message?
My Play application contains (as can bee seen from the IntelliJ project pane) some tens of 'external libraries'
Those are probably just transitive dependencies of your Play dependency, that is why sbt downloaded all of them and put them in your classpath so you could use them without you needing to tell it about them; because the pom of Play already did.
It is not that the build tool or the IDE magically added all those dependencies for you because they read your mind and magically understood you wanted them. And that for some reason the magic stopped working for your own library.
Why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
That is sufficient for the IDE to work, but not for the build tool. The build tool is independent of the IDE; it doesn't know about it. sbt just knows about the dependencies you configured in your definition file.
Even more, you should always configure your dependencies on your build tool and then import that in the IDE; rather than the opposite. IDEs are graphical tools, so their state can not be committed, can not be shared, can not keep track of changes, can not be used in CI / CD environments; additionally, different teammates may want to use different IDEs.
I resolved the error message by adding line in build.sbt file
libraryDependencies += "de.unruh" %% "scala-isabelle" % "master-SNAPSHOT"
and by subsequent run of sbt update.
Error is solved, but the main question still stand - why I had to do this? Why there are tens of dependencies that are not listed in build.sbt and why should I list my dependency in build.sbt and why it is not sufficient to list it Project-Setting--External Libraries in IntelliJ only?
OK, comment by #Luis_Miguel_Mejía_Suárez gave the explanation, that comment is the actual and expected answer to my question.

Trouble adding tensorflow dependency for scala 2.12.11

I am following a tutorial to perform object detection in scala. I am
having issues adding the tensorFlow dependency. I have followed the instructions on the official Tensorflow for Scala website http://platanios.org/tensorflow_scala/installation.html, but that doesn't seem to work. I also made sure to use the Java 11 JDK for the project. However, whenever I try to add the sbt dependency
libraryDependencies += "org.platanios" % "tensorflow" % "0.4.0" classifier "linux-cpu-x86_64", I get a "No dependencies found for given import" error in IntelliJ. Any idea on how to set this up properly ?
Try to replace one % in your dependency line to twice %%:
libraryDependencies += "org.platanios" %% "tensorflow" % "0.4.0" classifier "linux-cpu-x86_64"
On top of what the previous answer already suggested, I believe it's probably worth mentioning that (until 2.12) libraries in the 2.x are not binary-compatible across versions. The convention for Scala libraries is to append a _2.x to the published library JAR's artifact identifier. Since SBT was built around Scala (and it's its de facto standard build tool) it acknowledges this conventions and the %% operator will automatically append that extra "qualifier" based on the Scala version you are using.
Notice here on mvnrepository.com how the artifact identifier changes between the Maven and the SBT dependency declaration (in Maven, the artifact identifier is tensorflow_2.12, in SBT the %% allows you to not have to specify that).
The single % is generally used for Java dependencies (that are not affected by the aforementioned convention).
As an alternative (that I would suggest just to play around and see that there's no magic involved), you can also use % to specify a Scala dependency and explicitly mention the Scala version in the artifact identifier, as follows:
libraryDependencies += "org.platanios" % "tensorflow_2.12" % "0.4.0" classifier "linux-cpu-x86_64"
The good news is that starting from Scala 2.13 this issue was tackled at the very root using an intermediate representation that was also introduced to make sure the interoperability between Scala 2.13 and Scala 3.x compiled code.
EDIT
What you have found was actually an issue in the documentation that was already reported, I opened a PR to fix it.

How to use multiple versions of a library in Scala?

I am using a library say A in Scala which is dependent on version x.11 of another library say Z.
Now, I am also using a library say B which is dependent on version x.31 of Z.
This leads to compile error because we will have two versions of library Z, how can I use both libraries A and B in scala's sbt? Is there any way to specify it.
If completely replacing one dependency with a newer version happens to work, then Sparko's solution works. However, that isn't always the case.
If you want to include both versions of a library in the uber-jar produced by sbt-assembly, you'll need to use shading. See this post for an overview of what shading is, and some of the drawbacks associated with it.
Shading is covered in sbt-assembly's documentation here, but if you're anything like me, their way of explaining it will leave you more confused than you started. There's a good blog post, Spark, Uber Jars and Shading with sbt-assembly (waybackmachine link), that helps to demystify it a bit. Here's the relevant section:
I can shade over my typesafe config version, giving it a different
name so Spark won’t get confused between the versions. I quickly went
to my build.sbt file, and added the following code:
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.typesafe.config.**" -> "my_conf.#1")
.inLibrary("com.typesafe" % "config" % "1.3.0")
.inProject )
According to the documentation, this should place any class under
com.typesafe.config under the new package my_conf.
For your case, the solution would be adding something like this to your build.sbt file:
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("com.somecompany.**" -> "my_conf.#1")
.inLibrary("com.somecompany" % "libraryZ" % "0.11")
.inProject
)
In sbt, conflicts between libraries are configured using the conflict manager. By default, the latest revision is selected but this can also be overridden in you .sbt file:
conflictManager := ConflictManager.strict
If you're using sbt 0.13.6 or greater you will be warned when you have an incompatible binary version between your dependencies. In this situation, you could configure an override in your sbt file for the specific library:
dependencyOverrides += "org.raman" % "Z" % "x.11"
This will force the resolved version of Z to x.11 but not pull a direct dependency in.

ScalaTest errors on assert and test in Intellij

I am fairly new to Scala and Intellij and brand new to ScalaTest. I have set up Intellij 14 and Scala 2.11.4 with the latest Scala plugin and everything works fine. I then tried to introduce ScalaTest and I am very confused. I downloaded the latest version of ScalaTest (scalatest_2.11-2.2.1.jar) from scalatest.org. I went into my project structure and selected "Modules", clicked the green plus sign and added scalatest_2.11-2.2.1.jar. All went well and I can see the scalatest jar in External Libraries in my project. When I created my first test it imported org.scalatest.Funsuite and the class extended FunSuite and all was well. I then tried a very simple test that I copied from the Internet.
import org.scalatest.FunSuite
class MyTest extends FunSuite {
test("this is a test") {
assert(true)
}
}
I get 2 errors. It can not resolve the "assert" reference with that signature and for the "test" method it says unspecified value parameters Seq[Tag]: () => BoxedUnit.
I can see other method signatures for "assert" and "test" but I can not use the most basic ones.
Thanks for any help
While I didn't get exactly the same error messages, I had similar overall problems until I realized that you need two more JARs to use ScalaTest successfully: scala-reflect-2.11.4.jar and scala-xml-2.11-1.0.2.jar.
If you insist on doing this by hand you should also make sure that you have your tests in a directory that is marked as a "tests" directory -- so is highlighted in green: you can achieve this on the same "Modules" tab, but in the "Sources" sub-tab rather than "Dependencies". You also need to check that in the "Scope" column of the "Dependencies" sub-tab, the jars you added are marked as "Test" rather than "Compile".
If, on the other hand, you create an SBT project in IntelliJ rather than a regular Scala project, you could just add the following to your build.sbt:
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
Re-import the build.sbt when prompted, then you can start writing and running tests: all the dependencies and test directories are set up correctly and it all "just works". I just created an empty project this way and ran your example. It took about a minute from beginning to end.
Obviously you're facing a trade-off between managing JARs by hand and learning yet another tool, but at this point learning the very basics of SBT may be worth your while.