【问题标题】:Does Kotlin allow the use of backtick names for test classes?Kotlin 是否允许对测试类使用反引号名称?
【发布时间】:2021-04-26 20:54:40
【问题描述】:

Kotlin/JUnit 宣传使用反引号函数名称进行测试。我发现你也可以使用反引号的类名......

但我找不到任何参考。事物是 Kotlin 规范中的内容,还是只是“可能很快就会消失”的意外功能?

代码示例:

    @Nested
    inner class `when the entity path only specifies one location` {
        @BeforeEach
        fun setup() {
            entity = Entity(
                id = "entityid1",
                name = "entity1",
                type = "type",
                team = TeamColor.BLUE,
                currentLocation = Location(0.0, 10.0, 0.0),
                path = EntityPath(
                    startTime = "00:00:00",
                    timeLocation = listOf(
                        TimeLocation("00:00:00", Location(0.0, 10.0, 0.0)),
                    )
                ),
            )
        }

        @Test
        fun `it returns the first (only) location`() {
            val actualLocation = entityLocator.getEntityLocation(entity, calculation);
            val expectedLocation = Location(0.0, 10.0, 0.0)

            assertEquals(actualLocation, expectedLocation);
        }
    }

【问题讨论】:

  • 那只包括方法名而不是类名@JoachimIsaksson?

标签: kotlin junit backticks


【解决方案1】:

反引号是通用语言语法的一部分:您可以在任何可以写名称的地方(无论是函数、类、属性、变量还是其他),都可以将其放在反引号中并包含几乎任何字符(换行符或反引号)。

(最初的用例可能是为了与与 Kotlin 关键字同名的 Java 方法进行互操作,例如 System.out;但它更普遍有用,如您引用的测试所示。)

它记录在language grammar 本身中——在Identifier 的定义中——所以它是语言的预期部分,而且不太可能消失!

【讨论】:

    【解决方案2】:

    Kotlin coding convention 指定反引号仅用于测试:

    测试方法的名称

    在测试中(并且仅在测试中),您可以使用带有空格的方法名称并用反引号括起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-21
      • 2021-07-02
      • 2019-12-31
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多