Scalatest with eclipse shows errors while using Matchers - eclipse

I have an eclipse scala project which uses maven. Eclipse plugins for ScalaIDE and Scalatest are installed. I have tests like:
import org.scalatest._
class ExampleSpec extends FlatSpec with Matchers {
feature("Feature A Test") {
scenario("Foo scenario 1") {
val a = FooClass().getResult()
a.count shouldBe 1 // IDE shows error: value shouldBe is not a member of Long
a(0).getString(0) shouldBe "FOO" // IDE shows error: value shouldBe is not a member of String
}
}
}
The maven compilation and the tests run fine, but in eclipse when I open this file, I see an error in eclipse wherever I am using a Matcher as mentioned in the comments above. Eg.
value shouldBe is not a member of Long
What am I missing? A scala test file shows hundreds of problems.

After adding the following dummy code:
case class Bar() {
def count = Array(Bar())
def getString(x: Int) = Array("aqq")
def apply[T](x: Int) = this
}
case class FooClass() {
def getResult() = Bar()
}
and changing FlatSpec to FeatureSpec as this is the syntax you are using in your ExampleSpec, the code compiles without any issues.
If it's still not the case for you I can suggest creating simple build.sbt and generating project with Eclipse sbt plugin.

I know this is old, but I had the same issue with eclipse (late 2018), and I was able to fix this by making sure the test was NOT in the default package. That is, add "package org.scalatest.examples.flatspec" to the beginning of your test, as an example, and move the test into that package.

Related

VS Code and Metals: Run 'Build import' to enable code navigation

I have a Scala- / Mill-Project that I wan't to import into VSCode.
The Metals Doctor gives me the following warnings:
Is there something missing in my project?
Here is my Mill configuration (build.sc):
import mill._
import mill.define.Target
import scalalib._
trait MyModule extends ScalaModule {
def scalaVersion = "2.13.1"
object version {
val cats = "2.0.0"
..
}
object libs {
val cats = ivy"org.typelevel::cats-core:${version.cats}"
..
}
object test extends Tests {
...
}
}
object entity extends MyModule {
override def ivyDeps = {
Agg(
libs.cats,
..
)
}
}
object macros extends MyModule {
..
}
Here is the whole project: https://github.com/pme123/zio-examples
The console output shows no warnings, here is the end:
...
time: connected to build server in 0.33s
time: imported build in 0.41s
time: indexed workspace in 3.85s
no build target: /Users/mpa/dev/Github/pme123/zio-examples/build.sc
This error/help message is misleading, as it presumes you are using Bloop, in which case re-importing solves the isssue (from experience). When you are using Mill's built-in BSP server, this won't change much.
Mill's recent 0.10.0-M4 release got a major BSP revamp. Also Metals improved it's Mill support. So you should have a better overall experience.
Here is some documentation: https://com-lihaoyi.github.io/mill/mill/Intro_to_Mill.html#_build_server_protocol_bsp

Scala 2.10 reflection: ClassSymbol.isCaseClass works in scala console but not in script/app

I am playing around with reflection in Scala 2.10.0-M7 and stumbled upon the ClassSymbol.isCaseClass method which behaves like expected in the scala console but not when executed as a java application or as a scala script.
I've defined TestScript.scala like this:
import reflect.runtime.currentMirror
case class TestCase(foo: String)
object Test {
def main(args: Array[String]) {
val classSymbol = currentMirror.reflect(new TestCase("foo")).symbol
val isCaseClass = classSymbol.isCaseClass
println(s"isCaseClass: $isCaseClass")
}
}
Test.main(Array())
If I execute it on the command line calling
$ scala TestScript.scala
I get this output:
isCaseClass: false
If I instead input the code into the interactive scala shell or load it like this:
scala> :load TestScript.scala
I get the following correct output:
Loading TestScript.scala...
import reflect.runtime.currentMirror
defined class TestCase
defined module Test
isCaseClass: true
If I compile it and execute it as a standard Java app I get false as result for ClassSymbol.isCase again.
What am I missing? What are the differences between the scala console environment and the java runtime environment? How can I get the correct result in a real application?
https://issues.scala-lang.org/browse/SI-6277
val classSymbol = cm.reflect(new TestCase("foo")).symbol
{ classSymbol.typeSignature }
val isCaseClass = classSymbol.isCaseClass
println(s"isCaseClass: $isCaseClass")
Edit: to answer your last question, you wouldn't be using a milestone in a real application. :)
Upd. Fixed since Scala 2.10.0-RC1.

Confusion regarding working of protected[some_scope] and inheritance in scala

This question is about protected scope of scala. Following code runs perfectly.
//In firstfile.scala
package A{
class test{
protected[test] var a=0
}
}
package B{
class test1 extends A.test{
println(a)
}
}
But when I put package B in some other file,then compilation fails saying not found value a
// In secondfile.scala
import A.test
package B{
class test1 extends test{
println(a)
}
}
I am running this code in intellij.
what can be the reason of this? Are not the two codes same?
I think you've found a bug. I can reproduce it in Eclipse with Scala IDE 2.0.0-beta9 with Scala 2.9.0-1. First, let's create Foo.scala:
package a
class Foo {
protected[Foo] var x = 0
}
and then Bar.scala:
package b
class Bar extends a.Foo {
println(x) // Error: Not found: value x
}
Eclipse telling us, there is an error. But after cleaning a project this error disappears. And I checked, with scalac everything compiles with first attempt (who would doubt).
Update:
I've opened a ticket #1000567 on Scala IDE bugtracker regarding this issue.

#Test method in Scala trait not found

does somebody know why #Test annotated methods which are inherited from a Scala trait are not found by the JUnit 4 test runner? It gives me "No JUnit tests found".
class FooTests extends BarTesting
But the following modification works?
import junit.framework.TestCase
class FooTests extends TestCase with BarTesting
In the Scala trait BarTesting I defined methods with the following signature.
#Test
final def testBar() {
// ...
}
This is indeed a bug in Eclipse. You can raise as such if you like. http://www.assembla.com/spaces/scala-ide/tickets.
When you extend TestCase the test is being run because it starts with test, not because of the annotation. There was a problem with recognition of annotations, which is how the junit stuff works, and I haven't looked to see if it is fixed yet to make the junit stuff work.
Your best bet is to:
Raise the bug against scala-ide
Add #RunWith[classOf[JUnit]) to your class
The following works:
trait BarTesting {
#Test final def testBar() {
println("Hello world")
}
}
#RunWith(classOf[JUnit4])
class FooTesting extends BarTesting {
}
And I'll try and fix the bug.
EDIT: In the latest versions of scala-ide (as of 9 November 2011), this now works.

Difference in Scala type inference, Eclipse vs. Maven

I'm getting a compilation failure using the Maven Scala plugin that I'm not getting using The Eclipse Scala IDE. First the code:
package com.example
trait SomeTrait[OUTPUT, INPUT] {
def apply(request: INPUT, f: OUTPUT => Unit);
}
class SomeClass extends SomeTrait[String,String] {
def apply(request, f) {
f.apply(request.toUpperCase())
}
}
object App extends Application {
override def main(args: Array[String]) {
new SomeClass()("test", (value) => { println(value)})
}
}
Eclipse Scala IDE is fine but Maven gives me this error:
[ERROR] .../src/main/scala/com/example/App.scala:8:
error: ':' expected but ',' found.
[INFO] def apply(request, f) {
[INFO] ^
[ERROR] .../src/main/scala/com/example/App.scala:11:
error: identifier expected but '}' found.
[INFO] }
If I specify the types, as in:
class SomeClass extends SomeTrait[String,String] {
def apply(request: String, f: String => Unit) {
f.apply(request.toUpperCase())
}
}
It compiles in both.
Versions etc.:
Scala version: 2.8.1
Scala IDE for Eclipse: 1.0.0.201011130651
Maven: 3.0
Maven Scala Plugin: 2.15.0
Java: 1.6.0_17
Eclipse is wrong here ... you should be seeing the same compile time error there as you do in the command-line case.
What's baffling is that the Scala compiler embedded in Eclipse (which is just scalac 2.8.1.final running in an incremental compilation mode) is managing to successfully compile the source you originally provided and is inferring the argument types that you want ... classfiles are generated, and in the binary output SomeClass.apply has the signature you would expect.
This just shouldn't be happening.
UPDATE:
As Paul noted the IDE is behaving as if -Yinfer-argument-types were enabled. It's also behaving as if -Ydependent-method-types were enabled. Which suggests that there's something awry with the new logic behind the -Xexperimental option which manifests itself when compiler Settings instances are created in the way that the IDE does it.
You're using -Yinfer-argument-types wherever it's working.