Why does Scala avoid spaces in method names?

When compiling the following code snippet:

class MyTest { @org.junit.Test def `test test`() { } } 

The test test method is placed in bytecode as test$u0020test .

Why is this happening and how can I turn it off?

Space is a valid method name identifier according to the JVM specification. Nor is such coding mentioned in the Scala language specification. Moreover, other JVM languages ​​such as Groovy and Groovy based on the Spock Framework do not encode spaces.

Why do I need this: user-friendly JUnit test names and test reports.

Java 1.8.0_45, Scala 2.11.6

+7
scala
source share
2 answers

Take a look at https://github.com/sbt/junit-interface

-s Try decoding Scala names in stack trace and test names. Discard unformatted names if the corresponding Scala library is not in the class path.

So we can say

 testOptions += Tests.Argument(TestFrameworks.JUnit, "-q", "-v", "-s") 

in build.sbt , then for the method

 def `get one hundred if fifty plus fifty started` {} 

test result shows

 [info] Test ScalaTest.get one hundred if fifty plus fifty started 

not

 [info] Test ScalaTest.get$u0020one$u0020hundred$u0020if$u0020fifty$u0020plus$u0020fifty started 
+2
source share

Otherwise, it will not be called from Java. Interaction with Java was an important consideration when developing Scala.

OTOH, experience has shown that it is best to stick with Java names in such cases.

+3
source share

All Articles