Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Scala escapes spaces in method names?

Tags:

scala

When compiling following code snippet:

class MyTest {

  @org.junit.Test
  def `test test`() {

  }
}

Method test test is being put to bytecode as test$u0020test.

Why it happens and how can this be disabled?

Space is valid identifier for method name according to JVM spec. Also, there is no mention of such encoding in Scala language specification. Moreover, other JVM languages like Groovy and Groovy-based Spock Framework do not encode spaces.

Why do i need this: Human-friendly JUnit test names and test reports.

Java 1.8.0_45, Scala 2.11.6

like image 848
Sergey Alaev Avatar asked May 17 '15 10:05

Sergey Alaev


2 Answers

Look at https://github.com/sbt/junit-interface

-s Try to decode Scala names in stack traces and test names. Fall back silently to non-decoded names if no matching Scala library is on the class path.

So we can say

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

in build.sbt file, then for a method

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

the 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
like image 150
Yanbin Avatar answered Sep 27 '22 18:09

Yanbin


It wouldn't be callable from Java otherwise. Interoperability with Java was an important consideration in the design of Scala.

OTOH, experience has shown that it's best to just stick to Java names in such cases.

like image 38
Daniel C. Sobral Avatar answered Sep 27 '22 18:09

Daniel C. Sobral