Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spaces in test method names when targeting multiplatform

I really love the ability of Kotlin to add spaces to method names, which is a well-documented approach commonly used to make test names more readable:

In tests (and only in tests), it's acceptable to use method names with spaces enclosed in backticks. (Note that such method names are currently not supported by the Android runtime.) ...

class MyTestCase {
    @Test fun `ensure everything works`() { ... }
    ...
}

I have adopted them vigorously in a new library I'm developing, targeting the JVM. My tests show up beautifully in IntelliJ, and all is well.

Now, I am trying to port this library to be multiplatform (targeting both the JVM and JavaScript). When I add a test with spaces I receive the following Kotlin compiler error when Gradle build runs compileTestKotlin2Js:

Name contains illegal chars that can't appear in JavasScript identifier

Should I remove all spaces from my test methods, or is there another way to have the best of both worlds?

In addition, regarding the limitation that "such method names are currently not supported by the Android runtime", is this a real limitation? As long as I can run my tests on the JVM, I can still use the tested library (containing no spaces in any of the methods) targeting an Android runtime, correct?

like image 485
Steven Jeuris Avatar asked Jul 23 '18 09:07

Steven Jeuris


People also ask

Can method names have spaces?

In tests (and only in tests), it's acceptable to use method names with spaces enclosed in backticks.

Can method names have spaces in Java?

As @chris mentioned in the comments, the Official Java Code Conventions specifically states: Note that a blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.


1 Answers

For tests that are in a common or a JavaScript module, you are correct, you'll have to give up the nice names using backticks, and resort to the more traditional ensure_everything_works or similar. For tests that are JVM specific, you can continue to use backticks and spaces.

This includes JUnit tests for code that you're later planning to run on Android. The only limitation there is that you can't use spaces in tests that actually have to run in an Android environment, but the unit tests (I assume JUnit) run on your dev machine's JVM so they'll still work with spaces in the identifiers.

like image 186
zsmb13 Avatar answered Nov 14 '22 15:11

zsmb13