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?
In tests (and only in tests), it's acceptable to use method names with spaces enclosed in backticks.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With